Run command as Admin C #

1

I'll get right to the point! I have a C # application and need to run a command in Cmd with Admin privilege. I need to enable SQL Server if it is stopped.

public List<Atendimento> Pesquisar()
    {
        try
        {
            using (CasaVipEntities objEntity = new CasaVipEntities())
            {
                var result = objEntity.Atendimento.GroupBy(c => c.Cliente).ToList();
                return result.SelectMany(x => x).ToList();

            }
        }
        catch (System.Data.Entity.Core.EntityException) 
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

            Process myProcess = new Process();

            myProcess.StartInfo.FileName = "cmd.exe";
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.RedirectStandardInput = true;

            myProcess.Start();

            StreamWriter myStreamWriter = myProcess.StandardInput;
            myStreamWriter.WriteLine("net start MSSQL$SQLEXPRESS");
            myStreamWriter.Close();
            myProcess.WaitForExit();
            myProcess.Close();

        }

I do not know why you are not activating SQL Server. If I open the cmd normally and enter the command it activates, but skin application does not. Thank you in advance!

    
asked by anonymous 04.06.2018 / 02:39

1 answer

0

Try to put the following command:

myProcess.StartInfo.Verb = "runas";
    
04.06.2018 / 02:50