/****************************************
* 函数名称:DeleteFolder
* 功能说明:递归删除文件夹目录及文件
* 参 数:dir:文件夹路径
* 调用示列:
* string dir = Server.MapPath("test/");
* DotNet.Utilities.FileOperate.DeleteFolder(dir);
*****************************************/
///
/// 递归删除文件夹目录及文件
///
///
///
public static void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir, true); //删除已空文件夹
}
}
#region 获取指定文件详细属性
/****************************************
* 函数名称:GetFileAttibe(string filePath)
* 功能说明:获取指定文件详细属性
* 参 数:filePath:文件详细路径
* 调用示列:
* string file = Server.MapPath("robots.txt");
* Response.Write(DotNet.Utilities.FileOperate.GetFileAttibe(file));
*****************************************/
///
/// 获取指定文件详细属性
///
/// 文件详细路径
///
public static string GetFileAttibe(string filePath)
{
StringBuilder str = new StringBuilder();
System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);
str.AppendLine("详细路径:" + objFI.FullName);
str.AppendLine("文件名称:" + objFI.Name);
str.AppendLine("文件长度:" + objFI.Length.ToString()+"字节");
str.AppendLine("创建时间" + objFI.CreationTime.ToString());
str.AppendLine("最后访问时间:" + objFI.LastAccessTime.ToString());
str.AppendLine("修改时间:" + objFI.LastWriteTime.ToString());
str.AppendLine("所在目录:" + objFI.DirectoryName);
str.AppendLine("扩展名:" + objFI.Extension);
return str.ToString();
}
#endregion
#region 搜索指定目录含子目录,返回此文件的路径名
///
/// 搜索指定目录含子目录,返回此文件的路径名
/// 如果不存在,返回NULL
///
/// 指定路径(路径必须有访问权限)
/// 文件或文件夹名称
///
public string GetFileByNameURl(string dir, string ProName)
{
if (Directory.Exists(dir)) //如果路径正确
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
string FileName = d.Substring(d.LastIndexOf("\\") + 1);
if (ProName.ToLower().Equals(FileName.ToLower()))
{
return d;
}
string URl = GetFileByNameURl(d, ProName);
if (URl != null)
{
return URl;
}
}
}
return null;
}
#endregion
#region 读取文件所有内容
///
/// 读取文件内所有内容
///
///
///
public static string GetAllFileContent(string url)
{
if (string.IsNullOrEmpty(url)) throw new Exception("文件路径不能为空!");
using (var stream = File.OpenRead(url))
{
StreamReader StrRed = new StreamReader(stream);
var Content = StrRed.ReadToEnd();
StrRed.Close();
return Content;
}
}
#endregion
#region 逐行读取文件
///
/// 逐行读取文本文件
///
///
///
public static List<string> GetReadLine(string url)
{
if (string.IsNullOrEmpty(url)) throw new Exception("文件路径不能为空!");
using (var Stream = File.OpenRead(url))
{
StreamReader StrReader = new StreamReader(Stream);
string Rest = "";
List<string> Content = new List<string>();
while ((Rest = StrReader.ReadLine()) != null)
{
Content.Add(Rest);
}
return Content;
}
}
#endregion
#region 追加文件内容
///
/// 追加文件
///
/// 文件路径
/// 内容
public static void FileAdd(string Path, string strings)
{
StreamWriter sw = File.AppendText(Path);
sw.Write(strings);
sw.Flush();
sw.Close();
sw.Dispose();
}
#endregion
using (FileStream fileStream = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite))
{
BinaryReader bitRed = new BinaryReader(fileStream);
int fileLength = (int)fileStream.Length;
Byte[] image = new Byte[fileLength];
bitRed.Read(image, 0, fileLength); //按字节流读取
string savePath ="E:\\222.jpg";
FileStream fielwriter = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite);
using (BinaryWriter writer = new BinaryWriter(fielwriter))
{
writer.Write(image);
MessageBox.Show("文件写入成功!");
}
}
///
/// Returns file content in a form of base64 string
///
/// File stream
/// Base64 representation of the file
private string GetFileContent(Stream stream)
{
int streamLength = (int)stream.Length;
byte[] fileData = new byte[streamLength + 1];
stream.Read(fileData, 0, streamLength);
stream.Close();
return Convert.ToBase64String(fileData);
}
呼呼····就写到这里了。。