CMD Commands won't run in C# - c#

I'm trying to run the following:
String command = #"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder;
System.Diagnostics.Process.Start("CMD.exe", "/K PATH C:\\Program Files\\R\\R-3.1.1\\bin;%path%");
System.Diagnostics.Process.Start("CMD.exe", "/K " + command);
Nothing happens when I execute it, does anyone know why? If I try
System.Diagnostics.Process.Start("CMD.exe", "/K MD TEST");
That works fine :s
e: Some extra info, The first command is setting the PATH so that the Rscript can be called by just typing Rscript. Also, both of these commands work when I do them in a normal CMD interface.

Prepare a batch file and execute it
using(StreamWriter sw = new StreamWriter("runscript.cmd", false))
{
sw.WriteLine(#"PATH C:\Program Files\R\R-3.1.1\bin;%path%");
sw.WriteLine(#"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder);
}
System.Diagnostics.Process.Start("CMD.exe", "/K runscript.cmd");
This assumes that you have read/write permissions on the current directory. You can change the location to a more suitable position using
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileCmd = Path.Combine(path, "runscript.cmd");
using(StreamWriter sw = new StreamWriter(fileCmd, false)
....

As realised by Steve, I was running two console processes. To resolve this I simply ran both commands in the same process.

cmd.Arguments = "/K \"" + fullFilePath;
*try double " " right before PATH

Related

"sqlcmd.exe -S Instance USE ..." work in command line, but I can't move it to c # process

I am trying move this command to C# Process:
SQLCMD.EXE -S InstanceName
USE [master]
GO
CREATE DATABASE [Ek] ON
( FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Ek_Primary.mdf' ),
( FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Ek_Primary.ldf' )
FOR ATTACH ;
GO
EXIT
I have:
try
{
Process p = CreateProcess();
p.StartInfo.FileName = #"C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\SQLCMD.EXE";
p.StartInfo.Arguments = "-S InstanceName" + "\n" +
"USE [master]" + "\n" +
"GO" + "\n" +
"CREATE DATABASE [Ek] ON" + "\n" +
"( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\MSSQL\\DATA\\Ek_Primary.mdf' )," + "\n" +
"( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\MSSQL\\DATA\\Ek_Primary.ldf' )" + "\n" +
"FOR ATTACH ;" + "\n" +
"GO" + "\n" +
"EXIT" + "\n";
Console.WriteLine(p.StartInfo.Arguments);
p.Start();
var output = p.StandardOutput.ReadToEnd();
var err = p.StandardError.ReadToEnd();
Console.WriteLine("O: " + output);
Console.WriteLine("E: " + err);
}
catch (Exception e) { Console.WriteLine(e.Message); ; }
It return err = Unexpected argument. Enter '-?' for help.
I was trying set FileName on cmd.exe and move path to Arguments. But it waits forever for a response and does not exit p.StandardOutput.ReadToEnd ();
I was trying send each line of code individually, but also without success.
And I trying with /C on start p.StartInfo.Argument but it does not change anything.
Thanks to Selvin suggestion. I made it by:
string sqlConnectionString = #"Integrated Security=SSPI;Persist Security Info=False;Data Source=InstanceName";
string script = File.ReadAllText(#"../../sql.sql");
Microsoft.Data.SqlClient.SqlConnection conn = new Microsoft.Data.SqlClient.SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
where in sql.sql I added all my sql line.

Execute jar file in c#

I have a problem with executing the jar file in c#.
This jar file is epubcheck.jar
here is my code to run the file
public string IdpfValidateEpub(string epub)
{
try
{
string result = null;
string epubCheckPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "epubcheck.jar");
string arguments = "java -jar" + " \"" + epubCheckPath + "\"" + " \"" + epub + "\"";
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = #"cmd.exe";
//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = arguments;
Debug.WriteLine("arguments: " + pProcess.StartInfo.Arguments);
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.CreateNoWindow = false;
//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;
//Start the process
pProcess.Start();
//Get program output
result = pProcess.StandardOutput.ReadToEnd();
//Wait for process to finish
pProcess.WaitForExit();
return result;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
throw ex;
}
}
But the program is not running at all. I also set the property to show the window. So i can see if its running or not. But it only shows the command prompt like this
I also printed the arguments i passed in the System.Diagnostics.Process in order to check if the arguments are correct.
After the program printed the arguments. i just copied it and paste in the command prompt. And the program works as expected. But why does it doesn't work in my c# code?
Thank you so much.
Java is a program in its own right, so you don't actually need cmd.exe to run it.
Change your arguments to the following so that you're passing the arguments for java:
string arguments = "-jar" + " \"" + epubCheckPath + "\"" + " \"" + epub + "\"";
And then simply start java instead of cmd:
pProcess.StartInfo.FileName = #"java";

simple script for generating archives arj

here's code:
string command = string.Empty;
DirectoryInfo dInfoForTesting = new DirectoryInfo(#"E:\TestFiles");
FileInfo[] files = dInfoForTesting.GetFiles();
foreach (FileInfo file in files)
{
string rndPswd = "134";
command = "arj a -m1 -g" + rndPswd + " " + file.Name + ".arj " + file.FullName;
Process.Start(#"C:\ARJ_TEST\programming\test\ARJ.EXE", command);
}
I want to create automatically archives protected by simple password 134 uisng ARJ archiver. But when i run this code for the first file i see the image below. But this file exists!
Please, help to understand me, what i am doing wrong.
EDIT:
i 've decided to rewrite code to run cmd in the folder E:\TestFiles, where there are all files for testing:
ProcessStartInfo startInfo = new ProcessStartInfo
{
WorkingDirectory = #"E:\TestFiles",
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardInput = false
};
foreach (FileInfo file in files)
{
string rndPswd = "134";
command = "arj a -m1 -g" + rndPswd + " " + file.Name + ".arj " + file.Name;
startInfo.Arguments = command;
Process.Start(startInfo);
}
For example, if i run command arj a -m1 -g134 10.arj 10 directly from cmd, arj creates an archive 10.arj. But when i run my code for the first file (10) nothing happens...

Copying EXIF from One image to another using EXIF Tool Taking too long

I'm using the following code to copy the exif data from one file to another using exiftool.
When directly run from command line,the tool is quick.But when the command is executed from a winform app,it takes much greater time.What is going wrong here? Please advice. Is there any alternative way to achieve this?
foreach(string cpath in filelist)
{
string path = "-overwrite_original -TagsFromFile " + "\"" + file + "\"" +" "+ "\"" + outdir + "\\" + Path.GetFileNameWithoutExtension(file) + ext + "\"";
runCmd(path);
}
public void runCmd(string command)
{
ProcessStartInfo cmdsi = new ProcessStartInfo("exiftool.exe");
cmdsi.WindowStyle = ProcessWindowStyle.Hidden;
cmdsi.Arguments = command;
Process cmd = Process.Start(cmdsi);
cmd.WaitForExit();
}

Execute a exe file with arguments using cmd in winforms

I am developing a windows application.I am calling a command prompt,I need to call exe file that exe file take parameter.
I am able to open command prompt but not able to send the parametrs
string strCmdText = "create-keyfile.exe ..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
Can please help me out.
Thanks
Punith
System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo("create-keyfile.exe");
startInfo.Arguments = "..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start(startInfo);
You can also see MSDN site on Process.Start, there are examples on how to execute .exe and pass arguments to it.
ProcessStartInfo process = new ProcessStartInfo();
process.FileName = "yourprogram.exe";
process.Arguments = strCmdText; // or put your arguments here rather than the string
Process.Start(process);
have you tried
System.Diagnostics.Process.Start("CMD.exe "+strCmdText);
Actually on further inspection I don't think you need to call CMD.EXE
You should be calling your exe file, unless of course you are using CMD to to display something
string strCmdText = "..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start("create-keyfile.exe", strCmdText);
Have you tried cmd with /k or /c option
From the link
/c : Carries out the command specified by string and then stops.
/k : Carries out the command specified by string and continues.
string strCmdText = "/k create-keyfile.exe ..\\KatanaFirmware\\key-template.txt ..\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try this.
string strCmdText = "KatanaFirmware\\key-template.txt "+ "\\Keyfiles\\new-keyfile.txt " + cableTextBox.Text.Trim()+" " + startIdTextBox.Text.Trim();
Process mp = new Process();
mp.StartInfo.FileName = "E:\\create-keyfile.exe"; //path of the exe that you want to run
mp.StartInfo.UseShellExecute = false;
mp.StartInfo.CreateNoWindow = true;
mp.StartInfo.RedirectStandardInput = true;
mp.StartInfo.RedirectStandardOutput = true;
mp.StartInfo.Arguments = strCmdText;
mp.Start();
mp.WaitForExit();
Hope it helps.

Categories