0

I made an app that removes virus shortcuts using attrib command in CMD, and I need administrator rights so I can make the command.

Currently I'm not able to do that neither by whole program nor the Process itself. Here's a piece of code that needs UAC:

private void Button2_Click(object sender, EventArgs e)
{
    string Disktype = null;
    Disktype = ListBox1.GetItemText(ListBox1.SelectedItem); ;
    if (Disktype.Contains("C:") == true)
    {
        MessageBox.Show("You selected C:");
    }
    else
    {
        String Message = "Cleaning" + Disk.Diskvolume + "Are you Sure?";
        String Title = "Cleaning now";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result = MessageBox.Show(Message, Title, buttons);
        if (result == DialogResult.Yes)
        {
            String ShowFiles = "attrib -h -r -s /s /d" + Disktype + "*.*";
            String RemoveVirus = "del" + Disktype + "*.lnk";
            System.Diagnostics.Process.Start("CMD.exe", ShowFiles);
            System.Diagnostics.Process.Start("CMD.exe", RemoveVirus);
            MessageBox.Show("Cleaning");
            System.Diagnostics.Process.Start(Disktype);
            Close();
        }
        else
        {
            MessageBox.Show("Cleaning done");
            Close();
        }
    }

As you can see, I made ShowFiles and RemoveVirus directly to the cmd, but I'm having trouble as to how make them run as administrator.

Do I need it for the whole C# application or just the Process.Start ones?

dymanoid
  • 14,771
  • 4
  • 36
  • 64
Nxcc
  • 19
  • 1
  • 1
    You know you are supposed to use `System.IO.Path`, `System.IO.File` and `System.IO.Drive` for all this. No need to call a command prompt to do file operations. This seems like a monumentally bad idea. – John Alexiou Oct 05 '19 at 18:48
  • See https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically – ckuri Oct 05 '19 at 22:01
  • Taken literally, your question is answered by the marked duplicate. That said, I concur with the statement above that you are going about this all the wrong way. You should elevate your _own_ process, or even just run the program as elevated to start with (then you don't need to do anything programmatically), and then the `System.IO.File` class has all the functionality you need here. – Peter Duniho Oct 06 '19 at 00:31

1 Answers1

0

i was looking and you can try this:

string command;
command = "powershell -Command 'Start-Process cmd -Verb RunAs'"
System.Diagnostics.Process.Start("cmd.exe",command);

You can start CMD as Admin just using powershell.

Using this you can use the commands without problems.

I hope it works for you :)

Mume
  • 130
  • 6
  • That requires PowerShell to be installed. Also it’s overkill as you can just provide System.Diagnostics.Process.Start with the runas verb out-of-the-box, which is the same PowerShell does. Therefore there is no need to run a process just to start another process. – ckuri Oct 05 '19 at 22:04
  • No matter, you can install PowerShell in Linux or MacOS https://github.com/PowerShell/PowerShell/releases/ for MacOS download the **.pkg** file and for Linux, the **.deb** file and install it – Mume Oct 05 '19 at 22:30