|
-
April 6th, 2012, 07:23 AM
#1
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());
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|