Pergunta

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?

Foi útil?

Solução

What this means is that some other process (or even your process) holds a lock on one of the directories that prevents your process from enumerating its contents.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top