0

i'm writing setup code for setting file paths to exe's and some configs. I'm running a test just to make sure i'm doing this right, but getting a strange result.

I know for a fact that the file I'm searching for is at C:\ti\uniflash_3.4\uniflashCLI.bat

the following code correctly finds the file i want.

 var uniflashEXE = Directory.GetFiles(@"C:\ti", "uniflashCLI.bat", SearchOption.AllDirectories);

This is where the installer of the program puts it by default, but I won't be surprised if it gets installed into ProgramFiles instead by my users (I won't be overseeing the installation)

To account for that I changed the code to

 var uniflashEXE = Directory.GetFiles(@"C:\", "uniflashCLI.bat", SearchOption.AllDirectories);

but it doesn't work. I'm not getting any unauthorized user exceptions that should be thrown in the case of a UAC issue, and I've tried running Visual Studio (2015 community) as an Administrator to be sure.

This code is running in a form load (currently) and outputting the filepath to a messagebox the just displays the filepath. When running normally, this MessageBox pops up before the form loads and the form waits for me to press OK to load the form. When i change the directory to "C:" (or C:\ - it doesn't seem to matter) it just bypasses the entire process and just loads the form.

Whats going on here? It doesn't act like it's a permission issue and I know it's there, but when I ask it to search the entire directory it doesn't even try.

I get why searching the entire C directory would be a terrible idea, but i don't understand why it wouldn't do anything. More than likely I will try to identify likely installation spots and search through those directories and put a browse option for the edge cases that install in weird spots (though it scares me to do that, because these particular users are....well, they're not bright)

EDIT: adding entire code as requested

    private void frmInitialSetup_Load(object sender, EventArgs e)
    {
        try
        {
            var uniflashEXE = Directory.GetFiles(@"C\:", "uniflashCLI.bat", SearchOption.AllDirectories);
            MessageBox.Show(uniflashEXE[0].ToString());
        }
        catch (UnauthorizedAccessException UAex)
        {
            MessageBox.Show(UAex.ToString());
        }
        catch(DirectoryNotFoundException DNFex)
        {
            MessageBox.Show(DNFex.ToString());
        }

    }

EDIT2:

I placed a break point on both lines inside the try block.

When the code is set to search C:\ti and I stop it on the second line and hover over uniflashEXE it lists it as "uniflashEXE {string[1]}" and i can expand it to see that it contains the correct filepath. When it is set to search C:\ and do the same, it is listed as "uniflashEXE null".

I suppose passing a null value to a messagebox just makes it not show up and skips that whole section. It doesn't however, explain why it isn't searching through C:. Is it possible there's a maximum size that you can search through? The C:\ drive is 240 GB (ok, actually 223) while I only have 8 GB RAM.

David
  • 43
  • 7
  • If they're not bright, they probably won't change the installation directory, either... I'm not sure why setting C:\ doesn't do anything, it's probably because C:\ is not a directory, but a volume, but even so, I wouldn't bother finding a reason because doing that is absolutely ludicrous! – Bikonja Jun 24 '16 at 17:35
  • Hmm..are you sure it's not a permissions issue? I've just tried your code and it works, it's searching all directories – Pikoh Jun 24 '16 at 17:39
  • You've heard the term "smart enough to do a lot of damage"? That's these users. I intend to GTFO of this company as soon as this project ends and I want to leave them with as robust an app as possible. And even past that, I'm just too damn curious not want the answer – David Jun 24 '16 at 17:39
  • It lastly throws an `System.UnauthorizedAccessException` when trying to access `Program Files`, but your code is working – Pikoh Jun 24 '16 at 17:41
  • @Pikoh, like I said, no exceptions (MSDN says there should be an UnauthorizedAccessException or a DirectoryNotFoundException) that i can see. Also, I'm running as VB administrator on the only user account on my personal laptop, so I don't see a permission issue. Do you have any thoughts how else a permission issue could sneak in here (because I agree, it acts like a permission issue, but I can't find evidence of that) – David Jun 24 '16 at 17:42
  • Are you running Visual Studio as Administrator? There's another thing you could try:impersonate administrator to run that code and see if that way it works – Pikoh Jun 24 '16 at 17:44
  • `UnauthorizedAccessException` here too. Post your code. Isn't there some active `try` ... `catch` block? There might be `UnauthorizedAccessException` even if you're running as administrator. – Bozhidar Stoyneff Jun 24 '16 at 17:45
  • @pikoh, I am currently running VB as administrator but have had the same result either way. How would I go about impersonating administrator to do further testing – David Jun 24 '16 at 17:49
  • @BozhidarStoinev, I have updated the code in my post. I didn't originally have the try catch (knew it wasn't working by the incorrect behavior) but since investigating have added them and am still turning up nothing – David Jun 24 '16 at 17:53
  • David, to impersonate see [this answer](http://stackoverflow.com/questions/3003417/how-do-i-use-impersonation-on-a-c-sharp-winforms-application-to-run-with-admin-p) – Pikoh Jun 24 '16 at 18:12
  • Iterating the C:\ drive is a very hazardous operation. You'll for one inevitably run into the System Volume Information subdirectory, the home of restore points. Not even an admin has access to that directory. Hardly where it ends. And for another it completely sucks to iterate a terabyte drive. It is just not the right way to do it. Let your user tell you where the program is stored, trivially done with an Options menu entry, OpenFileDialog and a setting to preserve the selection. – Hans Passant Jun 24 '16 at 19:02
  • As an alternative, you could try [Windows Search](https://msdn.microsoft.com/en-us/library/windows/desktop/aa965362(v=vs.85).aspx).Here you've got an [example](http://www.thejoyofcode.com/Using_Windows_Search_in_your_applications.aspx) – Pikoh Jun 24 '16 at 19:06

0 Answers0