1.遍历文件夹下的所有文件
/// <summary>
/// 获得文件夹下的所有文件名
/// </summary>
/// <param name="StuListPath">文件夹路径</param>
public static List<string> GetAllFileName(string StuListPath)
{
List<string> ListFileName = new List<string> { };
DirectoryInfo dir = new DirectoryInfo(StuListPath);
获得所有文件
FileInfo[] files = dir.GetFiles();
foreach(FileInfo file in files)
{
ListFileName.Add(file.Name);
}
return ListFileName;
}
2.创建文件夹
sPath="E:\\StudentMIS\\list";
Directory.CreateDirectory(sPath);
sPath="E:\\StudentMIS\\list\\1班学生名单.log";
StreamWriter streamWrite = null;
streamWrite = new StreamWriter(sPath, true);
streamWrite.WriteLine(“我就就是城环第一帅怎么了?”);
streamWrite.Close();
4.读取文本文档(txt)的所有行,并进行修改后保存
sFilePath="E:\\StudentMIS\\list\\1班学生名单.log";
string[] sAllLines = File.ReadAllLines(sFilePath);
int i = 0;
while (sAllLines[i] != null)
{
sStuName = sAllLines[i].Substring(0, sAllLines[i].LastIndexOf(","));
sStuCounts = sAllLines[i].Substring(sAllLines[i].LastIndexOf(",") + 1);
点名名单存在时,点到某个学生时,其点名次数+1
if (sStuName.Trim().Equals(“奚学长”))
{
iStuCounts = Convert.ToInt32(sStuCounts) + 1;
sAllLines[i] = sAllLines[i].Replace(sStuCounts, iStuCounts.ToString());
保存
System.IO.File.WriteAllLines(sFilePath, sAllLines);
break;
}
i++;
}
5.复制文件
sFilePath="E:\\StudentMIS\\list\\1班学生名单.log";
sNewFilePath="D:\\1班学生名单.log";
FileInfo file = new FileInfo(sFilePath);
file.CopyTo(sNewFilePath, true);