C#删除目录我正在使用.NETCompactFramework3.5,想删除一些特定的文件夹及其子文件夹。当我运行应用程序时,它给出了IO异常。我尝试使用Directory.Delete(path)方法,但没有用。我怎么解决这个问题?这是我的代码:usingSystem;使用系统诊断;使用System.IO;使用System.Linq;使用System.Collections.Generic;使用System.Windows.Forms;namespaceReset_Client{staticclassProgram{staticvoidMain(){myfunc();MessageBox.Show("Cihazresetlendi!");}publicstaticvoidmyfunc(){stringmainPath=@"StorageCarddeneme";试试{DeleteDirectory(mainPath+"CRM");DeleteDirectory(mainPath+"BHTS");DeleteDirectory(mainPath+"IMAGES");DeleteDirectory(mainPath+"STYLES");DeleteDirectory(mainPath+"TABLES");DeleteDirectory(mainPath+"LOG");File.Delete(mainPath+"Agentry.ini");File.Delete(mainPath+"Agentry.app");File.Delete(mainPath+"Agentry.usr");}catch(IOExceptione){myfunc();}}publicstaticvoidDeleteDirectory(stringtarget_dir){FileInfofileInfo=newFileInfo(target_dir);FileAttributes属性=fileInfo.Attributes;如果((属性&FileAttributes.ReadOnly)==FileAttributes.ReadOnly){//将属性设置为非只读fileInfo.Attributes&=~FileAttributes.ReadOnly;}string[]files=Directory.GetFiles(target_dir);string[]dirs=Directory.GetDirectories(target_dir);foreach(文件中的字符串文件){File.Delete(文件);}foreach(stringdirindirs){DeleteDirectory(dir);}Directory.Delete(target_dir,false);为什么不递归删除目录:Directory.Delete(path,true);seeGohereAlso,takealookhere因为它可能与您遇到的类似。试试这个..vardir=newDirectoryInfo(@FolderPath);dir.Delete(真);你不知道你得到了什么样的IOexception,你是否错过了路径中的反斜杠()?以上是C#学习教程:C#删除目录共享的全部内容。如果对大家有用,需要详细了解C#学习教程,希望大家多多关注——mainPath+"CRM"变成"StorageCarddenemeCRM"而不是"StorageCarddenemeCRM"本文收集自网络,非原创代表一个位置。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
