📄 filesystemobject.cs
字号:
row[3] = info2.Extension.Replace(".", "");
row[4] = info2.CreationTime;
row[5] = info2.LastWriteTime;
table.Rows.Add(row);
}
}
return table;
}
public static long[] GetDirInfos(string dir)
{
long[] numArray = new long[3];
DirectoryInfo d = new DirectoryInfo(dir);
return DirInfo(d);
}
private static string[] getDirs(string dir)
{
return Directory.GetDirectories(dir);
}
private static string[] getFiles(string dir)
{
return Directory.GetFiles(dir);
}
public static string GetFileSize(string filePath)
{
FileInfo info = new FileInfo(filePath);
float num = info.Length / 0x400L;
return (num.ToString() + "KB");
}
public static bool IsExist(string file, FsoMethod method)
{
if (method == FsoMethod.File)
{
return File.Exists(file);
}
return ((method == FsoMethod.Folder) && Directory.Exists(file));
}
public static bool IsExistCategoryDirAndCreate(string categorDir, HttpContext context)
{
if (context != null)
{
string file = Path.Combine(context.Request.PhysicalApplicationPath, categorDir);
if (IsExist(file, FsoMethod.Folder))
{
return true;
}
Create(file, FsoMethod.Folder);
}
return false;
}
public static void Move(string oldFile, string newFile, FsoMethod method)
{
if (method == FsoMethod.File)
{
File.Move(oldFile, newFile);
}
if (method == FsoMethod.Folder)
{
Directory.Move(oldFile, newFile);
}
}
public static string ReadFile(string file)
{
string str = "";
using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
StreamReader reader = new StreamReader(stream, Encoding.Default);
try
{
return reader.ReadToEnd();
}
catch
{
return str;
}
finally
{
if (reader != null)
{
reader.Dispose();
}
}
}
return str;
}
public static void ReplaceFileContent(string dir, string originalContent, string newContent)
{
DirectoryInfo info = new DirectoryInfo(dir);
foreach (FileInfo info2 in info.GetFiles("*.*", SearchOption.AllDirectories))
{
StreamReader reader = info2.OpenText();
string str = reader.ReadToEnd();
reader.Dispose();
if (str.Contains(originalContent))
{
WriteFile(info2.FullName, str.Replace(originalContent, newContent));
}
}
}
public static DataTable SearchFileContent(string dir, string searchPattern)
{
DataTable table = new DataTable();
DirectoryInfo info = new DirectoryInfo(dir);
table.Columns.Add("name");
table.Columns.Add("type");
table.Columns.Add("size", typeof(int));
table.Columns.Add("content_type");
table.Columns.Add("createTime", typeof(DateTime));
table.Columns.Add("lastWriteTime", typeof(DateTime));
foreach (FileInfo info2 in info.GetFiles("*.*", SearchOption.AllDirectories))
{
DataRow row = table.NewRow();
StreamReader reader = info2.OpenText();
string str = reader.ReadToEnd();
reader.Dispose();
if (str.Contains(searchPattern))
{
row[0] = info2.FullName.Remove(0, info.FullName.Length);
row[1] = 2;
row[2] = info2.Length;
row[3] = info2.Extension.Replace(".", "");
row[4] = info2.CreationTime;
row[5] = info2.LastWriteTime;
table.Rows.Add(row);
}
}
return table;
}
public static DataTable SearchFiles(string dir, string searchPattern)
{
DataTable table = new DataTable();
DirectoryInfo info = new DirectoryInfo(dir);
table.Columns.Add("name");
table.Columns.Add("type");
table.Columns.Add("size", typeof(int));
table.Columns.Add("content_type");
table.Columns.Add("createTime", typeof(DateTime));
table.Columns.Add("lastWriteTime", typeof(DateTime));
foreach (FileInfo info2 in info.GetFiles(searchPattern, SearchOption.AllDirectories))
{
DataRow row = table.NewRow();
row[0] = info2.FullName.Remove(0, info.FullName.Length);
row[1] = 2;
row[2] = info2.Length;
row[3] = info2.Extension.Replace(".", "");
row[4] = info2.CreationTime;
row[5] = info2.LastWriteTime;
table.Rows.Add(row);
}
return table;
}
public static DataTable SearchTemplateFiles(string dir, string searchPattern)
{
DataTable table = new DataTable();
DirectoryInfo info = new DirectoryInfo(dir);
string str = searchPattern;
string str2 = searchPattern.ToLower();
int length = searchPattern.Length;
if (length < 4)
{
str = "*" + str + "*.html";
}
else if ((str2.Substring(length - 4, 4) != ".html") || (str2.Substring(length - 3, 3) != ".htm"))
{
str = "*" + str + "*.html";
}
table.Columns.Add("name");
table.Columns.Add("type");
table.Columns.Add("size", typeof(int));
table.Columns.Add("content_type");
table.Columns.Add("createTime", typeof(DateTime));
table.Columns.Add("lastWriteTime", typeof(DateTime));
try
{
foreach (FileInfo info2 in info.GetFiles(str, SearchOption.AllDirectories))
{
DataRow row = table.NewRow();
row[0] = info2.FullName.Remove(0, info.FullName.Length).Replace("/", "\"");
row[1] = 2;
row[2] = info2.Length;
row[3] = info2.Extension.Replace(".", "");
row[4] = info2.CreationTime;
row[5] = info2.LastWriteTime;
table.Rows.Add(row);
}
}
catch (ArgumentException)
{
}
return table;
}
public static string WriteAppend(string file, string fileContent)
{
string str;
FileInfo info = new FileInfo(file);
if (!Directory.Exists(info.DirectoryName))
{
Directory.CreateDirectory(info.DirectoryName);
}
FileStream stream = new FileStream(file, FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("utf-8"));
try
{
writer.Write(fileContent);
str = fileContent;
}
catch (Exception exception)
{
throw new FileNotFoundException(exception.ToString());
}
finally
{
writer.Flush();
stream.Flush();
writer.Close();
stream.Close();
}
return str;
}
public static string WriteFile(string file, string fileContent)
{
string str;
FileInfo info = new FileInfo(file);
if (!Directory.Exists(info.DirectoryName))
{
Directory.CreateDirectory(info.DirectoryName);
}
FileStream stream = new FileStream(file, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
try
{
writer.Write(fileContent);
str = fileContent;
}
catch (Exception exception)
{
throw new FileNotFoundException(exception.ToString());
}
finally
{
writer.Flush();
stream.Flush();
writer.Close();
stream.Close();
}
return str;
}
public static void WriteFile(string file, string fileContent, bool append)
{
FileInfo info = new FileInfo(file);
if (!Directory.Exists(info.DirectoryName))
{
Directory.CreateDirectory(info.DirectoryName);
}
StreamWriter writer = new StreamWriter(file, append, Encoding.GetEncoding("utf-8"));
try
{
writer.Write(fileContent);
}
catch (Exception exception)
{
throw new FileNotFoundException(exception.ToString());
}
finally
{
writer.Flush();
writer.Close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -