2

I wrote a program to get rid of unexpected errors like NULLs or zeros in .xml file after crash but the "restart" part of code isn't working. It's all good when I run the code in Visual Studio Code but when I use .exe file from dotnet publish function the program just crashes.

I've already tried setting UAC at level 0, UseShellExecute true/false, System.Diagnostics.Process.Start();, running as administrator.

static string exeAdress = @"C:\Program Files (x86)\NaturalPoint\SmartNav\SmartNAV.exe";

// Process.Start(exeAdress); // this isn't working either

Process p = new Process();
p.StartInfo.FileName = exeAdress;
p.StartInfo.UserName = "User";
p.StartInfo.Domain = "Domain";
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = false;

Actual output is throwing exception but I expect to run the exe without errors:

Unhandled Exception: System.ComponentModel.Win32Exception: The requested operation requires elevation at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • run your main application as Administrator – Derviş Kayımbaşıoğlu Jan 02 '19 at 21:53
  • Please copy/paste error as text, not as a screenshot. – abatishchev Jan 02 '19 at 21:55
  • It could be [file system redirection](https://learn.microsoft.com/en-us/windows/desktop/winprog64/file-system-redirector) messing with your Program Files path... but I don't really think so because of the "requires elevation" part of the exception message. That likely means you need to run your program as administrator. – Joel Coehoorn Jan 02 '19 at 22:27

1 Answers1

1

You need to run your main application as administrator (with elevated permission).

If you can run your application with elevated user then you do not need to supply

p.StartInfo.UserName = "User";
p.StartInfo.Domain = "Domain";

parameters.

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • I'm a bit confused, the main application you mean adding the info.Verb = "runas";? – Kacper Nowicki Jan 02 '19 at 22:14
  • don't add verb runas. just execute your main program as administrator so that your application has elevation to start another process as administrator – Derviş Kayımbaşıoğlu Jan 02 '19 at 22:19
  • I don't know why but your solution works perfectly after building it in Visual Studio 2017 but after building in VS code I don't get errors but the process doesn't start. Anyway thanks – Kacper Nowicki Jan 03 '19 at 11:02