C#中关于地址的不同写法及区别

发布时间 2023-04-19 12:17:19作者: 马玲

1.根目录

string selectedPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\MaterialFiles";
             if (!File.Exists(selectedPath))
             {
                 System.IO.Directory.CreateDirectory(selectedPath);//不存在就创建目录

 2.删除文件夹内容

  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);//直接删除其中的文件  
                }

            }

 3.打开文件目录

 System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
                if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {}