R
R
Rem_u2017-10-05 19:08:49
Programming
Rem_u, 2017-10-05 19:08:49

How to run an external program (for example, the steam client) in C#?

I am writing a program. I stopped at ignorance of how to run a separate program on behalf of the admin, I tried to find it on the Internet, but alas I didn’t find it, or I found it, but the code got bugged, or the launch of the program itself written in C # on behalf of the admin.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2017-10-05
@Rem_u

Simple option:

private void button1_Click(object sender, EventArgs e)
{
   ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
   info.UseShellExecute = true;
   info.Verb = "runas";
   Process.Start(info);
}

Variant with possible errors:
private void button1_Click(object sender, EventArgs e)
{
   const int ERROR_CANCELLED = 1223; //The operation was canceled by the user.

   ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
   info.UseShellExecute = true;
   info.Verb = "runas";
   try
   {
      Process.Start(info);
   }
   catch (Win32Exception ex)
   {
      if (ex.NativeErrorCode == ERROR_CANCELLED)
         MessageBox.Show("Why you no select Yes?");
      else
         throw;
   }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question