I want to run cmd in a asp.net application. Here is my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd";
process.StartInfo.Arguments = "cd C:\\";
process.Start();
The program starts the cmd.exe but instead of "C:>" I see this:
C:\program Files\Common Files\Microsoft Shared\Devserver\10.0>
Could somebody tell me what i do wrong in the code? Thanks in advance!
The Arguments are used as the parameters to the application being called, therefore it's the same as having cmd "cd c:\" in your Run prompt under the Start Menu.
In this instance, what I think you want is the following (instead of the Arguments line)...
process.StartInfo.WorkingDirectory = "c:\\";
Related
I can backup the settings of IIS with the following commands
//sites
C:\Windows\system32\inetsrv\appcmd.exe list site /config /xml > C:\Temp\iis_config_sites.xml
//apppools
C:\Windows\system32\inetsrv\appcmd.exe list apppool /config /xml > C:\Temp\iis_config_apppool.xml
When I run these commands from the Command Prompt it works fine. The XML files are created. But I wanted to automate this process by executing this command from C# code with System.Diagnostics.Process. For that I use the following code
using (Process process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = #"C:\Windows\system32\inetsrv\appcmd.exe";
startInfo.Arguments = #"list site /config /xml > C:\Temp\iis_config_sites.xml";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
But when I run this code nothing happens. The XML file is not created. But there are no errors and no warnings also. The program that executes this command runs under the Administrator account with Windows Server 2019 and IIS 10. I also tried adding WorkingDirectory, but that did not help either.
I use Process to generate a zipped folders with WinRar and that also works fine.
So if anyone knows what could be the problem that would be great.
The problem is that you are using '>' in the parameters, which is a CMD command to instruct it to write the output of command (appcmd.exe in this case) to specified file path (C:\Temp\iis_config_sites.xml). Which of course doesn't work as the parameter of appcmd.exe.
You have two options:
To use it the following way, which is equivalent of executing the command in CMD:
startInfo.FileName = #"C:\Windows\system32\cmd.exe";
startInfo.Arguments = "/c \"C:\\Windows\\system32\\inetsrv\\appcmd.exe\" list site /config /xml > C:\\Temp\\iis_config_sites.xml";
Note that the /c is a command line option of cmd.exe to execute the rest of parameters as a "command", just as if you entered it into command line window. The rest is just the same command which you typed in the cmd normally.
Use another option provided by appcmd.exe for backing up IIS that avoids the need to redirect output to file. The command is appcmd.exe add backup %backupname%. This command adds a backup of the IIS config and later you can restore it by restore backup $backupname%.
I'm trying to run a command on remote machine using PSEXEC tool, it runs successfully using command prompt but it fails in an asp.net project showing the following output error.
PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark
Russinovich Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to lnxdevx...
Starting PSEXESVC service on lnxdevx...
Connecting with PsExec service on lnxdevx...
Error deriving session key:.
Here is my sample c# code.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = "-u xxxxxx -p xxxxxxxxxx \\\\lnxdevx -i -d -w \"C:\\DIRECTORY\" cmd.exe /C dir";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string errormessage = p.StandardError.ReadToEnd();
p.WaitForExit();
The problem is that you don't have to open psexec, but cmd and from there you run psexec.
The code you're trying to do is wrong too, you need to use
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"; \\if it is in System32
startInfo.Arguments = "psexec -u xxxxxx -p xxxxxxxxxx \\machine *your stuff*";
process.StartInfo = startInfo;
process.Start();
I'm not testing, but I'm quite sure this will work :)
Although the accepted answer is solving the problem, there is a better solution (at least without involving cmd).
Go to the app pool -> Advanced Settings -> Section "Process Model" -> "Load User Profile".
The default is false. When you change it to true, everything should work.
A bit more of what there is (more of an interpretation, further research is needed).
psexec and all tools from SysInternals require EULA to be accepted. This can be solved by calling them with -accepteula see psexec at ss64
this should be saved in the registry. You have to have a profile for this to be read.
Based on https://nigglingaspirations.blogspot.com/2015/04/psexec-error-deriving-session-key.html
I want to run the command line pnputil in c# program. The program needs to install USB driver.
I know how to run cmd in c# program, but I have a different problem:
The driver that I want to install does't have permission of windows.
If I Install the driver via the "device manager->update driver" and choose the path of the driver, I get "Security Message" from windows that "windows can't verify the publisher of this drive software" and let me choose if install the driver or not (of course, if I choose to install - the installing succeeds).
If I run the command from the cmd pnputil -a <path_name_inf> I get this message too and I can install the driver.
But when I try to run the command via c# program - the program runs but the driver is not installed (I also don't get this message).
my code in c#:
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 Pnputil -a <path_name_inf>";
process.StartInfo = startInfo;
process.Start();
How can I do that?
You could try to run the cmd using the runas verb:
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
This parameter causes a privileges elevation. The same thing could be reached when you use "Run as administrator" in explorer.
Your questions is more towards installing unsigned drivers.
Can you please try following steps:
Open a command prompt as an admin and type:
bcdedit -set TESTSIGNING ON
Please refer MSDN link for more infromation on Test Signing. Making Test signing may put a watermark as TEST in desktop waterpaper
I know this is old, but I wanted to share my solution in case it can help someone else.
My specific scenario is that I wanted to add an unsigned driver package to the driver store on Windows 7 Home Premium 64-bit.
Like the OP, this worked as expected (meaning I received the security warning and the driver package was added to the driver store) if I executed pnputil -a <path_to_inf> using a command prompt using "Run as administrator".
However, if I tried to call pnputil from within C#, I was never able to get the security warning and the driver package was not added to the driver store. I had tried using various options (i.e. Verb = "runas", UseShellExecute = true, etc) with the FileName set as "pnputil" or "cmd".
Ultimately, what worked for me was to create a simple batch file that contained the following:
%windir%\sysnative\pnputil /a <path_to_inf>
My C# app would then call this batch file as follows:
Process proc = new Process();
proc.StartInfo.FileName = "<path_to_bat_file>";
proc.StartInfo.Verb = "runas";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
The user would first be prompted because I asked to run with elevated permission followed by the expected security warning. The driver package would then be added to the driver store as expected.
If this does not work as expected, you can add "pause" (without quotes) on a new line after the last command in the batch file and remove the WindowStyle and CreateNoWindow lines from the C# code to see what is happening on the command prompt.
I'm trying to silently install Microsoft Visual C++ 2005 Redistributable using process.start() but have never got any luck. Please anyone help me.
I have the following code
string path = mypath;
startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.Arguments = "/q:a /c:\"msiexec /i vcredist.msi /qn /l*v %temp%\\vcredist_x86.log\"";
startInfo.FileName = path + #"\vcredist_x86.exe";
System.Diagnostics.Process exeProcess = System.Diagnostics.Process.Start(startInfo);
exeProcess.WaitForExit();
the actual commandline argument is
/q:a /c:"msiexec /i vcredist.msi /qn /l*v %temp%\vcredist_x86.log"
I got this command from http://blogs.msdn.com/b/astebner/archive/2006/08/23/715755.aspx
I verified this argument works perfect manually in cmd, but the code never works.
I also tried following code but had no luck as well
startInfo.Arguments = Encoding.Default.GetString(Encoding.UTF8.GetBytes("/q:a /c:\"msiexec /i vcredist.msi /qn /l*v %temp%\\vcredist_x86.log\""));
startInfo.Arguments = "/q:a /c:\"msiexec /i vcredist.msi /qn /l*v %temp%\\\\vcredist_x86.log\""
Please anyone let me know.
Thanks
I don't have the bandwidth to download these things to test, but, try this:
replace the 4 slashes in:
/l*v temp%\\\\vcredist_x86.log\""
with just 2 slashes:
/l*v temp%\\vcredist_x86.log\""
This question already has answers here:
How to run silent installer in C#
(2 answers)
Closed 9 years ago.
I have this C# code:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "–s –v –qn";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = desktopPath + "\\" + "MyInstaller_7.1.51.14.exe";
Process.Start(psi);
The first line simply grabs the path of my desktop and the rest attempts to run an installer exe file in silent mode. By silent mode I mean, in the background, without the install wizard, or any UI of any sort during installation. The –s –v –qn arguments are there so that that the installation runs in silent mode.
The problem is that when I run the command equivalent of the above in the command prompt, which is this:
C:\Users\ME\Desktop>MyInstaller_7.1.51.14.exe -s -v -qn
The installer runs as wanted, in silent mode.
Unfortunately, the problem is that trying the same thing in C# with the above code does NOT run the installer in silent mode. The installation wizard DOES appear, which is BAD for by purposes.
I'm thinking maybe I need to run this like a service via C# or under the 0 id of the users. Or with an -i switch. I'm not really sure. Can anyone help??
Just for clarification, my question is, how do I write C# code to run my installer.exe file in silent mode, in the background, with no visible UI?
Please help.
This is the correct answer:
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = newRenamedFile;
psi.UseShellExecute = false;
Process.Start(psi);
The issue was the switches were missing the forward slashes.