简体   繁体   中英

Directory.GetDirectories() IOException

I have this piece of code which sometimes throws IOException, in its message it says The file is used by another process which makes no sense to me at all. I am really confused here. A directory is not a resource that needs to be handled for cross thread operations or sth like that as far as I know.

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

What might be the problem here? How can I overcome this issue?

There must be a problem behind the FileSystemEventArgs Because the rest is working.(Supposing that you use WFA)

Put (A) just above the Public form1()

You will se all the directories under the foldersToCreate variable. Check your "e" arguments

(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 You must add in your form folderBrowserDialog1

这意味着其他某个进程(甚至您的进程)在其中一个目录上持有一个锁,从而阻止您的进程枚举其内容。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM