Windows Form app in Adminstrator mode - c#

I have windows form application which needs to be run in administrator mode with out editing the app.manifest file. Got the below codebut don't know where to place the code in windows form application.
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas";
Process.Start(proc);
Application.Exit(); // Quit itself

Put this code inside you main form loading event
private void Form1_Load(object sender, EventArgs e)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas";
Process.Start(proc);
Application.Exit(); // Quit itself
}

Related

Opening a certain page on a PDF file in WPF

I Need to open a specific page of a pdf file.
I tried:
private void Button_Click_20(object sender, RoutedEventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.Arguments = "/A \"page=5\"";
startInfo.FileName = #"J:temp.pdf";
process.Start();
}
but it still opens the first page. Still unsolved.
if i Change to this
private void Button_Click_20(object sender, RoutedEventArgs e)
{
{
Process process = new Process();
process.StartInfo.Arguments = #"/A \"page=5\" \"J:\\temp.pdf"";
process.StartInfo.FileName = #"J:\temp.pdf";
process.Start();
}
}
i get seven Errors (Semicolon, page no context...)
It's unclear on how the arguments need to look like. Assuming it's J:\temp.pdf /A page=5
this should work:
Process process = new Process();
process.StartInfo.Arguments = #"/A page=5";
process.StartInfo.FileName = #"J:\temp.pdf";
process.Start();
However I'm not sure if you can pass arguments to a file name like this, I'd assume you need to call your PDF viewer's executable and pass both the file name and the page argument like in the question already linked in the comments:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
startInfo.Arguments = "/A \"page=5\" \"E:\\Users\\You\\temp.pdf\"";
Process.Start(startInfo);
This works on my machine (replace paths as needed of course).

c# start console application in win forms

I'm trying to make launcher for my games and I like to add music player in background, but if I start the process it instantly fail's.
Code
private void btnStartMusic_Click(object sender, EventArgs e)
{
ProcessStartInfo proc = new
ProcessStartInfo("MusicPlayer\\MemequickieMusicPlayer.exe");
proc.CreateNoWindow = true;
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.RedirectStandardError = false;
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = false;
proc.UseShellExecute = false;
Process.Start(proc);
}
Any Help is appreciated.
Try using the full path to the exe and setting the working directory; assuming the exe is in your executable folder:
string path = Path.Combine(Application.StartupPath, "MusicPlayer");
ProcessStartInfo proc = new
ProcessStartInfo(Path.Combine(path, "MemequickieMusicPlayer.exe"));
proc.WorkingDirectory = path;
If the error persists and you want to debug the output, change:
proc.RedirectStandardOutput = true;
Create the process like this:
Process process = new Process(proc);
process.Start();
while (!process.StandardOutput.EndOfStream) {
System.Diagnostics.Debug.Write(process.StandardOutput.ReadLine());
}
You should now see the output in your output window.

How to get the cmd command output in c# to a lable

I wrote a code for run cmd commands from c# form application. Now I want to get the output of the cmd to a lable in winform. I wrote a code. but it is giving me following error
Screenshot of the Error
An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: StandardOut has not been redirected or the process hasn't started yet.
how to fix it? this is my original code.
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startiNFO = new System.Diagnostics.ProcessStartInfo();
startiNFO.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startiNFO.FileName = "cmd.exe";
startiNFO.Arguments = "/C ipconfig";
startiNFO.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo = startiNFO;
process.Start();
string outp = process.StandardOutput.ReadToEnd();
process.WaitForExit();
MessageBox.Show(outp);
}
instead of process.WaitForExit(); do something like this
The complete code for your function would look something like this
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C ipconfig";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
string q = "";
while(!process.HasExited)
{
q += process.StandardOutput.ReadToEnd();
}
label1.text = q;
MessageBox.Show(q);
}

Windows auto activate method

I need a windows activate method. My code works, but it create a popup window and I don't want it.
Is there any way to activate in background without any message?
private void tryingActivateWindows()
{
ProcessStartInfo psi = new ProcessStartInfo("cmd", "/c " + "SLMGR -ato");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit();
}
private void tryingActivateWindows()
{
Process activateScript = new Process();
activateScript.StartInfo.FileName = #"cscript";
activateScript.StartInfo.WorkingDirectory = #"C:\Windows\System32\";
activateScript.StartInfo.Arguments = "//B //Nologo slmgr.vbs -ato";
activateScript.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
activateScript.Start();
activateScript.WaitForExit();
}
Run "cscript C:\Windows\System32\slmgr.vbs /ato"
It will prevent the pop-up.

How to hide the Command Window using c#

Building a console app that will execute an exe file(pagesnap.exe). I would like to hide its window(pagesnap.exe) during execution. How is it done.
ProcessStartInfo Psi = new ProcessStartInfo("D:\\PsTools\\");
Psi.FileName = "D:\\PsTools\\psexec.exe";
Psi.Arguments = #"/C \\DESK123 D:\files\pagesnap.exe";
Psi.UseShellExecute = false;
Psi.RedirectStandardOutput = true;
Psi.RedirectStandardInput = true;
Psi.WindowStyle = ProcessWindowStyle.Hidden;
Psi.CreateNoWindow = true;
Process.Start(Psi).WaitForExit();
DESK123 is the local PC. Would later try this with remote PCs.
Things I have tried
Psi.Arguments = #"/C start /b psexec \\DESK123 D:\files\pagesnap.exe";
Psi.Arguments = #"/b psexec \\DESK123 D:\files\pagesnap.exe";
Psi.Arguments = #"/C psexec \\DESK123 /b D:\files\pagesnap.exe";
Psi.Arguments = #"/C psexec \\DESK123 D:\files\pagesnap.exe 2>&1 output.log";
Update: I have built pagesnap with Output type as a windows application instead of console. The cmd window doesn't come up, now. Seems this is the only way for me
Simply call the following function. Pass the argument as your command and your working directory
private string BatchCommand(string cmd, string mapD)
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd);
procStartInfo.WorkingDirectory = mapD;
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process cmdProcess = new System.Diagnostics.Process();
cmdProcess.StartInfo = procStartInfo;
cmdProcess.ErrorDataReceived += cmd_Error;
cmdProcess.OutputDataReceived += cmd_DataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine("ping www.google.com"); //Execute ping
cmdProcess.StandardInput.WriteLine("exit"); //Execute exit.
cmdProcess.WaitForExit();
// Get the output into a string
return Batchresults;
}
static void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Batchresults += Environment.NewLine + e.Data.ToString();
}
void cmd_Error(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
Batchresults += Environment.NewLine + e.Data.ToString();
}
}

Categories

Resources