文件,文件夹基本操作

1:创建文件夹用到是DirectoryInfo类的Create()方法;

如:string name = txtName.text;

  string path = @"D:\Programe Files" + name;

  DirectoryInfo directoryInfo = new DirectoryInfo(path);

  if(directoryInfo.Exist)

      {
            MessageBox.Show("目录已存在当前文件夹");
            return;
      }
       else

  {
           directoryInfo.Create();

    MessageBox.Show("文件夹创建成功");

  }

注意:如果DirectoryInfo directoryInfo = new DirectoryInfo(path);

   改为DirectoryInfo directoryInfo = new DirectoryInfo(name);则创建在工程下的Debug目录下。

   还可以用directoryInfo.Delete()方法删除文件夹,directoryInfo.MoveTo()方法移动文件夹,directoryInfo.GetAllFiles()方法遍历文件夹中的所有文件。

2:创建文应用的是File类的Create()静态方法;

如:string name = txtName.text;

  string path = @"D:\Programe Files" + name;

  File.Create(path); 则在制定位置创建了名字为name的文件;

注意:可以调用File.Delete(path)删除文件,File.Copy(path,newPath)拷贝文件;

3:获取文件的基本信息是用FileInfo 类来映射文件;

如:FileInfo  fileInfo = new FileInfo(path);可以通过fileInfo来获取文件基本信息;

原文地址:https://www.cnblogs.com/PaulMa/p/1869774.html