Command Syntax for Processstartinfo from variables - c#

Can someone please help me out with the syntax here
string sourcePath = #textBox2.Text.ToString();
string targetPath = #textBox1.Text.ToString();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(#"XCOPY C:\folder D:\Backup\folder /i");
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process copyFolders = System.Diagnostics.Process.Start(psi);
copyFolders.WaitForExit();
I am trying to change this line below to replace c:\folder with (sourcePath) and D:\Backup\folder with targetPath
(#"XCOPY C:\folder D:\Backup\folder /i");
I keep getting source not found when I messagebox like this it looks ok
MessageBox.Show(#"XCOPY " + (sourcePath) + (targetPath) + " /i");

You'll need to debug, I'd start here:
string args = string.format("{0} {1} /i", sourcePath, targetPath);
Debug.WriteLine(args);
//verify your paths (both) are correct
string cmd = string.format("XCOPY {0}", args);
Debug.WriteLine(cmd);
//copy the command and test it out directly in cmd
Also, try passing your arguments....as arguments....
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("XCOPY", args);
You can look at the documentation for an example of passing with arguments

Nothing there really looks wrong. Perhaps debug it so you can get the text out of it and copy/paste it to Explorer to make sure the paths are valid and there's nothing funky going on?
Also, you don't need to do .ToString() on the .Text property of a TextBox. it's already a string.

Related

Other way to put a path in quotes when using DirectoryInfo?

I'm currently working with reg.exe and I'm creating a process with reg.exe as the Process.FileName.
When I try to execute reg.exe like following
REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS D:\\Backups\\Test.reg
everthing works fine.
But as soon as I try to execute it like this
REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS D:\\Backups\\Backup folder 1\\Test.reg
nothing happens - and I know why! The target path isn't put in quotes. As soon as I do that everything works fine again.
My problem now is that I'm handling all my file and folder paths as instances of DirectoryInfo. When I pass the path with quotes as a string, e.g. like that
DirectoryInfo targetFolder = new DirectoryInfo("\"D:\\Backups\\Backup folder 1\\Test.reg\"")
I instantly receive an exception telling me that the given path's format is not supported.
Is there any way to put the path in quotes and still work with DirecotryInfo?
I really need to put my path in quotes - otherwise the command won't work.
Here's some example code:
DirectoryInfo backupPath = new DirectoryInfo("D:\\Backups\\Backup folder 1\\Test.reg");
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "reg.exe";
startInfo.CreateNoWindow = true;
startInfo.Arguments = "REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS " + backupPath.FullName;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
When I run this code, nothing happens - nor errors or exceptions. The .reg file itself isn't created either.
When I try to run it like this
DirectoryInfo backupPath = new DirectoryInfo("\"D:\\Backups\\Backup folder 1\\Test.reg\"");
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "reg.exe";
startInfo.CreateNoWindow = true;
startInfo.Arguments = "REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS " + backupPath.FullName;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
I'm getting a System.NotSupportedException telling me "The given path's format is not supported." But I actually need to put the path in quotes - otherwise the command itself won't work...
You are adding quotes in the wrong place: constructor of DirectoryInfo will strip them anyway to normalize the path, so you can skip adding them:
var backupPath = new DirectoryInfo("D:\\Backups\\Backup folder 1\\Test.reg");
You can force quotes around the path when you add backupPath.FullName to the arguments, like this:
startInfo.Arguments = "REG EXPORT HKLM\SOFTWARE\Intel\IntelAMTUNS \"" + backupPath.FullName + "\"";

running dos command line from C#?

I am trying to run this command from command-line prompt:
"D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless"
It works perfect when I type it in a command-line console.
However, when I was trying to make it work from C# application, it failed. I tried following, but seems the command above did not get executed somehow:
string fijiCmdText = "D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless";
System.Diagnostics.Process.Start("cmd.exe", fijiCmdText);
Anyone has any idea how to change it to work? Thanks.
The problem was solved as in the direction Chris Haas pointed out. It does not mean other answers don't work, it just means the problem can be solved at least in one way.
Here it is, simply adding "/C " in the code, and it should work:
Original that did not work:
string fijiCmdText = "D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless";
System.Diagnostics.Process.Start("cmd.exe", fijiCmdText)
;
Current code that works:
string fijiCmdText = "/C D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless";
System.Diagnostics.Process.Start("cmd.exe", fijiCmdText);
Here is the reference mentioned by Chris Haas. See EDIT3
You don't have to run cmd.exe, just create ProcessStartInfo object and pass the command with its parameters to it. Like this:
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("your command", "parameters");
Here is an example that shows you how to do it:
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("tree.com", "/f /a");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = info;
p.Start();
p.WaitForExit();
So in your case, this is your command: "D:\\fiji\\fiji.exe" and this is your command parameters or arguments: #"-macro D:\\fiji\\macros\\FFTBatch.ijm --headless"
Try This:
ProcessStartInfo info = new ProcessStartInfo(#"D:\fiji\fiji.exe",#"-macro D:\fiji\macros\FFTBatch.ijm --headless");
Process process = new Process();
process.StartInfo = info;
process.Start();

c# ProcessStartInfo

Why is this
ProcessStartInfo myProcess = new ProcessStartInfo(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);
working, but
ProcessStartInfo myProcess = new ProcessStartInfo();
myProcess.FileName = Path.GetFileName(path);
myProcess.WorkingDirectory = Path.GetDirectoryName(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);
is not.
I wanted to use the second one because of this question: https://stackoverflow.com/a/2621943/1306186
I am constantly getting a file not found exception... Any ideas?
Edit:
Path is for example #"C:\Users\User\Desktop\ConsoleApplication2.exe"
This bit is wrong
myProcess.FileName = Path.GetFileName(path);
this should be
myProcess.FileName = path;
Pass in C:\SomeDir\SomeApp.exe and the code you have will set the filename to SomeApp.exe, which it can't find. Count yourself lucky, there are circumstances where it could have (e.g. your app and the app you want to run being in the same folder), and then you would have possibly got a funny when deploying.
I would try using Path.GetFullPath() instead of simply Path.GetFileName() since the constructor initializes the FileName with the full path when you use it with the string parameter.

Spawning command prompt app in CMD window in C#

I need to run a legacy app that is run from a cmd window using the Process class.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C \"C:\\MySys\\My2Com.exe –r " + Parameters.FullPath;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch (Exception e)
{
string sMsg = "Error copying the files to " + Parameters.FullPath + ".";
HandleErrorMsg(e, sMsg);
return;
}
The process My2Com.exe should run in the background, however, I consistantly get the message that a file, used when run from the cmd line with different flags, is missing. If I run the command as indicated in a cmd window, C:\MySys\My2Com.exe –r FullyQualPath, it works as expected. I have tried several different ways to set up the Process class without success.
Any suggestions would be appreciated.
Thank you.
Try this one -
startInfo.Arguments = "/C \"C:\\MySys\\My2Com.exe –r\" " + Parameters.FullPath;
will this work if you do the following
startInfo.Arguments = #"/C "C:\MySys\My2Com.exe /r" " + Parameters.FullPath +"\"";
keep in mind that if there are spaces in the filepath you need to wrap around """ for example if the filepath were like this #"""C:\Wolf Lair\WorkDeskTemp\"
notice the # and the """
you need to append the ending quotes to the string +"\""; after Parameters.FullPath;
You know why is it not working because
You haven't completed quotes
Try this:-
startInfo.Arguments = "/C \"C:\\MySys\\My2Com.exe\" –r \"" + Parameters.FullPath+"\"";
startInfo.Arguments = #"/C ""C:\MySys\My2Com.exe –r """ + Parameters.FullPath + "\"";
Also, see Sending commands to cmd prompt in C#. I'd recommended using some of the code from my answer there so that you can intercept the standard output and standard error to see what you're getting.

Running cmd commands via .NET?

System.Diagnostics.Process proc0 = new System.Diagnostics.Process();
proc0.StartInfo.FileName = "cmd";
proc0.StartInfo.WorkingDirectory = Path.Combine(curpath, "snd");
proc0.StartInfo.Arguments = omgwut;
And now for some background...
string curpath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
omgwut is something like this:
copy /b a.wav + b.wav + ... + y.wav + z.wav output.wav
And nothing happens at all. So obviously something's wrong. I also tried "copy" as the executable, but that doesn't work.
Try the prefixing your arguments to cmd with /C, effectively saying cmd /C copy /b t.wav ...
According to cmd.exe /? using
/C <command>
Carries out the command specified by
string and then terminates
For your code, it might look something like
// ..
proc0.StartInfo.Arguments = "/C " + omgwut;
Notes:
A good way to test whether your command is going to work is to actually try it from a command prompt. If you try to do cmd.exe copy ... you'll see that the copy doesn't occur.
There are limits to the length of the arguments you can pass as arguments. From MSDN: "The maximum string length is 2,003 characters in .NET Framework applications and 488 characters in .NET Compact Framework applications."
You can bypass the shelling out to command by using the System.IO classes to open the files and manually concatenate them.
Try this it might help you.. Its working with my code.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// 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.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 proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
Even you can try this.. this is even better.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="iexplore";
proc.StartInfo.Arguments="http://www.microsoft.com";
proc.Start();
proc.WaitForExit();
MessageBox.Show("You have just visited " + proc.StartInfo.Arguments);
Daniels cmd /c idea will work. Keep in mind there is a limit to the length of a command line probably 8k in your case see this for details.
Since you are in a .Net app anyway, File.Copy may be quite a bit easier/cleaner than this approach.

Categories