C#文件操作

本文将逐步收录工作中遇到的文件方面的操作方法。

首先需要使用以下命名空间:
    using System.IO;
然后要获得一个文件路径,如果在配置文件中使用了文件路径,就这样写:
    string WebPath,tmpPath;
    WebPath = ConfigurationManager.AppSettings["Path"];
    tmpPath = WebPath + "Temp\\";//拼装路径
    FileInfo NewFile

    //遍历目录中的文件
    DirectoryInfo tmpdir = new DirectoryInfo(tmpPath);//取得目录对象
    int i;
    string tmp;
    foreach (FileInfo tmpfile in tmpdir.GetFiles())
    {
         tmp=tmpfile.Name;//取得文件名
    }
    

你可能感兴趣的:(文件操作)