C#中的文件读写
一、递归删除文件夹目录和文件
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);
}
}
2、获取指定文件的详细属性
#region 获取指定文件详细属性
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 搜索指定目录含子目录,返回此文件的路径名
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(
"文件写入成功!");
}
}
七、一封装方法,
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);
}
呼呼····就写到这里了。。
转载请注明原文地址: https://ju.6miu.com/read-969732.html