Problem with recursive file search
I am trying to recursively search through all the files on my computer and for the life of me, I can't seem to get it to go without erroring/getting stuck in a loop. Basically, when the program accesses the C:\ drive, everything inside gives me an access denied, I've made sure that all my folders have permissions within Windows and whatnot, and I have tried setting permission controls and whatnot in my actual code but nothing is working. Please help me out thanks
At this point, I get no errors however my main form freezes and doesn't bring up my new form which is supposed to run the recursive search.
Code:
public List<DriveInfo> drives
{
get;
set;
}
public List<FileInfo> files
{
get;
set;
}
public static string thisFile
{
get;
set;
}
public static bool isThreadActive = false;
public Thread t;
public Search_Form(List<DriveInfo> drivesChosen)
{
InitializeComponent();
drives = drivesChosen;
if(files == null)
files = new List<FileInfo>();
else
files.Clear();
t = new Thread(new ThreadStart(recurseFiles));
t.Start();
isThreadActive = true;
while (isThreadActive)
Thread.Sleep(100);
t.Abort();
if (files.ToArray().Length > 0)
{
MessageBox.Show("Finished Searching with " + files.ToArray().Length + " bad files found");
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
else
this.DialogResult = System.Windows.Forms.DialogResult.Abort;
}
public void recurseFiles()
{
foreach (DriveInfo di in drives)
{
getFiles(di.RootDirectory, Properties.Settings.Default.Filters);
}
isThreadActive = false;
}
public void getFiles(DirectoryInfo currentPath, string filter)
{
string[] filterTypes = filter.Split(';');
FileIOPermission fiop = new FileIOPermission(new FileIOPermissionAccess(), currentPath.FullName );
fiop.AllFiles = FileIOPermissionAccess.AllAccess;
//// Get a DirectorySecurity object that represents the
//// current security settings.
//DirectorySecurity dSecurity = currentPath.GetAccessControl();
//// Add the FileSystemAccessRule to the security settings.
//dSecurity.AddAccessRule(
// new FileSystemAccessRule(
// new System.Security.Principal.SecurityIdentifier(
// System.Security.Principal.WellKnownSidType.BuiltinUsersSid,
// null
// ),
// FileSystemRights.DeleteSubdirectoriesAndFiles,
// //FileSystemRights.FullControl,
// AccessControlType.Allow
// )
//);
//// Set the new access settings.
//currentPath.SetAccessControl(dSecurity);
try
{
foreach (FileInfo curFile in currentPath.EnumerateFiles())
{
try
{
textBox1.Text = curFile.FullName;
foreach (string s in filterTypes)
{
if (curFile.Extension == s)
files.Add(curFile);
}
foreach (DirectoryInfo newDirs in currentPath.EnumerateDirectories())
{
FileAttributes attr = newDirs.Attributes;
if (attr.ToString().Contains("System") || attr.ToString().Contains("Hidden"))
continue;
getFiles(newDirs, filter);
}
//DirectoryInfo newDir = currentPath.EnumerateDirectories();
//getFiles(currentPath.EnumerateDirectories, filter);
}
catch (Exception e)
{
MessageBox.Show(e.StackTrace);
MessageBox.Show(e.Message);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
MessageBox.Show(e.StackTrace.ToString());
}
}
Re: Problem with recursive file search
First of all, if you want to wait for a thread to finish, use the Thread.Join method, instead of sleeping and waking up when it ends.
Also, if you are waiting for the thread to finish, it will block your UI. The user will not expect for the UI to be blocked until the search, which might take a lot of time, finished. Consider using a BackgroundWorker to fix this issue.
As to your original concern, you'll have to solve it yourself, or at least provide us with more relevant information.
It's possible that the search process does end, but just takes a lot of time. It is also possible that it's getting hanged somewhere.
In order to find out exactly what happens, write logs to the output window (using the Debug.WriteLine method), place breakpoints, and use the Debug->Break to find out where the code is at a given time.
Re: Problem with recursive file search
As Talikag says, using a separate thread to do processing but then putting the UI thread to sleep until it has finished defeats the whole purpose of using a separate thread. You could achieve the same effect by just doing the processing in the UI thread.
But, in any case, the while loop you are using to make the UI thread wait is an infinite loop. If you ran this code through the debugger you would notice this:
Code:
isThreadActive = true;
while (isThreadActive)
Thread.Sleep(100);
This loop will never end as you are not updating isThreadActive.