Before you downvote the question, you need to know that I have spent some time on this and I am somewhat confused. I have looked through several answers but almost all of them have comments saying this is a good solution or will not work in a lot of cases.
The problem, ultimately, is that the program closes after excepting the error.
Examples below.
C# Test if user has write access to a folder
Some Code:
public static void CcnDirSearch(string sDir) // This is the directory I am passing c:\\ to here
{
try
{
foreach (string file in Directory.EnumerateFiles(sDir, "*.*", SearchOption.AllDirectories)) // File is supposed to hold the individual file...
{
bool isSystem = ((File.GetAttributes(file) & FileAttributes.System) == FileAttributes.System);
if (HasFolderWritePermission(file) == true && isSystem == false && file != @"c:\$Recycle.Bin\S-1-5-18")
{
Console.WriteLine(file);
using (var stream = File.OpenRead(file))
{
// I am checking file here with my own functions.
}
}
}
}
catch (UnauthorizedAccessException ex)
{
//User cannot access directory
Console.WriteLine("I AM NOT CONTINUING " + ex.Message);
}
catch (System.Exception excpt)
{
// Console.WriteLine("I AM AN ERROR!!!\n");
Console.WriteLine(excpt.Message);
}
}
The Error I am receiving is
I AM NOT CONTINUING Access to the path 'c:\$Recycle.Bin\S-1-5-18' is denied.
Then my program exists.
And yes I have looked this up and all of the examples thus far seem to not cover this completely.