文件目录操作(winfrom篇)

文件操作:

选择文件(返回选择路径):OpenFileDialog file = new OpenFileDialog();

             if(file.ShowDialog() == DialogResult.OK){string 路径 = file.filename}

选择文件夹(返回选择路径):FolerBrowserDialog folder = new FolderBrowserDialog();

            if(folder.ShowDialog() == DialogResult.OK){string 路径 = folder.SelectedPath}

读取文件:SearchText.LoginReader();

清空文件:if(File.exists(path)){file.delete(path)}

清空文件夹:if(Directory.exists(path)){

  foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件  
                }
                else
                {
                    DirectoryInfo d1 = new DirectoryInfo(d);
                    if (d1.GetFiles().Length != 0)
                    {
                        DeleteFolder(d1.FullName);////递归删除子文件夹
                    }
                    Directory.Delete(d);
                }
            }

}

posted @ 2015-08-26 16:08  谁动了我的面包  阅读(252)  评论(0编辑  收藏  举报