2014-01-30 40 views
0

我有这段代码有时会抛出IOException异常,在它的消息中说The file is used by another process对我来说根本没有任何意义。我真的很困惑。就我所知,一个目录不是一个需要为交叉线程操作处理的资源。Directory.GetDirectories()IOException

void CloudFolderWatcher_Created (object sender, FileSystemEventArgs e) 
{ 
    var foldersToCreate = Directory.GetDirectories(e.FullPath, "*", SearchOption.AllDirectories); 
    /// do something with foldersToCreate 
} 

这里有什么问题?我怎样才能克服这个问题?

+0

难道这与“NTFS”(符号链接或其他?)有关。解决方案可能不会一次性获得子文件夹,但使用递归处理大小写,当某个目录失败时使用'IOExcpetion',然后忽略它并继续。 – Sinatr

+0

我是从高层的顶层搜索SearchOption.AllDirectories,否则你会得到一个UnauthorizedAccessException –

回答

0

这意思是,一些其他进程(或者甚至是你的进程)上持有锁阻止您的进程枚举其内容的目录之一。

0

因为其余的工作必须有FileSystemEventArgs 后面的问题。(假设你使用WFA)

认沽(A)仅高于公共Form1的()

你会本身所有foldersToCreate变量下的目录。检查你的“E”的论点

(A)

public string Mypath; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    DialogResult result = folderBrowserDialog1.ShowDialog(); 
    if (result == DialogResult.OK) 
     Mypath = folderBrowserDialog1.SelectedPath; 
    if (Mypath != "") 
     CheckFOlder(); 
} 

private void CheckFOlder() 
{ 
    try 
    { 
     var foldersToCreate = Directory.GetDirectories(Mypath, "*", SearchOption.AllDirectories); 
    } 
    catch (IOException e) 
    { 
     MessageBox.Show(e.Message.ToString()); 
    } 
    catch (UnauthorizedAccessException e) 
    { 
     MessageBox.Show(e.Message.ToString()); 
    } 
} 

PS您必须在表单中添加folderBrowserDialog1