I going to get the command prompt from my C# application to compile some C++ files. Code is like this.
private void button1_Click(object sender, EventArgs e)
{
string filePath = #"C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe";
System.Diagnostics.Process.Start(filePath);
}
But After i click the button it suddenly comes and disappear. Even its opening two command prompts. I need to Hold it and only one command prompt should appear. Can some one provide me the necessary codes. Thank you.
You could also do this:
Process.Start("cmd /k cl.exe");
This will launch cmd.exe, running the specified command, and keeping the window open after it's done.
You could change the command from:
<command>
to:
cmd /k <command>
That will cause the command to be run, and then the window will stay open with the command prompt.
The simplest way would probably be something like so:
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "cmd",
Arguments = #"/k ""C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe"""
};
Process.Start(psi);
You could write your own command prompt program that acts as a container and runs the required EXE files. After running, it can use "cin" (c input) to wait for a keypress and this will stop it closing.
You're not opening a command prompt as such, you're just starting a command line app that opens, have nothing to do and then closes.
If you want to open a command prompt you can call System.Diagnostics.Process.Start("cmd");.
Related
I am trying to call tpmvscmgr.exe located in C:\Windows\System32
from my c# application.
I found this code:
string strCmdText;
strCmdText = #"/C Robocopy C:\Users\Johannes\test1 C:\Users\Johannes\test2";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
here Run Command Prompt Commands
and it works good.
However when I try to change it to
strCmdText = "/C Tpmvscmgr.exe create /name tpmvsc /pin default /adminkey random /generate ";
This does not work.
In fact when I debug and look around in the command prompt opened by the code I can not find the tpmvscmgr.exe in windows/system32.
Im guessing it is opened as a different user or with other priviledges or something but can this be fixed?
I really would need to run a tpmvscmgr.exe command from code.
OK I figured something out.
In my visual studio c# project I had "Any CPU" marked for the project. Apparently this means the console opens as win32.
When I changed the c# project to x86 I could run tpmvscmgr.exe
Pretty tricky stuff.
C# cmd prompt cannot see telnet.exe
Here's something that I want to achieve:
Lets say I'm preparing abc.exe (console application).
I want to invoke cmd.exe and then launch abc.exe through cmd. And I won't be keeping cmd.exe in my projects folder. I'll be using it from system32 folder from user's machine.
Is it possible?
As mentioned in comments, you can use Process.Start(pathToExe) to launch a new process.
You can start your program in a new cmd with cmd /C start "Title" "C:\path\to\app.exe":
string cmdPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
ProcessStartInfo newCmd = new ProcessStartInfo(cmdPath);
newCmd.Arguments = String.Format(#"/C start ""{0}"" ""{1}""", "WindowTitle", exePath);
Process.Start(newCmd);
You probably want some sort of conditional around that in order to not fork bomb yourself
Ok, so I'm having an issue starting the the Windows XP mode VM through a c# program. The command I'm using is vmwindow -file "absolute path to vmcx file" , but the problem is that the command does not work with the cmd prompt that my program kicks off. So, it's very weird. I can go to command prompt on my computer and run this command on my computer and it works, but if i have the same command on my c# program, the command prompt that pops up tells me the "vmwindow" is not a recognized command. I even looked at the paths of each of the command prompts and they're different, but they still both contain "C:\Windows\system32\" which is where vmwindow.exe exists. So, I navigate on the command prompt window that my program populated and the file "vmwindow.exe" is not there, but if I open a command prompt window from my computer and navigate to that folder, it exists there. I can't think of anything else as I already made sure they're both running in administrator mode, and also i tried starting a bat file which contained that command instead of running the command directly. Hope anyone knows anything about this. Here is the code I'm using:
private void button1_Click(object sender, EventArgs e)
{
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"<my path>";
startInfo.Arguments = "/k vmwindow.exe -file \"<path to vcmx file>\\Windows XP Mode.vmcx\"";
process.StartInfo = startInfo;
process.Start();
}
What you can do is using Powershell. It has a native integration for Hyper V control and is easy to call from c#
You can see all HV-cmdlets here
a simple command to start your machine would be
Start-VM "Windows 8.1 Pro" -Computername HV-Host1
// etcetc
Stop-VM "Windows 8.1 Pro" -Save
So this should be something like this in C#
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Start-VM "Windows 8.1 Pro" -Computername HV-Host1");
}
Probably it's because of the bitness setting you compile your program with. ("Platform target" and "Prefer 32-bit" settings under the build tab of the project).
32 and 64 bit processes see different files under System32.
See https://stackoverflow.com/a/950011
I have the below code to launch a cmd prompt and from that cmd prompt launch putty.exe with and IP address at the end.
private void btnRouter_Click(Object sender, EventArgs e)
{
MessageBox.Show((string)((Button)sender).Tag);
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
cmd.StartInfo.UseShellExecute = true;
cmd.Start();
cmd.StandardInput.WriteLine("Putty.exe " + ((string)((Button)sender).Tag));
cmd.WaitForExit();
}
Problem is I keep getting an error "StandardIn has not been redirected." from Visual Studio and when I try typing putty.exe in the command window that gets launched I get
"'putty.exe' is not recognized as an internal or external command,
operable program or batch file." which is REALLY weird because if I go to the run line, type cmd and then putty.exe it opens up immediately ever since I added the absolute folder path to the putty application to my system environment path.
Is there a reason the CMD opened from Visual Studio isn't using my Environment Path?
Still don't know why it is happening, however I went back to some previous code and put a copy of putty.exe in my debug folder and it launched successfully this time.
private void btnRouter_Click(Object sender, EventArgs e)
{
MessageBox.Show((string)((Button)sender).Tag);
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = "Putty.exe ";
startInfo.Arguments = ((string)((Button)sender).Tag);
myProcess.StartInfo = startInfo;
myProcess.Start();
}
This line will be causing the "StandardIn has not been redirected." error since you are trying to write to stdin and that handle has not be properly setup for input:
cmd.StandardInput.WriteLine("Putty.exe " + ((string)((Button)sender).Tag));
As to this question:
Is there a reason the CMD opened from Visual Studio isn't using my Environment Path?
When a parent process starts a child process that child process will inherit the environment of it's parent.
In other words the new cmd window you're starting will be inheriting the Visual Studio environment, but that does not mean the Visual Studio environment is the same as the environment of the command prompt.
You can test this by starting a command line prompt, running Visual Studio from that command line prompt and then creating your child cmd process.
Now your cmd process should have an environment matching the original command line plus any changes that Visual Studio added to it's copy of the environment.
Use the runas command.
http://technet.microsoft.com/en-us/library/cc771525.aspx
Can you run dir ?
public void runBatchfile(String batchfilename)
{
try
{
ProcessStartInfo processInfo = new ProcessStartInfo(batchfilename);
processInfo.UseShellExecute = false;
Process batchProcess = new Process();
batchProcess.StartInfo = processInfo;
batchProcess.StartInfo.CreateNoWindow = true;
batchProcess.Start();
batchProcess.WaitForExit();
}
catch (Exception r) { }
}
runBatchfile(#"c:\lol.bat");
lol.bat contains these 2 lines
dir c:\ /s /b > c:\filelist.txt
exit
and when I run my code all it does is creating a filelist.txt file, but doesn't actually perform the rest of the command, which does work if I manually insert it into CMD.
Btw I've tried making the extension .cmd and I also tried without the exit command, without any other results.
please help me :)
On my Windows 7 64-bit machine with Visual Studio 2010, running this code straight from the IDE doesn't do anything whatsoever.
On a hunch that it might have something to do with permission to write to the root directory of drive C:, I tried running the .exe directly from an Explorer window with admin rights (right-click, Run as Administrator) and that worked.
I'd say it's a permission problem.
Maybe you could redirect the output to a file located elsewhere?
update:
I changed the batch file so that the dir command gets redirected to my desktop and it runs just fine.
I've had issues with this as well when calling from C#. The workaround that I usually use is to physically allow it to show the window when running the command. Not sure why this makes a difference but it has worked for me in the past.