0

I want the C# code to open a program using a shortcut. The program requires admin privileges.

Whilst ensuring Visual Studio is running as administrator, I have tried:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "FN.lnk";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.Start();

This fails with the exception: 'The operation was canceled by the user'.

Replacing "FN.lnk" with "cmd.exe" runs a command prompt window with admin privileges...

I have also tried:

System.Diagnostics.Process.Start("CMD.exe", "/K FN.lnk");

which launches an elevated command prompt window, but still says "Access is denied"

If I run cmd as administrator and manually navigate to the folder to run FN.lnk, it works perfectly... which makes this problem even more confusing.

Namyts
  • 373
  • 1
  • 9
  • 1
    You need to create an `Application Manifest` in order for your program to run as administrator and hence be able to open programs as admin – Camilo Terevinto Oct 09 '17 at 00:43
  • @CamiloTerevinto Thanks for getting back so quickly. I saw this mentioned when I was searching for a solution earlier. I would have tried it out by following the instructions provided by [this link](https://stackoverflow.com/questions/6050478/how-do-i-create-edit-a-manifest-file), but my copy of visual studio doesn't have an option to create an Application Manifest File... Is it possible to create these manually? If so, how would you "_link_" it to the program? – Namyts Oct 09 '17 at 00:56
  • That's very weird. Which specific version of VS do you have? – Camilo Terevinto Oct 09 '17 at 00:57
  • [Community 2017 15.3.5](https://i.imgur.com/QlWQzTV.png). And [here](https://i.imgur.com/eTmN2Fy.png) is a quick merged screenshot of all the items I am able to add to a project – Namyts Oct 09 '17 at 01:04
  • See [here](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374191(v=vs.85).aspx). It should be easy to create it by hand – Camilo Terevinto Oct 09 '17 at 01:15
  • I achieved running another application as admin without any kind of manifest. – Unknown Oct 09 '17 at 01:20
  • That's what makes this so much stranger. I got the program to create an admin cmd window (by subbing in cmd.exe into the first bit of code), and then created an admin cmd window manually and ran fn.lnk. Tell me I'm not going crazy... [These should both be functionally identical?](https://i.imgur.com/SXYLK7U.png) – Namyts Oct 09 '17 at 01:59
  • 1
    Try `whoami /verbose` in both consoles, compare and contrast the output. – Harry Johnston Oct 09 '17 at 02:03
  • @HarryJohnston I tried whoami, and tested all the arguements shown by whoami /?. There were no differences at all. – Namyts Oct 09 '17 at 02:14
  • 1
    Sorry I meant `whoami /all` but I assume you found that? – Harry Johnston Oct 09 '17 at 02:15
  • 1
    OK, one obvious difference from the screenshot: the command shell that isn't working is 32-bit. Not sure *why* that would make any difference, mind you, but it's something you should check. – Harry Johnston Oct 09 '17 at 02:17
  • Yeah, it all seemed exactly the same (unless I'm expecting literally a single character/digit to change, which it's possible (but unlikely) I might have missed at 3am) – Namyts Oct 09 '17 at 02:20
  • UPDATE: I uninstalled Visual Studio and installed Visual Studio Code. Suddenly both methods to launch the shortcut worked perfectly... I'm not questioning this too deeply, but I'd guess that my copy of Visual Studio was just broken somehow. I also had a bug where I couldn't access a file more than once without the error "Can not access a disposed object", which supports this. It's a shame that there wasn't a direct solution for this problem, but I'd like to thank all of you for taking the time to answer :) – Namyts Oct 09 '17 at 14:56

0 Answers0