FileInfo fi = new FileInfo(filePath); //file info. check file time , or file size:long size = fi.Length / 1024;
//获取文件的名称
FileInfo fi = new FileInfo(filePath);
return fi.Name.Split('.')[0];
File.Exists(filePath); // 判断是否存在文件
File.Delete(filePath);// 删除文件
string content;
File.WriteAllText(filePath, content); //
File.AppendAllTex(filePath,content); //追加
File.Copy(sourceFilePath, destFilePath, true); // 复制
File.Copy(sourceFilePath, destFilePath);// 移动
FileInfo fi = new FileInfo(filePath); //创建文件,并写入一些东西
FileStream fs = fi.Create();
fs.Write(buffer,0,buffer.length);
fs.Close();
//等价操作:
FileStream fs = File.Create(filePath);
fs.Close();fs.Dispose();*
FileStream fs = fi.Open(FileMode.Open); //读出到字符串数组
fileSize = fi.Length;
fs.Read(buffer, 0, fileSize);
return buffer;
StreamReader reader = new StreamReader(filePath,encoding); //读出到字符串
return (string)reader.ReadToEnd();
public static bool WriteLine(string fileName, string content)
{
using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate | FileMode.Append))
{
lock (fs)
{
if (!fs.CanWrite)
{
throw new System.Security.SecurityException("文件fileName=" + fileName + "是只读文件不能写入!");
}
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(content);
sw.Dispose();
sw.Close();
return true;
}
}
}