📄 tools.cs
字号:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Globalization;
using ScrewTurn.Wiki.PluginFramework;
namespace ScrewTurn.Wiki {
/// <summary>
/// Contains useful Tools.
/// </summary>
public static class Tools {
/// <summary>
/// Gets all the included files for the HTML Head, such as CSS, JavaScript and Icon files.
/// </summary>
public static string Includes {
get {
StringBuilder result = new StringBuilder();
string[] css = Directory.GetFiles(Settings.ThemesDirectory + Settings.Theme, "*.css");
string firstChunk;
for(int i = 0; i < css.Length; i++) {
if(Path.GetFileName(css[i]).IndexOf("_") != -1) {
firstChunk = Path.GetFileName(css[i]).Substring(0, Path.GetFileName(css[i]).IndexOf("_")).ToLower();
if(firstChunk.Equals("screen") || firstChunk.Equals("print") || firstChunk.Equals("all") ||
firstChunk.Equals("aural") || firstChunk.Equals("braille") || firstChunk.Equals("embossed") ||
firstChunk.Equals("handheld") || firstChunk.Equals("projection") || firstChunk.Equals("tty") || firstChunk.Equals("tv")) {
result.Append(@"<link rel=""stylesheet"" media=""" + firstChunk + @""" href=""" + Settings.ThemePath + Path.GetFileName(css[i]) + @""" type=""text/css"" />" + "\n");
}
else {
result.Append(@"<link rel=""stylesheet"" href=""" + Settings.ThemePath + Path.GetFileName(css[i]) + @""" type=""text/css"" />" + "\n");
}
}
else {
result.Append(@"<link rel=""stylesheet"" href=""" + Settings.ThemePath + Path.GetFileName(css[i]) + @""" type=""text/css"" />" + "\n");
}
}
string[] js = Directory.GetFiles(Settings.ThemesDirectory + Settings.Theme, "*.js");
for(int i = 0; i < js.Length; i++) {
result.Append(@"<script src=""" + Settings.ThemePath + Path.GetFileName(js[i]) + @""" type=""text/javascript""></script>" + "\n");
}
string[] icons = Directory.GetFiles(Settings.ThemesDirectory + Settings.Theme, "Icon.*");
if(icons.Length > 0) {
result.Append(@"<link rel=""shortcut icon"" href=""" + Settings.ThemePath + Path.GetFileName(icons[0]) + @""" type=""");
switch(Path.GetExtension(icons[0]).ToLower()) {
case ".ico":
result.Append("image/x-icon");
break;
case ".gif":
result.Append("image/gif");
break;
case ".png":
result.Append("image/png");
break;
}
result.Append(@""" />" + "\n");
}
js = Directory.GetFiles(Settings.JsDirectory, "*.js");
for(int i = 0; i < js.Length; i++) {
result.Append(@"<script type=""text/javascript"" src=""" + Settings.JsDirectoryName + "/" + Path.GetFileName(js[i]) + @"""></script>" + "\n");
}
// Include HTML Head
result.Append(LoadFile(Settings.HtmlHeadFile));
return result.ToString();
}
}
/// <summary>
/// Returns the content of a file.
/// </summary>
/// <param name="path">The full path of the file to read.</param>
/// <returns>The content of a file.</returns>
public static string LoadFile(string path) {
if(!File.Exists(path)) return null;
FileStream fs = null;
for(int i = 0; i < Settings.FileAccessTries; i++) {
try {
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
break;
}
catch {
Thread.Sleep(Settings.FileAccessTryDelay);
}
}
if(fs == null) throw new IOException("Unable to open the file: " + path);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
string res = sr.ReadToEnd();
sr.Close();
return res;
}
/// <summary>
/// Writes the content of a File.
/// </summary>
/// <param name="path">The full path of the file to write.</param>
/// <param name="content">The content of the file.</param>
public static void WriteFile(string path, string content) {
FileStream fs = null;
for(int i = 0; i < Settings.FileAccessTries; i++) {
try {
fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
break;
}
catch {
Thread.Sleep(Settings.FileAccessTryDelay);
}
}
if(fs == null) throw new IOException("Unable to open the file: " + path);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.Write(content);
sw.Close();
Log.LogEntry("File " + Path.GetFileName(path) + " written", EntryType.General, "SYSTEM");
}
/// <summary>
/// Appends some content to a File. If the file doesn't exist, it is created.
/// </summary>
/// <param name="path">The full path of the file to append the content to.</param>
/// <param name="content">The content to append to the file.</param>
public static void AppendFile(string path, string content) {
FileStream fs = null;
for(int i = 0; i < Settings.FileAccessTries; i++) {
try {
fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None);
break;
}
catch {
Thread.Sleep(Settings.FileAccessTryDelay);
}
}
if(fs == null) throw new IOException("Unable to open the file: " + path);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.Write(content);
sw.Close();
Log.LogEntry("File " + Path.GetFileName(path) + " written", EntryType.General, "SYSTEM");
}
/// <summary>
/// Converts a byte number into a KB number.
/// </summary>
/// <param name="bytes">The # of bytes.</param>
/// <returns>The # of KB.</returns>
public static float BytesToKiloBytes(long bytes) {
return (float)bytes / 1024F;
}
/// <summary>
/// Converts a byte number into a KB number, in a string format.
/// </summary>
/// <param name="bytes">The # of bytes.</param>
/// <returns>The # of KB.</returns>
public static string BytesToKiloBytesString(long bytes) {
return string.Format("{0:N2}", BytesToKiloBytes(bytes));
}
/// <summary>
/// Converts a byte number into a string, formatted using KB, MB or GB.
/// </summary>
/// <param name="bytes">The # of bytes.</param>
/// <returns>The formatted string.</returns>
public static string BytesToString(long bytes) {
if(bytes < 1024) return bytes.ToString() + " B";
else if(bytes < 1048576) return string.Format("{0:N2} KB", (float)bytes / 1024F);
else if(bytes < 1073741824) return string.Format("{0:N2} MB", (float)bytes / 1048576F);
else return string.Format("{0:N2} GB", (float)bytes / 1073741824F);
}
/// <summary>
/// Computes the Disk Space Usage of a directory.
/// </summary>
/// <param name="dir">The directory.</param>
/// <returns>The used Disk Space, in bytes.</returns>
public static long DiskUsage(string dir) {
string[] files = Directory.GetFiles(dir);
string[] directories = Directory.GetDirectories(dir);
long result = 0;
FileInfo file;
for(int i = 0; i < files.Length; i++) {
file = new FileInfo(files[i]);
result += file.Length;
}
for(int i = 0; i < directories.Length; i++) {
result += DiskUsage(directories[i]);
}
return result;
}
/// <summary>
/// Generates the standard 5-digit Page Version string.
/// </summary>
/// <param name="version">The Page version.</param>
/// <returns>The 5-digit Version string.</returns>
public static string GetVersionString(int version) {
string result = version.ToString();
int len = result.Length;
for(int i = 0; i < 5 - len; i++) {
result = "0" + result;
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -