Hi,
My application needs to run the command prompt with admin privilage . ie with the same effect when we select start-> all program-->
accessories--> right click on command prompt --> run as administrator.

Here is the current code i have for using command prompt from c#

static void commandtorun(string commandexecuted)
        {

            string currentstatus;
            ProcessStartInfo startInfo = new ProcessStartInfo();
            Process myprocess = new Process();
            try
            {
                startInfo.FileName = "cmd"; //
                startInfo.RedirectStandardInput = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false; //'required to redirect
                startInfo.CreateNoWindow = true; // '<---- creates no window, obviously
                myprocess.StartInfo = startInfo; //
                myprocess.Start(); //
                System.IO.StreamReader SR;
                System.IO.StreamWriter SW;
                Thread.Sleep(200);
                SR = myprocess.StandardOutput;
                SW = myprocess.StandardInput;
                SW.WriteLine(commandexecuted); // 'the command you wish to run.....
                SW.WriteLine("exit"); // 'exits command prompt window
                Thread.Sleep(200);
                currentstatus = SR.ReadToEnd();

                SW.Close();
                SR.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
                Console.ReadLine();

            }


            //  throw new NotImplementedException();
        }

I think the way to do this is to use a manifest file as explained here.

I think this will still prompt the user to click the run as admin popup though.

In vista and win 7 so long as User Account Control is turned on you will be prompted, a much better approach would be to check if the current app is running as an admin and if not request that it is, then restart it, then check again, that way the program won't get any further until they user accepts making it run as admin.

Then just remember that any program started by a program with administrative privileges will have administrative privileges.

Here is the simplest way, the ultimate solution.

first add these namespaces

using Microsoft.Win32;
using System.Security.Principal;

then to the onload event of the first form add this

WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);

            if (hasAdministrativeRight)
            {

            }
            else
            {
                if (MessageBox.Show("This application requires admin privilages.\nClick Ok to elevate or Cancel to exit.", "Elevate?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    ProcessStartInfo processInfo = new ProcessStartInfo();
                    processInfo.Verb = "runas";
                    processInfo.FileName = Application.ExecutablePath;
                    try
                    {
                        Process.Start(processInfo);
                    }
                    catch (Win32Exception ex)
                    {
                        Application.Exit();
                    }

                    Application.Exit();

                }
                else
                {
                    Application.Exit();
                }
            }

The key here is the "runas" verb. this tells the system you want to run the current application as admin.

Now, assuming you just want to run a single child app as admin you just use a startinfo object using the verb "runas"

example as follows

ProcessStartInfo startInfo = new ProcessStartInfo()
            startInfo.FileName = @"pathgoeshere";
            startInfo.Verb = "runas";
            startInfo.Arguments = "anyargshere"; //feel free to lose this line

            Process p = new Process();
            p.StartInfo = startInfo;
            p.Start();

And that's all there is to it :)

commented: excellent +6

The key here is the "runas" verb. this tells the system you want to run the current application as admin.

Now, assuming you just want to run a single child app as admin you just use a startinfo object using the verb "runas"

example as follows

ProcessStartInfo startInfo = new ProcessStartInfo()
            startInfo.FileName = @"pathgoeshere";
            startInfo.Verb = "runas";
            startInfo.Arguments = "anyargshere"; //feel free to lose this line

            Process p = new Process();
            p.StartInfo = startInfo;
            p.Start();

And that's all there is to it :)

I tried below code

startInfo.FileName = "cmd"; //
                startInfo.RedirectStandardInput = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false; //'required to redirect
            //    startInfo.UseShellExecute = true;
              startInfo.Verb = "runas";
              startInfo.Arguments = "/env /user:" + "Administrator" + " cmd"; 

                startInfo.CreateNoWindow = true; // '<---- creates no window, obviously
                myprocess.StartInfo = startInfo; //
                myprocess.Start(); //
                System.IO.StreamReader SR;
                System.IO.StreamWriter SW;
                Thread.Sleep(200);
                SR = myprocess.StandardOutput;
                SW = myprocess.StandardInput;
                SW.WriteLine(commandexecuted); // 'the command you wish to run.....
                SW.WriteLine("exit"); // 'exits command prompt window
                Thread.Sleep(200);
                currentstatus = SR.ReadToEnd();

commandexecuted is route -p delete 0.0.0.0 mask 0.0.0.0
I am expecting currentstatus = ok, but got M

commandexecuted is route -p delete 0.0.0.0 mask 0.0.0.0
I am expecting currentstatus = ok, but got M

Not sure what you mean here.

Not sure what you mean here.

When you perform a route add / route delete command, you get a status message, like OK! . When i try to run as normal user, i am not getting the OK! messge

hrm, not sure how much more help I could be here, sorry.

hrm, not sure how much more help I could be here, sorry.

thanks for the help.. I am planning to throw an error if not able to access the routing table

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.