process.start() arguments
Try fully qualifying the filenames in the arguments - I notice you're specifying the path in the FileName part, so it's possible that the process is being started elsewhere, then not finding the arguments and causing an error.
If that works, then setting the WorkingDirectory property on the StartInfo may be of use.
Actually, according to the link
The WorkingDirectory property must
be set if UserName and Password are
provided. If the property is not set,
the default working directory is
%SYSTEMROOT%\system32.
How to pass a path as an argument to Process.Start
I think the problem lies in the way you setup ProcessStartInfo
. So command
should be:
string command = @"C:\Users\Me\Desktop\TempExtract\MyApp\*.* " + @"\\TestShare\SharedFolder\Applications\ /Y /I";
and add
Xcopy.FileName = "xcopy";
this is what worked for me:
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
var command = @"C:\Users\Me\Desktop\TempExtract\MyApp\*.* " + @"\\TestShare\SharedFolder\Applications\ /Y /I";
var Processo = new Process();
var Xcopy = new ProcessStartInfo("cmd.exe")
{
Arguments = command,
FileName = "xcopy",
UseShellExecute = false
};
Processo = Process.Start(Xcopy);
Processo.WaitForExit();
}
}
System.Diagnostics.Process.Start() arguments dotnet and diff
This happens because > is not a part of the command arguments but the standard output redirection operand which is handled not by the process itself, but by the OS starting the process.
When starting a process through code, we need to handle this by ourselves.
Here is a solution working on windows:
var cmd = "diff"; //if ran under windows is the choco path: C:\\ProgramData\\chocolatey\\bin\\diff.exe
var args = "--unchanged-group-format=\"\" --old-group-format=\"\" --changed-group-format=\"%>\" --new-group-format=\"\" old.txt new.txt";
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = cmd;
p.StartInfo.Arguments = args;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
using (var outputFile = File.OpenWrite("diff.txt"))
{
p.StandardOutput.BaseStream.CopyTo(outputFile);
}
p.WaitForExit();
EDIT 1:
Having these two files (old.txt and new.txt)
old.txt new.txt
Line 1 - abc Line 1 - def
Line 2 - def Line 2 - def
Line 1 - abc Line 1 - def
Line 2 - def Line 2 - def
The output (diff.txt) is as follows:
Line 1 - def
Line 1 - def
Line 1 - def
Line 1 - def
Start A Process With Parameters
You can use this:
Process.Start("MyExe.exe", "arguments");
Process.Start and Process.StartInfo not passing arguments properly
The code you use works fine. It does send the right argument to the program. No doubt.
Some things that could go wrong:
- The file doesn't exist;
- The file doesn't exist in the current working directory, aka the place where
mame
is looking for it (you can try using an non-existing file name in the command line as a test. Is the error the same, then probably the file can't be found. Try to use the full path of the file); - Some permission problems, the error is obscured by the program.
As Sinatr suggested, you could set the current working directory using Directory.SetCurrentDirectory
. Be aware that the current working directory of this program is influenced too, so you might want to consider what you are doing.
You'd better set the working directory of the process you start, using Process.WorkingDirectory
.
C# Use Process.Start with parameters AND spaces in path
The /K argument will execute the command that follows it. It can be a whole lot of commands chained with & but, and this is what you were missing in your attempts, they need to be fully enclosed in double quotes in that case. See the SS64 cmd reference
In other words, the command should look like this:
cmd /K " here what ever you want to execute"
Because of the forward and backslashes you quickly lose track. So I took out the outer double quotes wrapping a String.Format, so it becomes a bit more clear what your actual command should be like. Here is the code that I expect should work:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd";
startInfo.Arguments = String.Format(
"/K \"{0}\"",
" \"D:\\as df\\solver\\Swag.Console.exe\" -f \"D:\\as df\\solver\\2035.swag\" -p 5555");
Process.Start(startInfo);
If Swag.Console.Exe would simply dump its arguments to the console this would be the outcome:
-f
D:\as df\solver\2035.swag
-p
5555
Process.Start arguments not working
I'm not quite sure what D:\Projects\MyProg.exe
is doing but following sample is working for. Two variable strings are declared. The two strings indicate two argument parameters I want to use with the executable.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'// Set first file parameter to the executable
Dim sourceFileName As String = "source.txt"
'// Set second file parameter to the executable
Dim targetFileName As String = "target.txt"
'// Create a new ProcessStartInfo
Dim p As New ProcessStartInfo
'// Specify the location of the binary
p.FileName = "D:\_working\ConsoleApplication3.exe"
'// Use these arguments for the process
p.Arguments = " """ & sourceFileName & """ """ & targetFileName & """ -optionalPara"
' Use a hidden window
'p.WindowStyle = ProcessWindowStyle.Hidden
' Start the process
Process.Start(p)
End Sub
End Class
See resulting screenshot:
C# process.start parameters
You can try this:
private void button4_Click(object sender, EventArgs e)
{
HttpWebRequest req =
WebRequest.Create("http://test.com/contact_ip.txt") as HttpWebRequest;
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string ResponseText = sr.ReadToEnd();
//ResponseText
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = @".\\folder\\test.exe";
proc.StartInfo.Arguments = "127.0.0.1 4455";
proc.Start();
}
}
Related Topics
Why Is Inserting Entities in Ef 4.1 So Slow Compared to Objectcontext
Pass Parameter to Eventhandler
Best Way to Call a JSON Webservice from a .Net Console
How to Find a Specific Element in a List<T>
Check If a String Contains an Element from a List (Of Strings)
Asp Net Web API 2.1 Get Client Ip Address
Restrict Access to a Specific Controller by Ip Address in ASP.NET MVC Beta
What Does the Word "Literal" Mean
How to View Msil/Cil Generated by C# Compiler? Why Is It Called Assembly
Getting Http Status Code Number (200, 301, 404, etc.) from Httpwebrequest and Httpwebresponse
What .Net Collection Provides the Fastest Search
No Connection String Named 'Myentities' Could Be Found in the Application Config File
Open Xml Reading from Excel File
What Is Quicker, Switch on String or Elseif on Type
How to Get the Child Windows of a Window Given Its Hwnd
Using Itextsharp to Extract and Update Links in an Existing PDF