0

I wanted to have an application remove a targeted file from specified worksations. The user executing the software would be an admin on the target machine so I used the following code for a test:

string strTarget = @"\\" + textBox1.Text + @"\C$\Temp\temp.txt";

            try
            {
                File.Delete(strTarget);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failure to delete: " + ex.Message);
            }

I then created a \Temp\Temp.txt file on both my own workstation and another test machine. I am an admin on both machines and can manually access and delete the file through the UNC path in question. When I ran the code debugger no exceptions are thrown, but the file doesn't delete. I can't figure our what is not happening for this to fail.

Is there anything I can check or any code I need to add? I've done a search on other questions, but I havn't been able to find an answer yet.

ChargerIIC
  • 1,620
  • 1
  • 33
  • 47

1 Answers1

1

If you're running under Vista/7, your program is probably running under non-escalated priveleges. Make sure you either explicitly run your program as an administrator, or specify it in your project's manifest file:

  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <!--
      UAC Manifest Options
      If you want to change the Windows User Account Control level replace the 
      requestedExecutionLevel node with one of the following.

    <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
    <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
    <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

     If you want to utilize File and Registry Virtualization for backward 
     compatibility then delete the requestedExecutionLevel node.
-->
    <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
  </requestedPrivileges>
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • I ran the file as an administrator but there was no change. My AD account is an administrator on both machines. I've checked the event viewer, but don't see any credital events outside of my workstation login/log outs. Should I see events related to my using the app? – ChargerIIC Apr 30 '12 at 23:33
  • I tested a simple one-line program `File.Delete(@"\\foo\c$\temp\temp.txt");` (where foo is replaced by my machine name) and it deleted a c:\temp\temp.txt file I created without any problem. Are you sure your textbox input is correct? According to Microsoft, File.Delete() does not throw an exception if the file is not found. – itsme86 Apr 30 '12 at 23:48
  • After some more testing it seems that using the target name as lower case returns an 'access denied' error for the network path. My user account has admin rights to the path so I'm guessing I need to figure out how to pass those rights to the program, correct? – ChargerIIC May 01 '12 at 00:01
  • And for reference this is a windows 7 machine on a domain – ChargerIIC May 01 '12 at 00:05
  • Well, you can always just explicitly "run as administrator" to test if it's a rights issue. – itsme86 May 01 '12 at 03:08
  • I have and it's still failing. I'm now seeing if explicitly passing the currently used domain creditials in the code will fix the issue. Still you definitely found the bug I was dealing with. Thanks! – ChargerIIC May 01 '12 at 04:09