- 浏览: 149410 次
- 性别:
- 来自: 深圳
文章分类
最新评论
分享一个收集到的文件和目录操作类FileSystemObject
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Web;
public abstract class FileSystemObject
{
// Methods
protected FileSystemObject()
{
}
public static string ConvertSizeToShow(int fileSize)
{
int num = fileSize / 0x400;
if (num < 1)
{
return (fileSize.ToString() + "<span style='color:red'> B</span>");
}
if (num < 0x400)
{
return (num.ToString() + "<span style='color:red'> KB</span>");
}
int num2 = num / 0x400;
if (num2 < 1)
{
return (num.ToString() + "<span style='color:red'> KB</span>");
}
if (num2 >= 0x400)
{
num2 /= 0x400;
return (num2.ToString() + "<span style='color:red'> GB</span>");
}
return (num2.ToString() + "<span style='color:red'> MB</span>");
}
public static void CopyDirectory(string oldDir, string newDir)
{
DirectoryInfo od = new DirectoryInfo(oldDir);
CopyDirInfo(od, oldDir, newDir);
}
private static void CopyDirInfo(DirectoryInfo od, string oldDir, string newDir)
{
if (!DirIsExist(newDir))
{
Create(newDir, FsoMethod.Folder);
}
foreach (DirectoryInfo info in od.GetDirectories())
{
CopyDirInfo(info, info.FullName, newDir + info.FullName.Replace(oldDir, ""));
}
foreach (FileInfo info2 in od.GetFiles())
{
CopyFile(info2.FullName, newDir + info2.FullName.Replace(oldDir, ""));
}
}
public static DataTable CopyDT(DataTable parent, DataTable child)
{
for (int i = 0; i < child.Rows.Count; i++)
{
DataRow row = parent.NewRow();
for (int j = 0; j < parent.Columns.Count; j++)
{
row[j] = child.Rows[i][j];
}
parent.Rows.Add(row);
}
return parent;
}
public static void CopyFile(string oldFile, string newFile)
{
File.Copy(oldFile, newFile, true);
}
public static bool CopyFileStream(string oldPath, string newPath)
{
try
{
FileStream input = new FileStream(oldPath, FileMode.Open, FileAccess.Read);
FileStream output = new FileStream(newPath, FileMode.Create, FileAccess.Write);
BinaryReader reader = new BinaryReader(input);
BinaryWriter writer = new BinaryWriter(output);
reader.BaseStream.Seek(0L, SeekOrigin.Begin);
reader.BaseStream.Seek(0L, SeekOrigin.End);
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
writer.Write(reader.ReadByte());
}
reader.Close();
writer.Close();
input.Flush();
input.Close();
output.Flush();
output.Close();
return true;
}
catch
{
return false;
}
}
public static void Create(string file, FsoMethod method)
{
try
{
if (method == FsoMethod.File)
{
WriteFile(file, "");
}
else if (method == FsoMethod.Folder)
{
Directory.CreateDirectory(file);
}
}
catch
{
throw new UnauthorizedAccessException("没有权限!");
}
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="dirPath">相对路径</param>
/// <returns>是否成功</returns>
public static bool CreateDirectory(string dirPath)
{
if (string.IsNullOrEmpty(dirPath))
return false;
dirPath = HttpContext.Current.Server.MapPath(dirPath);
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
if (dirInfo.Exists)
return true;
try
{
Directory.CreateDirectory(dirPath);
return true;
}
catch
{
return false;
}
}
public static void Delete(string file, FsoMethod method)
{
if ((method == FsoMethod.File) && File.Exists(file))
{
File.Delete(file);
}
if ((method == FsoMethod.Folder) && Directory.Exists(file))
{
Directory.Delete(file, true);
}
}
private static long[] DirInfo(DirectoryInfo d)
{
long[] numArray = new long[3];
long num = 0L;
long num2 = 0L;
long num3 = 0L;
FileInfo[] files = d.GetFiles();
num3 += files.Length;
foreach (FileInfo info in files)
{
num += info.Length;
}
DirectoryInfo[] directories = d.GetDirectories();
num2 += directories.Length;
foreach (DirectoryInfo info2 in directories)
{
num += DirInfo(info2)[0];
num2 += DirInfo(info2)[1];
num3 += DirInfo(info2)[2];
}
numArray[0] = num;
numArray[1] = num2;
numArray[2] = num3;
return numArray;
}
private static DataTable GetDirectoryAllInfo(DirectoryInfo d, FsoMethod method)
{
DataRow row;
DataTable parent = new DataTable();
parent.Columns.Add("name");
parent.Columns.Add("rname");
parent.Columns.Add("content_type");
parent.Columns.Add("type");
parent.Columns.Add("path");
parent.Columns.Add("creatime", typeof(DateTime));
parent.Columns.Add("size", typeof(int));
foreach (DirectoryInfo info in d.GetDirectories())
{
if (method == FsoMethod.File)
{
parent = CopyDT(parent, GetDirectoryAllInfo(info, method));
}
else
{
row = parent.NewRow();
row[0] = info.Name;
row[1] = info.FullName;
row[2] = "";
row[3] = 1;
row[4] = info.FullName.Replace(info.Name, "");
row[5] = info.CreationTime;
row[6] = "";
parent.Rows.Add(row);
parent = CopyDT(parent, GetDirectoryAllInfo(info, method));
}
}
if (method != FsoMethod.Folder)
{
foreach (FileInfo info2 in d.GetFiles())
{
row = parent.NewRow();
row[0] = info2.Name;
row[1] = info2.FullName;
row[2] = info2.Extension.Replace(".", "");
row[3] = 2;
row[4] = info2.DirectoryName + @"\";
row[5] = info2.CreationTime;
row[6] = info2.Length;
parent.Rows.Add(row);
}
}
return parent;
}
public static DataTable GetDirectoryAllInfos(string dir, FsoMethod method)
{
DataTable directoryAllInfo;
try
{
DirectoryInfo d = new DirectoryInfo(dir);
directoryAllInfo = GetDirectoryAllInfo(d, method);
}
catch (Exception exception)
{
throw new FileNotFoundException(exception.ToString());
}
return directoryAllInfo;
}
public static DataTable GetDirectoryInfos(string dir, FsoMethod method)
{
DataRow row;
DataTable table = new DataTable();
table.Columns.Add("name", typeof(string));
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));
if (method != FsoMethod.File)
{
for (int i = 0; i < getDirs(dir).Length; i++)
{
row = table.NewRow();
DirectoryInfo d = new DirectoryInfo(getDirs(dir)[i]);
long[] numArray = DirInfo(d);
row[0] = d.Name;
row[1] = 1;
row[2] = numArray[0];
row[3] = "";
row[4] = d.CreationTime;
row[5] = d.LastWriteTime;
table.Rows.Add(row);
}
}
if (method != FsoMethod.Folder)
{
for (int j = 0; j < getFiles(dir).Length; j++)
{
row = table.NewRow();
FileInfo info2 = new FileInfo(getFiles(dir)[j]);
row[0] = info2.Name;
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 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");
}
/// <summary>
/// 返回文件是否存在
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>是否存在</returns>
public static bool FileExists(string filename)
{
return File.Exists(filename);
}
/// <summary>
/// 返回目录是否存在
/// </summary>
/// <param name="filename">文件目录</param>
/// <returns>是否存在</returns>
public static bool DirIsExist(string file)
{
return Directory.Exists(file);
}
public static bool IsExistCategoryDirAndCreate(string categorDir, HttpContext context)
{
if (context != null)
{
string file = Path.Combine(context.Request.PhysicalApplicationPath, categorDir);
if (DirIsExist(file))
{
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();
}
}
}
}
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();
}
}
}
public enum FsoMethod
{
Folder,
File,
All
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Web;
public abstract class FileSystemObject
{
// Methods
protected FileSystemObject()
{
}
public static string ConvertSizeToShow(int fileSize)
{
int num = fileSize / 0x400;
if (num < 1)
{
return (fileSize.ToString() + "<span style='color:red'> B</span>");
}
if (num < 0x400)
{
return (num.ToString() + "<span style='color:red'> KB</span>");
}
int num2 = num / 0x400;
if (num2 < 1)
{
return (num.ToString() + "<span style='color:red'> KB</span>");
}
if (num2 >= 0x400)
{
num2 /= 0x400;
return (num2.ToString() + "<span style='color:red'> GB</span>");
}
return (num2.ToString() + "<span style='color:red'> MB</span>");
}
public static void CopyDirectory(string oldDir, string newDir)
{
DirectoryInfo od = new DirectoryInfo(oldDir);
CopyDirInfo(od, oldDir, newDir);
}
private static void CopyDirInfo(DirectoryInfo od, string oldDir, string newDir)
{
if (!DirIsExist(newDir))
{
Create(newDir, FsoMethod.Folder);
}
foreach (DirectoryInfo info in od.GetDirectories())
{
CopyDirInfo(info, info.FullName, newDir + info.FullName.Replace(oldDir, ""));
}
foreach (FileInfo info2 in od.GetFiles())
{
CopyFile(info2.FullName, newDir + info2.FullName.Replace(oldDir, ""));
}
}
public static DataTable CopyDT(DataTable parent, DataTable child)
{
for (int i = 0; i < child.Rows.Count; i++)
{
DataRow row = parent.NewRow();
for (int j = 0; j < parent.Columns.Count; j++)
{
row[j] = child.Rows[i][j];
}
parent.Rows.Add(row);
}
return parent;
}
public static void CopyFile(string oldFile, string newFile)
{
File.Copy(oldFile, newFile, true);
}
public static bool CopyFileStream(string oldPath, string newPath)
{
try
{
FileStream input = new FileStream(oldPath, FileMode.Open, FileAccess.Read);
FileStream output = new FileStream(newPath, FileMode.Create, FileAccess.Write);
BinaryReader reader = new BinaryReader(input);
BinaryWriter writer = new BinaryWriter(output);
reader.BaseStream.Seek(0L, SeekOrigin.Begin);
reader.BaseStream.Seek(0L, SeekOrigin.End);
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
writer.Write(reader.ReadByte());
}
reader.Close();
writer.Close();
input.Flush();
input.Close();
output.Flush();
output.Close();
return true;
}
catch
{
return false;
}
}
public static void Create(string file, FsoMethod method)
{
try
{
if (method == FsoMethod.File)
{
WriteFile(file, "");
}
else if (method == FsoMethod.Folder)
{
Directory.CreateDirectory(file);
}
}
catch
{
throw new UnauthorizedAccessException("没有权限!");
}
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="dirPath">相对路径</param>
/// <returns>是否成功</returns>
public static bool CreateDirectory(string dirPath)
{
if (string.IsNullOrEmpty(dirPath))
return false;
dirPath = HttpContext.Current.Server.MapPath(dirPath);
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
if (dirInfo.Exists)
return true;
try
{
Directory.CreateDirectory(dirPath);
return true;
}
catch
{
return false;
}
}
public static void Delete(string file, FsoMethod method)
{
if ((method == FsoMethod.File) && File.Exists(file))
{
File.Delete(file);
}
if ((method == FsoMethod.Folder) && Directory.Exists(file))
{
Directory.Delete(file, true);
}
}
private static long[] DirInfo(DirectoryInfo d)
{
long[] numArray = new long[3];
long num = 0L;
long num2 = 0L;
long num3 = 0L;
FileInfo[] files = d.GetFiles();
num3 += files.Length;
foreach (FileInfo info in files)
{
num += info.Length;
}
DirectoryInfo[] directories = d.GetDirectories();
num2 += directories.Length;
foreach (DirectoryInfo info2 in directories)
{
num += DirInfo(info2)[0];
num2 += DirInfo(info2)[1];
num3 += DirInfo(info2)[2];
}
numArray[0] = num;
numArray[1] = num2;
numArray[2] = num3;
return numArray;
}
private static DataTable GetDirectoryAllInfo(DirectoryInfo d, FsoMethod method)
{
DataRow row;
DataTable parent = new DataTable();
parent.Columns.Add("name");
parent.Columns.Add("rname");
parent.Columns.Add("content_type");
parent.Columns.Add("type");
parent.Columns.Add("path");
parent.Columns.Add("creatime", typeof(DateTime));
parent.Columns.Add("size", typeof(int));
foreach (DirectoryInfo info in d.GetDirectories())
{
if (method == FsoMethod.File)
{
parent = CopyDT(parent, GetDirectoryAllInfo(info, method));
}
else
{
row = parent.NewRow();
row[0] = info.Name;
row[1] = info.FullName;
row[2] = "";
row[3] = 1;
row[4] = info.FullName.Replace(info.Name, "");
row[5] = info.CreationTime;
row[6] = "";
parent.Rows.Add(row);
parent = CopyDT(parent, GetDirectoryAllInfo(info, method));
}
}
if (method != FsoMethod.Folder)
{
foreach (FileInfo info2 in d.GetFiles())
{
row = parent.NewRow();
row[0] = info2.Name;
row[1] = info2.FullName;
row[2] = info2.Extension.Replace(".", "");
row[3] = 2;
row[4] = info2.DirectoryName + @"\";
row[5] = info2.CreationTime;
row[6] = info2.Length;
parent.Rows.Add(row);
}
}
return parent;
}
public static DataTable GetDirectoryAllInfos(string dir, FsoMethod method)
{
DataTable directoryAllInfo;
try
{
DirectoryInfo d = new DirectoryInfo(dir);
directoryAllInfo = GetDirectoryAllInfo(d, method);
}
catch (Exception exception)
{
throw new FileNotFoundException(exception.ToString());
}
return directoryAllInfo;
}
public static DataTable GetDirectoryInfos(string dir, FsoMethod method)
{
DataRow row;
DataTable table = new DataTable();
table.Columns.Add("name", typeof(string));
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));
if (method != FsoMethod.File)
{
for (int i = 0; i < getDirs(dir).Length; i++)
{
row = table.NewRow();
DirectoryInfo d = new DirectoryInfo(getDirs(dir)[i]);
long[] numArray = DirInfo(d);
row[0] = d.Name;
row[1] = 1;
row[2] = numArray[0];
row[3] = "";
row[4] = d.CreationTime;
row[5] = d.LastWriteTime;
table.Rows.Add(row);
}
}
if (method != FsoMethod.Folder)
{
for (int j = 0; j < getFiles(dir).Length; j++)
{
row = table.NewRow();
FileInfo info2 = new FileInfo(getFiles(dir)[j]);
row[0] = info2.Name;
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 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");
}
/// <summary>
/// 返回文件是否存在
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>是否存在</returns>
public static bool FileExists(string filename)
{
return File.Exists(filename);
}
/// <summary>
/// 返回目录是否存在
/// </summary>
/// <param name="filename">文件目录</param>
/// <returns>是否存在</returns>
public static bool DirIsExist(string file)
{
return Directory.Exists(file);
}
public static bool IsExistCategoryDirAndCreate(string categorDir, HttpContext context)
{
if (context != null)
{
string file = Path.Combine(context.Request.PhysicalApplicationPath, categorDir);
if (DirIsExist(file))
{
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();
}
}
}
}
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();
}
}
}
public enum FsoMethod
{
Folder,
File,
All
}
相关推荐
这类网站通常收集并分享各种软件、源码、教程等资源,对于开发者来说是一个寻找和学习新知识的好去处。 总结,"FSO文件浏览器 v1.0-ASP源码.zip"是一个用于Web文件管理的ASP应用程序,它利用FSO组件实现文件和目录...
例如,.NET Framework中的System.IO命名空间提供了更高效和功能丰富的文件操作类。 通过以上策略,可以显著改善在ASP中使用FileSystemObject处理大量文件时的性能。在实际应用中,应根据具体需求和环境选择最合适的...
根据提供的文件信息,...综上所述,“fso文件排序演示(fso)”脚本不仅展示了如何利用FSO进行文件操作,还通过简单的算法实现了实用的排序功能。虽然该示例较为简单,但在实际项目中,这类技术仍具有一定的参考价值。
WScript.Arguments是VBScript中的一个集合对象,它允许访问传递给脚本的命令行参数。在脚本中,可以通过WScript.Arguments(index)的方式来访问,其中index代表参数的索引位置,索引从0开始。 #### 4. WMI基础 WMI...
6. **交互界面**:`System.frm`可能设计了一个用户界面,用于显示收集到的系统信息,可能包括文本框、列表框或表格控件,用于展示CPU使用率、内存使用情况、硬盘空间等。 7. **运行与调试**:使用VB6的IDE,开发者...
【标题】"vb资料收集 空记事本"指的是一个基于Visual Basic(VB)开发的简单记事本程序,它被设计为一个空白的文本编辑器,允许用户收集、存储和管理各种源代码或文本资料。这个工具可能是为了方便程序员和IT爱好者...
FSO提供了对本地文件系统进行操作的方法,如创建、删除、复制、移动文件和目录,以及读取和写入文件内容。例如,以下VBScript代码用于创建一个新文件: ```vb Dim fso, file Set fso = CreateObject("Scripting....
这个"ASP源码—君子兰图霸(笑话版).zip"文件很可能包含了一个基于ASP技术构建的网站源代码,该网站可能是一个以笑话为主题的信息分享平台,可能包含了用户互动、笑话展示、分类搜索等功能。 ASP的核心概念包括...
这个"asp网上办公管理系统设计(源代码+论文)"的压缩包内容可能包含了一个完整的项目,包括系统的源代码和相关的设计论文,为学习者提供了一个实际的案例来理解和研究ASP技术在实际工作中的应用。 在ASP技术中,...
除此之外,VB还提供了丰富的标准库,如FileSystemObject用于文件操作,ADODB用于数据库访问,以及许多内置的函数和组件,如Math库中的数学函数,Date和Time相关的函数,以及字符串处理函数等。 在VB精华文摘中,...
- **权限问题**:由于涉及到文件删除和进程终止等敏感操作,执行上述脚本时可能需要管理员权限。 - **兼容性考虑**:虽然上述脚本适用于大多数Windows版本,但在某些特殊配置下可能会出现兼容性问题,请根据实际情况...
然后,代码通过FileSystemObject遍历指定路径下的所有文件和子文件夹,收集了所有需要打包的文件名,并将它们连接成一个字符串。 对于每个找到的文件,代码调用objTar.AddFile方法将其添加到打包文件中。这个objTar...
"我的毕生绝学——改变windows的小工具”这个压缩包显然包含了作者精心收集或制作的一系列用于优化和个性化Windows操作系统的实用程序。这些小工具通常体积小巧,功能却十分强大,能够帮助用户更加便捷地管理和调整...
这可能会使用到ASP内置的对象,如FileSystemObject (FSO),用于创建、读取、写入和删除文件。 3. ASP内置对象: - Request对象:负责收集客户端发送的数据,如表单提交的信息,这在许愿墙中用于获取用户输入的...
- **流式文件操作**:利用FileStream和StreamReader/StreamWriter类进行高级文件操作。 5. **VB的数据类型和数组** - **基本数据类型**:除了前面提到的Integer、Double、String,还有Boolean、Date等。 - **...