Skip to content

Commit

Permalink
2 ways to run cmd instead of 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sanik0300 committed Sep 6, 2023
1 parent 27b4b32 commit 12dab8b
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions OpenWithCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,33 @@ protected override ContextMenuStrip CreateMenu()
ContextMenuStrip menuStrip = new ContextMenuStrip();

ToolStripMenuItem menuItem = new ToolStripMenuItem() { Text = "Open in Command prompt" };
menuItem.Click += OnClicked;

ToolStripMenuItem runAsUserItem = new ToolStripMenuItem() { Text = "As Current User" },
runAsAdmin = new ToolStripMenuItem() { Text = "As Administrator" };

runAsUserItem.Click += (s, e) => { RunCMD(false); };
runAsAdmin.Click += (s, e) => { RunCMD(true); };

menuItem.DropDownItems.AddRange(new ToolStripItem[] {runAsUserItem, runAsAdmin});

menuStrip.Items.Add(menuItem);
return menuStrip;
}

private void OnClicked(object sender, EventArgs e)
private void RunCMD(bool asadmin)
{
Process cmd = new Process();

cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WorkingDirectory = FolderPath;

if(asadmin)
{
cmd.StartInfo.UseShellExecute = true;
cmd.StartInfo.Arguments = $"/k cd /d \"{FolderPath}\"";
cmd.StartInfo.Verb = "runas";
}
else {
cmd.StartInfo.WorkingDirectory = FolderPath;
}

cmd.Start();
}
Expand Down

0 comments on commit 12dab8b

Please sign in to comment.