When my application is using an app.manifest, and the line below is set as shown
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
the code
textBox1.Text = Path.Combine(Application.StartupPath, "setupws.exe");
returns "\\server\myApp\setupws.exe"
If I change the requestedExecutionLevel to "asInvoker", or remove the app.manifest altogether, it returns "F:\myApp\setupws.exe". Which is what I need.
Is there a way to get Application.StartupPath to behave the way I want and still use requireAdministrator?
I can’t think of any way to change the response you’re getting, but you can always try to remap back the UNC to local – to do this, you’ll need to call out to the underlying Win32 calls (C dll calls) using “Platform Invoke”, or “PInvoke”; the syntax for referring to a win32 call looks a bit like:
(What this is doing is mapping the unmanaged call/types to their managed equivalents, as well as telling the compiler “Hey, there’s code outside of this stuff that I’m going to call”)
WNetGetConnectionis a fairly ho-hum Windows function: give it a drive letter, and you’ll get back a UNC path, if it’s mapped to one. We’ll cycle through the “known drive letters”, feeding each one in turn to this call, and if we get any partial matches with the returned UNC name and the path we’re trying to “reverse map”, we’ll replace the UNC part of the path with the corresponding drive letter and we’re done: