command line Start.Info.Argument error - c#

I would like run this command with C# :
"%WIX%/bin/heat.exe" dir "C:\Documents and Settings\APP" -gg -sfrag
-cg Appli -out wixappli.wxs
I have this code (Program.cs) :
System.Diagnostics.Process process1 = new System.Diagnostics.Process();
process1.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
process1.StartInfo.Arguments = String.Format("/k \"%WIX%/bin/heat.exe\" dir \"{0}\" -gg -sfrag -cg Appli -out wixappli.wxs ",
#"C:\Documents and Settings\APP");
process1.StartInfo.WorkingDirectory = #"C:\Documents and Settings\test";
process1.StartInfo.CreateNoWindow = true;
process1.StartInfo.ErrorDialog = false;
process1.Start();
Error:
'C:\Program' is not recognized as an internal command or external
command
I don't understand because without C#, it works.
Can you help me ?

Usually, path environments (%windir%, %temp%, etc) uses backslashes (\). try modifying your arguments to use backslashes instead of slashes (\"%WIX% \ bin \ heat.exe\"...

Related

How use cmd command in c# program?

I want cmd command in my c# winforms program. I want use command to compress file with 7zip.
In normal cmd i use cd "C:\Program Files(x86)\7-Zip" & 7z.exe a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
string zip = #"/c C:\Program Files(x86)\7-Zip";
string file = #"/c C:\xyz\txt";
string conv = "/c & 7z.exe a -tzip";
string method = "/c -m0=BZip2 -mx5";
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = #"C:\windows\system32\cmd.exe";
proc.Arguments = #"/c cd" + zip + conv + file + file + method;
System.Diagnostics.Process.Start(proc);
Doesn't work my code. How can i use this command in my program. I want compress file when i click in button
Something like this:
// File (exe) to start: combination of folder and exe
string fileName = Path.Combine(
// Let's not hardcode "C:\Program Files(x86)" and alike
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
#"7-Zip",
#"7z.exe");
// If desired arguments are
// a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
// we can join them as
string arguments = string.Join(" ",
#"a",
#"-tzip", //TODO: you may want to have these values
#"""C:\xyz\test\txt""", // as variables like file, conv, method etc.
#"""C:\xyz\test\txt""",
#"-m0=BZip2",
#"-mx5");
ProcessStartInfo procInfo = new ProcessStartInfo() {
FileName = fileName, // We want to start fileName
Arguments = arguments, // With arguments
}
// Process is IDisposable, do not forget to Dispose HProcess handle
using (var process = Process.Start(procInfo)) {
// It's fire and forget implementation, if you want to wait for results
// add process.WaitForExit();
// process.WaitForExit(); // uncomment, if you want to pause
}
You seem to have made a mistake with your arguments.
Your current command line will be:
C:\windows\system32\cmd.exe /c cd/c C:\Program Files(x86)\7-Zip/c & 7z.exe a -tzip/c C:\xyz\txt/c C:\xyz\txt/c -m0=BZip2 -mx5
The spurious /c everywhere is not going to help and neither are the lack of spaces.
I'm guessing what you meant to run was these two commands in succession:
cd C:\Program Files(x86)\7-Zip
7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5
Which means you should change your code to:
string zip = #" C:\Program Files(x86)\7-Zip";
string file = #" C:\xyz\txt";
string conv = " & 7z.exe a -tzip";
string method = " -m0=BZip2 -mx5";
This will produce:
C:\windows\system32\cmd.exe /c cd C:\Program Files(x86)\7-Zip & 7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5

C# Process removes quoted arguments

The command I'm trying to execute is
"C:\Program Files (x86)\MyAPP\solr-6.2.0\bin\solr" start -f -c -z "10.195.42.93:2181,10.195.42.92:2181" -h 10.195.42.92
And this works just fine on the command Line.
I'm trying to execute this as C# process.
NOTE: Below code works fine if i remove the quotes surrounding IPList
var IPList="10.195.42.93:2181,10.195.42.92:2181";
var hostIP="10.195.42.92"
string command = #"/c ""C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr"" start -f -c -z """ + IPList + #""" -h " + hostIP;
Process process = new Process();
log.Info("Starting " + command);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (s, e) => log.Info(e.Data);
process.ErrorDataReceived += (s, e) => log.Info(e.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
This throws error :
'C:\Program' is not recognized as an internal or external command,...
I have looked at the similar issue posted Here and tried the /s option but no heed.
What am i missing here ?
As the error says, it is trying to execute C:\Program which basically means something is wrong with the way you are escaping quotes. May be try to escape " using - \"
You're asking cmd.exe to run the command C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr. It will not run "C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr" because the quotes are consumed by the invocation of cmd.exe.
I do not know the proper way to fix this. You probably need to add escaped quotes somehow.
Enclosing the command with "(quotes) after /c solves the issue.
Example :
"/c ""c:\prog files\xyz\solr" start -c -z "blah,blah,blah" -h
IP "
Basically the command that is getting executed is
> cmd.exe /c "C:\Program Files (x86)\MyAPP\solr-6.2.0\bin\solr" start -f -c -z "10.195.42.93:2181,10.195.42.92:2181" -h 10.195.42.92
This command fails, and this has nothing to do with c# process api or .net.

How to escape characters in string variables? (cmd commands)

I have a list of cmd commands I need to run:
sc config winmgmt start= demand
net stop winmgmt /y
rd /S /Q %windir%\\system32\\wbem\\repository
rd /S /Q %windir%\\syswow64\\wbem\\repository
C:\\WINDOWS\\system32\\msiexec.exe /unregserver
C:\\WINDOWS\\system32\\msiexec.exe /regserver
regsvr32 /s C:\\WINDOWS\\system32\\scecli.dll
regsvr32 /s C:\\WINDOWS\\syswow64\\scecli.dll
for %i in (C:\\WINDOWS\\system32\\wbem\\*.dll) do RegSvr32 -s %i
mofcomp C:\\WINDOWS\\system32\\wbem\\rsop.mof
mofcomp C:\\WINDOWS\\system32\\wbem\\en-US\\rsop.mfl
for %i in (C:\\WINDOWS\\syswow64\\wbem\\*.mof) do mofcomp %i
mofcomp.exe C:\\Windows\\system32\\WBEM\\win32_encryptablevolume.mof
sc config winmgmt start= auto
net start winmgmt
I have a txt file in Properties.Resources containing the above commands. And I try to run them like this:
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
Process process = Process.Start(processStartInfo);
if (process != null)
{
foreach(var myString in File.ReadAllLines(Properties.Resources.commandsTXT))
{
process.StandardInput.WriteLine(myString);
}
process.StandardInput.Close();
}
The problem is when I try to run it, I get Unhandled Exception: System.ArgumentException: Illegal characters in path. How could I possibly escape every character in it? As you can see I already tried replacing \s to \\s, but no luck

Parameters from bat file to sql script not working

I have a C# application calling a bat file:
new string[] { branchName + ".Order", "Order.bak", "Order"}
var arguments = String.Format("\"{0}\"\"{1}\"\"{2}\"\"{3}\"", stringse[0], stringse[1], stringse[2], sqlPath + "\\");
var psi = new System.Diagnostics.ProcessStartInfo(filename)
{
RedirectStandardOutput = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
UseShellExecute = false,
Arguments = arguments
};
Process createDBs = Process.Start(psi);
System.IO.StreamReader myOutput = createDBs.StandardOutput;
while (!createDBs.HasExited)
{
string output = myOutput.ReadToEnd();
LogStep(output);
}
The bat file looks like this:
#echo off
REM ************************************************
REM * RestoreDB script for use by Development Team *
REM ************************************************
#echo %1 %2 %3 %4
#echo Restore of database started...
SQLCMD -S (local) -d Master -i RestoreDB.sql -v varDatabaseName=%1 varDatabaseBackupFileName= %2 varLogicalName= %3 varSQLDataFolder= %4
#echo Restore of database finished...
pause
The Sql script looks receive these parameters like this:
SET #DatabaseName = N'$(varDatabaseName)'
SET #DatabaseBackupFileName = N'$(varDatabaseBackupFileName)'
SET #LogicalName = N'$(varLogicalName)'
SET #SQLDataFolder = N'$(varSQLDataFolder)'
(Can't paste complete code due to security reasons)
Now my problem is that I don't think I'm passing the variables from bat to sql script correctly.
When calling the bat file out of C# it echo's the values
When calling the sql directly out of command prompt it works correctly, so I know my sql script is working:
SQLCMD -S (local) -d Master -i RestoreDB.sql -v varDatabaseName="Piet.Order.Test" varDatabaseBackupFileName="Order.bak" varLogicalName="Order" varSQLDataFolder="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\"
I don't get any errors back to my C# application, so when running my application it just runs and calls the bat with parameters, the call from bat to sql seems to go wrong.Is it a format problem with my parameters. I tried single quotes '%1' and tried doubele quotes "%1", nothing seems to work.Can anybody shine some light.Any way maybe how I can retrieve the error that is happening with the call to sql
You need to add a space between arguments
var arguments = String.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"",
stringse[0], stringse[1], stringse[2], sqlPath + "\\");
In these scenarios (debugging parameters) could be very useful to start the command processor and leave the console window open after the execution of the program
var arguments = String.Format("/K {4} \"{0}\" \"{1}\" \"{2}\" \"{3}\"",
stringse[0], stringse[1], stringse[2], sqlPath + "\\", filename);
and call the command processor in the ProcessStartInfo
var psi = new System.Diagnostics.ProcessStartInfo("cmd.exe")

Cannot open batch file using System.Diagnostics.Process

For some reason I can't get this batch file to run in my .NET 4.0 MVC3 project. I am using server 2008R2 64 bit - does cmd.exe operate differently?
System.Diagnostics.Process process1;
process1 = new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine = "d:\audioTemp\test.bat";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
System.Diagnostics.Process.Start("cmd.exe", #"/c d:\audioTemp\test.bat")
Your string contains a tab character \t. Either escape the backslashes:
strCmdLine = "d:\\audioTemp\\test.bat";
Or use a verbatim string literal:
strCmdLine = #"d:\audioTemp\test.bat";

Categories

Resources