run command promt application from code C# - c#

This is my code:
var startupPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // +\\Common
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 " + startupPath + #"\Common\sound 1.wav result.wav -tempo=35";
process.StartInfo = startInfo;
process.Start();
Pathes are correct. A wanna to start application and set this parameters sound 1.wav result.wav -tempo=35. What am I doing wrong?
content of startInfo.Arguments:
"/c C:\\SOUNDS\\New folder\\TrunscribeHelper\\Common\\sound 1.wav result.wav -tempo=35"
I've tryed to start it directly:
ProcessStartInfo startInfo = new ProcessStartInfo(string.Concat(startupPath + #"\Common", "\\", "sound.exe"));
startInfo.Arguments = "1.wav result.wav -tempo=35";
startInfo.UseShellExecute = false;
System.Diagnostics.Process.Start(startInfo);
But Am I doing wrong now?

Your have to set your path in the args under double quotes:
"/c \"C:\\SOUNDS\\New folder\\TrunscribeHelper\\Common\\sound 1.wav\" result.wav -tempo=35"

Can you try changing this line:
startInfo.Arguments ="/c " + startupPath + #"\Common\sound 1.wav result.wav -tempo=35";
to
startInfo.Arguments ="/c \"" + startupPath + #"\Common\sound"" 1.wav result.wav -tempo=35";

Related

Create a forms app to restart a remote PC

I just need to create a tool to restart a specific windows device in our city offices. which will always in 192.168.cityID.33 I have found a way to detect the citycode portion of the IP and to add 33 to it in GetIPMethod.
thee mentioned device have different set of username and password im having issues passing the restart command to the cmd
PS: I'm not a full time developer i'm just a network admin who tries to reduce my daily work load :)
Thanks in advance
public void Command1()
{
String IP = GetIPAddress().ToString();
string NewIP = IP.Substring(0, IP.LastIndexOf("."));
string TOPIP = NewIP + ".33";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StandardInput.WriteLine("NET USE \\" + TOPIP + "\receiver /USER:GenusDS G3nu5DS");
process.StandardInput.WriteLine("shutdown /m \\" + TOPIP + " /r /f -t 00");
process.Start();
process.StandardInput.Flush();
process.Close();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.ReadKey();
string strCmdText;
strCmdText = "NET USE \\" + TOPIP + "\receiver /USER:GDS G3nS";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
}
public static IPAddress GetIPAddress()
{
IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address =>
address.AddressFamily == AddressFamily.InterNetwork).First();
return ip;
}
I normally do something similar to below:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C net use \\[My IP] [My Password] /USER:[My Username]";
process.StartInfo = startInfo;
Process.Start(startInfo);
And this question shows how to run multiple commands in one process.
Hope that helps.
var proc1 = new ProcessStartInfo();
string Command;
proc1.UseShellExecute = true;
Command = "net use " + slash + TOPIP + "\\receiver /user:GenusDS G3nu5DS&shutdown /m " + slash + TOPIP + " /r /f -t 00";
proc1.WorkingDirectory = #"C:\Windows\System32";
proc1.FileName = #"C:\Windows\System32\cmd.exe";
/// as admin = proc1.Verb = "runas";
proc1.Arguments = "/c " + Command;
proc1.WindowStyle = ProcessWindowStyle.Maximized;
Process.Start(proc1);

ffmpeg ask for intsall

I am trying to create an image/thumbnail from the video stored in local folder.
Here is what I am doing is-
Process p;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Server.MapPath("~/FFMPEG/ffmpeg.exe");
info.RedirectStandardOutput = false;
info.CreateNoWindow = false;
info.Arguments = " -i " + videopath + " -vframes 1 " + imagepath + "%d.jpg";
info.UseShellExecute = false;
p = Process.Start(info);
while (!p.HasExited) { Thread.Sleep(10); }
When I execute above code a popup box comes to install ffmpeg.exe
If I install the software, for the next time it asks again.
Am I doing some mistake?
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Below is the code
Process p;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Server.MapPath("~/FFMPEG/ffmpeg.exe");
info.RedirectStandardOutput = false;
// below lines will not open the command window
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = " -i " + videopath + " -vframes 1 " + imagepath + "%d.jpg";
info.UseShellExecute = false;
p = Process.Start(info);
while (!p.HasExited) { Thread.Sleep(10); }

C# run batch file with argument with spaces

I need to run batch file, which has path (can contains spaces) as argument.
Batch file is really simple:
echo off
echo %1 > echotest.txt
Csharp code I am using to run this batch file:
ProcessStartInfo info = new ProcessStartInfo();
info.UserName = KIM_USER;
info.Password = ConvertToSecureString(KIM_USER_PASSWORD);
info.FileName = theTask.Path;
info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\"";
info.Domain = Environment.MachineName;
info.WorkingDirectory = Path.GetDirectoryName(theTask.Path);
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process batProcess = Process.Start(info);
batProcess.WaitForExit();
Basically in parameter comes e.g {Test_Path} and this is in TranslateParameter converted to real path, e.g: D:\Test Path\ (contains spaces)
This does not work for me, it returns me exit code 1 everytime.
If i remove \" from info.Arguments, it works, but in output file is just D:\Test
Any suggestions?
Regards
Can you try this if that works:
ProcessStartInfo info = new ProcessStartInfo();
info.UserName = KIM_USER;
info.Password = ConvertToSecureString(KIM_USER_PASSWORD);
info.FileName = theTask.Path + " \"" + TranslateParameter(theTask.Parameter) + "\"";
//info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\"";
info.Domain = Environment.MachineName;
info.WorkingDirectory = Path.GetDirectoryName(theTask.Path);
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process batProcess = Process.Start(info);
batProcess.WaitForExit();

Error with a code

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"c:\windows\system32\rundll32.exe";
startInfo.Arguments =
#"C:\WINDOWS\System32\shimgvw.dll,ImageView_Fullscreen C:\Users\Sony\Desktop\c.jpg"
I want to specify the image URI (C:\Users\Sony\Desktop\c.jpg) as a string variable.So what changes do I have to make in startInfo.Arguments.
Construct it before hand then.
string imagePath = "C:\Users\Sony\Desktop\c.jpg"; // or get this value from however you please
string arguments = string.Format("C:\WINDOWS\System32\shimgvw.dll,ImageView_Fullscreen {0}", imagePath);
startInfo.Arguments = arguments;

Multiple arguments with cmd.exe via a C# Process

I try to call the esriRegAsm.exe with arguments from a C# program.
The purpose is to register a Dll. Therefore I usually call the esriRegAsm.exe with the Dll as argument plus some additional parameters (/p:Desktop /s). This works fine if I type it into cmd.exe. Somehow I think that the process sends only the first string to the cmd and not the whole argument list, but I need the "" for the space character in the paths.
For debugging I added a message box and the strings seems to be okay.
Backslash or double backslash seems to be unimportant.
string targetDir = this.Context.Parameters["targ"];
string programFilesFolder = this.Context.Parameters["proFiles"];
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 \"" + programFilesFolder + "Common Files\\\\ArcGIS\\\\bin\\\\esriRegAsm.exe\" " + "\"" + targetDir + "RArcGISTest.dll\" /p:Desktop /s";
MessageBox.Show("/C \"" + programFilesFolder + "Common Files\\\\ArcGIS\\\\bin\\\\esriRegAsm.exe\" " + "\"" + targetDir + "RArcGISTest.dll\" /p:Desktop /s");
process.StartInfo = startInfo;
process.Start();
As I cannot attache a picture of the message box... the output is:
/C "C:\Program Files (x86)\Common Files\ArcGIS\bin\esriRegAsm.exe" "C:\install\RArcGISTest.dll" /p:Desktop /s"
Why are you double-escaping things, and why are you routing it through cmd.exe? Just execute the process directly:
string targetDir = this.Context.Parameters["targ"];
string programFilesFolder = this.Context.Parameters["proFiles"];
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = Path.Combine(programFilesFolder, #"Common Files\ArcGIS\bin\esriRegAsm.exe");
startInfo.Arguments = "\"" + Path.Combine(targetDir, "RArcGISTest.dll") + "\" /p:Desktop /s";
process.StartInfo = startInfo;
process.Start();

Categories

Resources