Domanda

I asked a question yesterday, and recieved great help (especially from @AviTurner).

I have further developed the program I was working on yesterday, and I have encountered a new problem.

The code of my program can be found here.

Basicly what it does, is:

The user can select a path of a directory, and the program scans all files for read-only attribute.

It sets the read-only attribute on those files that does not currently have it.

Now the problem occurs, when it encounters a file that is currently in use (such as system files).

I have been told there is no way around this, but I thought:

Is there a way to ignore the error (by this I mean continue the program, just skip this file); and add the name of the file to a list for later tracking purposes?

I hope I made my problem clear.

Thanks.

È stato utile?

Soluzione

try surrounding your code in try/catch:

try
{
    System.IO.FileAttributes attr = System.IO.File.GetAttributes(file);
}
catch(Exception ex)
{
    files.add(file)
}

basically if you get an exception in the try block, the program executes the catch block

Altri suggerimenti

I suggest...

try
{
    System.IO.File.SetAttributes(file, attr);
}
catch // You can specify a specific error with catch(UnauthorizedAccessException ex) for instance.
{
    filesInError.Add(file); // A list<string>() to keep track of errors.
}

Here the details, and exceptions raised, by the SetAttributes(). SetAttributes on MSDN

And some explanations about try catch if you're not familiar with. try ... catch on MSDN

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top