I have found this via stack-overflow search but no one has given a solution that works. I am writing a simple program and the first part of it is to launch sysprep.exe with some arguments. For some reason sysprep does not launch when code is run. It gives an error that file cannot be found. For e.g. by using the code below Notepad will open with no issues. If I try and open sysprep it will not.
Process.Start(#"C:\Windows\System32\notepad.exe"); -- opens with no issue
Process.Start(#"C:\Windows\System32\sysprep\sysprep.exe"); -- does not open
Any help would be appreciated.
{
public MainWindow()
{
InitializeComponent();
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (radioButtonYes.IsChecked == true)
{
Process.Start(#"C:\Windows\System32\sysprep\sysprep.exe");
}
}
I see that another answer has worked for you, but I would like to include a different answer that will allow you to access files from System32 at any time. If you start with a public class to modify the kernel momentarily you should be able to access anything you need so long as you have the right permissions.
public class Wow64Interop
{
[DllImport("Kernel32.Dll", EntryPoint = "Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);
}
After this the way that i wrote out my call to sysprep was as follows
private void RunSysprep()
{
try
{
if (Wow64Interop.EnableWow64FSRedirection(true) == true)
{
Wow64Interop.EnableWow64FSRedirection(false);
}
Process Sysprep = new Process();
Sysprep.StartInfo.FileName = "C:\\Windows\\System32\\Sysprep\\sysprep.exe";
Sysprep.StartInfo.Arguments = "/generalize /oobe /shutdown /unattend:\"C:\\Windows\\System32\\Sysprep\\unattend.xml\"";
Sysprep.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
Sysprep.Start();
if (Wow64Interop.EnableWow64FSRedirection(false) == true)
{
Wow64Interop.EnableWow64FSRedirection(true);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
when doing something like this you want to make sure that if the process will restart your pc to NOT use the "WaitForExit()" method. Hope this helps anyone else looking for this answer.
It is actually a redirection problem on 64 bit Windows.
According to this discussion,
the System32 calls are redirected to the SysWOW64 folder.
And since C:\Windows\SysWOW64\Sysprep\sysprep.exe does not exist, you get the error.
This is what you want:
Process p = Process.Start(#"C:\Windows\sysnative\Sysprep\sysprep.exe");
Simply use sysnative instead.
I think this is a permission problem, you can try run as admin
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName ="cmd.exe";
startInfo.Arguments = #"/c C:\Windows\System32\sysprep\sysprep.exe";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
Related
What I intend to do is build an application which, among other things, will have a command line embedded in it just like some IDEs do (something I find extremely useful).
This is the code that I have so far, do note that it's a Winforms project:
public partial class Form1 : Form
{
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
p.StartInfo = info;
p.Start();
}
private void button1_Click(object sender, EventArgs e) {
using(StreamWriter sw = p.StandardInput) {
if(sw.BaseStream.CanWrite) {
sw.WriteLine(textBox1.Text);
}
}
textBox2.Text = p.StandardOutput.ReadToEnd();
textBox3.Text = p.StandardError.ReadToEnd();
p.WaitForExit();
}
}
}
As you can see there are 3 textboxes and one button:
textbox1 is for entering the command
textbox2 is for stdout
textbox3 is for stderr
On to my problem:
I can only input one command because after executing it, my CMD window vanishes. I know it dies off because I've set info.CreateNoWindow = false; and it indeed vanishes and if I try to enter another command I get an exception.
How would I go on about keeping my CMD window 'alive' so that I can use it as much as I please? In short I want to truly mimic CMD behavior.
Feel free to ask for more information if something is not clear.
Extra info/What I tried:
I've tried adding info.Attributes = "/K"; since I know that /K should keep the CMD alive. I've also read that p.WaitForExit(); should keep the CMD alive, but from what I figured this is only for the purpose of reading the output. Needless to say, I do not need that since I'm already redirecting its output. Neither of these solutions work but it is entirely possible that I'm using them the wrong way.
I need that process alive so I can easily navigate using cd and executing a sequence of commands when needed, such as when accessing ftp or mysql. I know I can work around these two examples with parameters, but not for every application. In short, spawning a new process every time is not something I want. I want that CMD interface to be up at all times.
The cmd process dies after
using(StreamWriter sw = p.StandardInput) {
if(sw.BaseStream.CanWrite) {
sw.WriteLine(textBox1.Text);
}
}
But I cannot pinpoint why.
What CMD console provides is an interface to execute predefined functions (in System32 or in %PATH%). Process class also have same capabilities ,what you can do is as the user enters command text and presses return key in textbox2 (which can be multi-lined, black-background, white text) you can pass the command text to Process p = new Process();and append the result so it looks like single cmd session. Now before passing the whole command text we need to separate arguments (if any) which is text appearing after first space. Example:
SHUTDOWN /S /T 10
where Shutdown will be filename and /S /T 10 will be arguments.
Before executing set default directory of ProcessStartInfo:-
_processStartInfo.WorkingDirectory = #"%Path%";
Otherwise default will be System32 folder.
thanks in advance for your time.
I'm working on an application to automate some of the things that I have to check each day.
One of them is making sure that all of our daily Robocopies took place successfully.
I'm having issues figuring out the best way to communicate this to my program. I've looked into the various Exit codes that Robocopy uses which would be very useful, but is there anyway to output this to a file, and would there be anyway to know which backup it was associated with?
Sorry if this is vague, please let me know of any other info that could be of use.
Thanks,
Will
If you start the robocopy process from your application you could check the exit code from there.
Process myProcess = null;
// Start the process.
myProcess = Process.Start("robocopy ....");
myProcess.WaitForExit(1000);
Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode(v=vs.110).aspx
You could write the Robocopy output to a file and read that in, like
robocopy c:\Test\ c:\temp /LOG+:myLogFile
Then experiment with the log options to get a useful verbosity ( see http://ss64.com/nt/robocopy.html )
You could do something like this:
public static void Main()
{
// Prepare the process.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "robocopy.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "WHATEVER YOU NEED"
// Start process and wait for it to end
Process exeProcess = Process.Start(startInfo)
exeProcess.WaitForExit();
// Display what the exit code was
Console.WriteLine();
Console.WriteLine("Process exit code: {0}", exeProcess.ExitCode);
}
Create a Robocopy-process like this:
string command = $"Robocopy {sourcePath} {targetPath} /MIR /w:1 /r:10";
var copyTask = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "CMD.exe"
Arguments = $"/c {command}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
Use it like this:
copyTask.Start()
string Output = copyTask.StandardOutput.ReadToEnd();
copyTask.WaitForExit();
Now you have the whole log in the "string output". This is useful if you want to find out WHAT went wrong.
From an application that is not being run as administrator, I have the following code:
ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas";
When I call Process.Start(proc), I do not get a pop up asking for permission to run as administrator, and the exe is not run as administrator.
I tried adding an app.manifest to the executable found at myExePath, and updated the requestedExecutionLevel to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
With the updated app.manifest, on the Process.Start(proc) call, I get an exception, "The requested operation requires elevation."
Why isn't the .Verb action not setting administrator privileges?
I am testing on Windows Server 2008 R2 Standard.
You must use ShellExecute. ShellExecute is the only API that knows how to launch Consent.exe in order to elevate.
Sample (.NET) Source Code
In C#, the way you call ShellExecute is to use Process.Start along with UseShellExecute = true:
private void button1_Click(object sender, EventArgs e)
{
//Public domain; no attribution required.
ProcessStartInfo info = new ProcessStartInfo(#"C:\Windows\Notepad.exe");
info.UseShellExecute = true;
info.Verb = "runas";
Process.Start(info);
}
If you want to be a good developer, you can catch when the user clicked No:
private void button1_Click(object sender, EventArgs e)
{
//Public domain; no attribution required.
const int ERROR_CANCELLED = 1223; //The operation was canceled by the user.
ProcessStartInfo info = new ProcessStartInfo(#"C:\Windows\Notepad.exe");
info.UseShellExecute = true;
info.Verb = "runas";
try
{
Process.Start(info);
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode == ERROR_CANCELLED)
MessageBox.Show("Why you no select Yes?");
else
throw;
}
}
Bonus Watching
UAC - What. How. Why.. The architecture of UAC, explaining that CreateProcess cannot do elevation, only create a process. ShellExecute is the one who knows how to launch Consent.exe, and Consent.exe is the one who checks group policy options.
I have been trying to start an application from a C# application but it fails to start properly. From the cmd the application plus the arguments launch a small window showing the output then the application in minimized to the system tray.
Launching the application from the C# application using the code below results in the process appearing in the task manager but nothing else, no output window, no system tray icon. What could be the issue?
myProcess.StartInfo.FileName = ...;
myProcess.StartInfo.Arguments = ...;
myProcess.Start();
also tried passing the following
myProcess.StartInfo.RedirectStandardOutput = true; //tried both
myProcess.StartInfo.UseShellExecute = false; //tried both
myProcess.StartInfo.CreateNoWindow = false;
using
Process.Start(Filename, args)
also did not work. Would really appreciate any help on how to tackle this.
UPDATE:
I think the issue maybe the multiple arguments that are to be passed to the process
RunMode=Server;CompanyDataBase=dbname;UserName=user;PassWord=passwd;DbUserName=dbu;Server=localhost;LanguageCode=9
regards
I don't see any mistake in your code. I have written a little program that prints out its args to the console
static void Main (string[] args)
{
foreach (string s in args)
Console.WriteLine(s);
Console.Read(); // Just to see the output
}
and then I have put it in C:, being the name of the app "PrintingArgs.exe", so I have written another one that executes the first:
Process p = new Process();
p.StartInfo.FileName = "C:\\PrintingArgs.exe";
p.StartInfo.Arguments = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18";
p.Start();
this gives me the desired output of the list of numbers. The app that calls PrintingArgs exits as it reachs p.Start(), this could be avoided by using p.WaitForExit(); or just Console.Read();.
Also I have used both p.UseShellExecute and p.CreateNoWindow. Only in the case that
p.UseShellExecute = false;
p.CreateNoWindow = true;
makes the PrintingArgs app not to show a window (even if I put only p.CreateNoWindow = true it shows a window).
Now it comes to my mind that maybe your are passing the args in a wrong way and makes the other program to fail and close inmediately, or maybe you are not pointing to the right file. Check paths and names, in order to find any mistake you could omit.
Also, using
Process.Start(fileName, args);
does not uses the info you set up with StartInfo into your Process instance.
Hope this will help,
regards
Not sure if anyone is still following this but here is what I came up with.
string genArgs = arg1 + " " + arg2;
string pathToFile = "Your\Path";
Process runProg = new Process();
try
{
runProg.StartInfo.FileName = pathToFile;
runProg.StartInfo.Arguments = genArgs;
runProg.StartInfo.CreateNoWindow = true;
runProg.Start();
}
catch (Exception ex)
{
MessageBox.Show("Could not start program " + ex);
}
Adding a space in the string allowed two arguments to be passed into the program I wanted to run. The program ran without issue after executing the code.
Have u set your ProcessWindowStyle to Hidden?
This is my code, working fine:
Process p=new Process();
p.StartInfo.FileName = filePath;//filePath of the application
p.StartInfo.Arguments = launchArguments;
p.StartInfo.WindowStyle = (ProcessWindowStyle)ProcessStyle;//Set it to **Normal**
p.Start();
System.Diagnostics.Process.Start(FileName,args);
Eg
System.Diagnostics.Process.Start("iexplore.exe",Application.StartupPath+ "\\Test.xml");
This question already has answers here:
Elevating process privilege programmatically?
(6 answers)
Closed 6 years ago.
I have a Visual Studio Windows app project. I've added code to download an installer update file. The installer after it has finished downloading would need administrator privileges to run. I have added a manifest file.
When user clicks on the DownloadUpdate.exe, UAC prompts the user for Admin permissions. So I assumed that all processes created and called within DownloadUpdate.exe will run in admin capacity. So I made the setup call my downloaded file with the following code:
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = strFile;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
Try this:
//Vista or higher check
if (System.Environment.OSVersion.Version.Major >= 6)
{
p.StartInfo.Verb = "runas";
}
Alternatively, go the manifest route for your application.
First of all you need to include in your project
using System.Diagnostics;
After that you could write a general method that you could use for different .exe files that you want to use. It would be like below:
public void ExecuteAsAdmin(string fileName)
{
Process proc = new Process();
proc.StartInfo.FileName = fileName;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.Start();
}
If you want to for example execute notepad.exe then all you do is you call this method:
ExecuteAsAdmin("notepad.exe");
This is a clear answer to your question:
How do I force my .NET application to run as administrator?
Summary:
Right Click on project -> Add new item -> Application Manifest File
Then in that file change a line like this:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Compile and run!
var pass = new SecureString();
pass.AppendChar('s');
pass.AppendChar('e');
pass.AppendChar('c');
pass.AppendChar('r');
pass.AppendChar('e');
pass.AppendChar('t');
Process.Start("notepad", "admin", pass, "");
Works also with ProcessStartInfo:
var psi = new ProcessStartInfo
{
FileName = "notepad",
UserName = "admin",
Domain = "",
Password = pass,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process.Start(psi);
This works when I try it. I double-checked with two sample programs:
using System;
using System.Diagnostics;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
Process.Start("ConsoleApplication2.exe");
}
}
}
using System;
using System.IO;
namespace ConsoleApplication2 {
class Program {
static void Main(string[] args) {
try {
File.WriteAllText(#"c:\program files\test.txt", "hello world");
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
}
}
First verified that I get the UAC bomb:
System.UnauthorizedAccessException:
Access to the path 'c:\program
files\test.txt' is denied. //
etc..
Then added a manifest to ConsoleApplication1 with the phrase:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
No bomb. And a file I can't easily delete :) This is consistent with several previous tests on various machines running Vista and Win7. The started program inherits the security token from the starter program. If the starter has acquired admin privileges, the started program has them as well.
Here is an example of run process as administrator without Windows Prompt
Process p = new Process();
p.StartInfo.FileName = Server.MapPath("process.exe");
p.StartInfo.Arguments = "";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Verb = "runas";
p.Start();
p.WaitForExit();
Process proc = new Process();
ProcessStartInfo info =
new ProcessStartInfo("Your Process name".exe, "Arguments");
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute =true;
info.Verb ="runas";
proc.StartInfo = info;
proc.Start();
Use this method:
public static int RunProcessAsAdmin(string exeName, string parameters)
{
try {
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = CurrentDirectory;
startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
startInfo.Verb = "runas";
//MLHIDE
startInfo.Arguments = parameters;
startInfo.ErrorDialog = true;
Process process = System.Diagnostics.Process.Start(startInfo);
process.WaitForExit();
return process.ExitCode;
} catch (Win32Exception ex) {
WriteLog(ex);
switch (ex.NativeErrorCode) {
case 1223:
return ex.NativeErrorCode;
default:
return ErrorReturnInteger;
}
} catch (Exception ex) {
WriteLog(ex);
return ErrorReturnInteger;
}
}
Hope it helps.
You probably need to set your application as an x64 app.
The IIS Snap In only works in 64 bit and doesn't work in 32 bit, and a process spawned from a 32 bit app seems to work to be a 32 bit process and the same goes for 64 bit apps.
Look at: Start process as 64 bit