Whitespaces at the Process Arguments - c#

I am trying to create a Visual Studio Add-In by using Visual Studio 2015, VSIX project. The add-in should be able to run an .exe file along with current project path. Currently i can find the path of the project and give it as argument of the Process (the .exe file).
The .exe file is run by cmd as follows:
the.exe -d projectDirectoryPath
Detecting the project path is not my problem but, if the path contains Space Character , it is understood as a seperate argument.
Following code is what i've used for starting the process:
string myDir = activeProjectPath();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = pathOfExe + "\\the.exe";
startInfo.Arguments = "-d " + myDir;
The code works fine when the path of the current project does not contain Space Character, otherwise process can not started. The process gives following error:
Given args:-d C:\Users\theUser\Documents\Visual Studio 2015\Projects\testProject\testProject
Too many arguments specified in your command line! Skipping extra argument: Studio
Too many arguments specified in your command line! Skipping extra argument: 2015\Projects\testProject\testProject
Note: When i try to run the.exe from cmd, it accepts space characters in the path of the projects.
How can i solve this issue?
Thanks!

Try to use " character. You can specify the path in the following form:
"C:\Users\foo\My Documents\file name.sln", for example.
You can surround any path with " character, like in batch or Powershell script.
startInfo.Arguments = string.Concat("-d ", "\"", myDir, "\"");

Related

Call to Process works fine with Debug, but it doesn't work in the installed application

I am developing a Windows Form program that has callings to ffmpeg library through the class Process.
It works fine when I run it with the Debug in Visual Studio 2013. But when I install the program and I invoke the operation that call to the ffmpeg Process, it doesn't work. The cmd screen appears an disappears and nothing happens.
I have tried to know what can be happening getting a log file with the output of ffmpeg, in case it was a problem in the ffmpeg libraries. However, after executing it the log is empty, what means that the ffmpeg command has not been executed.
Can someone help me, please?
The code is this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + ffmpegPath + " " + commandArguments;
using (Process processTemp = new Process())
{
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
processTemp.Start();
processTemp.WaitForExit();
}
I am invoking to cmd.exe (not directly ffmpeg.exe) because in the arguments sometimes there can be a pipe (that is why the command starts with "/c").
Are you sure this isn't a privileges issue when trying to execute the cmd.exe (e.g you need administrator privileges)
try adding
startInfo.Verb = "runas";
Paul
Hmm its not a path issue with spaces in file/directory names is it? For ffmpegPath or one of your command parameters (if a file path). Surround all file paths with ' like below.
Try surrounding any file paths with '
startInfo.Arguments = "/c '" + ffmpegPath + "' " + commandArguments;
Also you could try adding /K to the cmd command call to stop if from closing the command prompt when it finishes. It might tell you the error before it closes the window but you wont see it if it closes so quickly
Good luck :)
Paul

Opening pdf files using Process.Start

I am trying to open PDF files in Adobe reader using C#'s Process.Start().
When I provide a path without white spaces it works fine, but paths and pdf files containing white spaces don't open.
This is my code:
Button btn = (Button)sender;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "AcroRd32";
string s = btn.Tag.ToString();
//btn.Tag Contains the full file path
info.Arguments = s;
Process.Start(info);
If it is C:\\Users\\Manish\\Documents\\ms_Essential_.NET_4.5.pdf it works fine but if it is F:\\Tutorials\\C#\\Foundational\\Microsoft Visual C# 2012 Step By Step V413HAV.pdf Adobe Reader gives an error saying there was an error in opening the document file can't be found.
I have read through many questions related to this topic in SO but it won't work. As I can't figure out how to apply # prefix in my string s.
Any ideas how to fix this?
Just a little trick there is a default PDF reader set on the client: just use the file name as FileName if the process. Usually you don't care which program to use, so then this solution just works:
Process.Start(pdfFileName);
This doesn't need special quoting too, so it instantly fixes your problem.
Try to wrap the arguments around quotes:
info.Arguments = "\"" + s + "\"";
Using the character # before the string value should work:
var path = #"F:\Tutorials\C#\Foundational\Microsoft Visual C# 2012 Step By Step V413HAV.pdf";
You should enquote the path provided in the argument list. This will cause it to view the path as a single argument instead of multiple space separated arguments:
info.Arguments = "\"" + s + "\"";

C# archiving using 7zip, keeps hanging on wildcard call

Hey all quick question for you. I am trying to archive a directory of files into a zip folder using 7zip as a system process but I am running into an odd error. Whenever I do a wildcard specification in my source file name such as "*.txt" it works fine. However, once I use the wildcard * around the actual file name (Which I need to and can't work around that), 7zip just seems to hang. I can see the archive directory get created in the folder but I can't open it and I have to close 7zip down via task manager.
Any advice?
Code is below:
public static void archiveFiles(string executionDirectory,string workDirectory,
string[] files)
{
string sourceName = #"C:\mypath\*testfile*"; <----// This seems to be my issue
string targetName = #"C:\\testcompress\archive.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"C:\program files\7-zip\7z.exe";
p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
You're most likely getting this issue because your path #"C:\mypath*testfile*" is a verbatim string, thus using the "*" character, or other such wildcards, are being taken as literal in the path instead of wildcards. Trying taking the # off the beginning of strings containing wildcards and that should fix your problem. You can find a more thorough explanation of what the "#" character, used in context of a prefix to strings, does here:
Verbatim Strings
I think you are having issues with your \'s as well as the fact that you should be using 7za.exe and not 7z.exe. Make sure your application has access to write to the directory in question.
string sourceName = #"C:\mypath\*testfile*";
string targetName = #"C:\testcompress\archive.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"C:\program files\7-zip\7za.exe";
p.Arguments = string.Format ( "a -t7z {0} {1} -mx=9",targetName,sourceName);
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
console.writeline(string.format("7zip returned with exit code {0}",x.ExitCode));
7zip exit codes can be found here
Remember you can test it in a command prompt window to be sure that it works.
C:\program files\7-zip\7za.exe a -t7z C:\program files\7-zip\7za.exe C:\mypath*testfile* -mx=9

Calling G++ from C# Program

I've made a C++ code editor in C# and now am trying to invoke g++ to compile the source files. So I've copied the TDM-GCC installation besides my program and wrote a small batch script to call that.
#echo off
#set PATH="%~dp0TDM-GCC-32\bin\";%PATH%
call "%~dp0TDM-GCC-32\mingwvars.bat"
cd %1
"%~dp0TDM-GCC-32\bin\g++.exe" %2 -o %3
And from C# code, I'm trying to call this script by using CMD like in this script of code.
string fileName = Path.GetFileName(CurrentFile);
string exeName = Path.GetFileNameWithoutExtension(CurrentFile) + ".exe";
string workingDir = Directory.GetParent(CurrentFile) + "";
string compile = Directory.GetParent(Application.ExecutablePath) + "\\compile.cmd";
File.Delete(Path.Combine(workingDir, exeName));
StartProcess(true, "cmd", "/c", "\"" + compile + "\"", workingDir, fileName, exeName);
And here is the StartProcess method:
void StartProcess(bool hidden, string command, params string[] args)
{
ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = command;
pStartInfo.Arguments = string.Join(" ", args);
pStartInfo.UseShellExecute = false;
if (hidden)
{
pStartInfo.RedirectStandardError = true;
pStartInfo.RedirectStandardOutput = true;
pStartInfo.CreateNoWindow = true;
}
Process proc = new Process();
proc.StartInfo = pStartInfo;
proc.Start();
logBox.Clear();
if (hidden)
{
while (!proc.StandardError.EndOfStream)
{
logBox.AppendText(GetTimestamp() + " Error: " + proc.StandardError.ReadLine() + Environment.NewLine);
}
}
}
Update: 26-9-14
I doubted whether the batch file is ever called, because if I call it from cmd, it simply works. So I tried to echo to the standard error from the batch file like this:
echo %PATH% 1>&2
And I can see that the GCC's bin folder is in the path too, but for some reason, the exe is not getting created. But sometimes, the script works, and the exe gets created.
But whenever I execute this, there will be nothing in the LOG and also, no executable is being created. I know that the TDM-GCC's bin folder must be in the PATH, but that is what the second line of the batch script needs to do. Dunno what this is and why this error.
End of update
Any suggestions on how can I get it working?
Thanks.
I don't know if anything written below really helps on this issue. But at least those hints should be useful in any case.
I suggest for the batch file
#echo off
call "%~dp0TDM-GCC-32\mingwvars.bat"
set "PATH=%~dp0TDM-GCC-32\bin;%PATH%"
cd /D %1
"%~dp0TDM-GCC-32\bin\g++.exe" %2 -o %3
PATH is modified after execution of mingwvars.bat. It could be that this batch file also modifies PATH. Or it runs commands like find.exe with the executables expected in %SystemRoot%\System32, but perhaps also existing in directory bin. I have seen already several times not working logon batch scripts because PATH on client computer contained as first folder path the bin directory of a compiler ported from Unix to Windows with executables also found in %SystemRoot%\System32, but working completely different as ported from Unix.
Folder paths should be added to environment variable PATH always without double quotes even if the folder path contains 1 or more spaces. The double quotes used in third line just make sure that a trailing space is not added additionally to PATH and that command set works even with not typical folder paths like an ampersand in path.
And folder paths should be added to environment variable PATH without a trailing backslash.
On command cd the parameter /D is additionally used in case of a change to a different drive must be performed, too. The command cd would not change the current directory if the specified path is on a different drive without parameter /D.
In C# code you have to make sure that workingDir, fileName and exeName are finally on execution of cmd.exe enclosed in double quotes in the arguments string as Erti-Chris Eelmaa already wrote.
And it would be perhaps better to read in your C# application the value of environment variable ComSpec and use this value instead of just cmd for executing the batch file.
This might, or might not be part of the problem, but you don't take care of spaces.
pStartInfo.Arguments = string.Join(" ", args);
you probably would want something like this:
pStartInfo.Arguments = string.Join(" ", args.Select(x => "\"" + x + "\""));

Exception: string filename too long (Process)

I am using the code below to pass an argument to a process
ProcessStartInfo StartInfo = new ProcessStartInfo();
StartInfo.FileName = HttpContext.Current.Server.MapPath(#"\highcharts\phantomjs.exe");
StartInfo.Arguments = HttpContext.Current.Server.MapPath(#"\highcharts\highcharts-convert.js");
StartInfo.Arguments += #" -outfile " + path + #"\chart" + templateData[i].ReportTemplateChartId + ".png -width 800 -infile " + chartoptions1;
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.CreateNoWindow = false;
In chartoptions1 I was using a JSON string, but it throws an exception saying string filename too long, so I created a text file and tried to pass it, but the chart image is not generated.
From this MSDN page,
On Windows Vista and earlier versions of the Windows operating
system, the length of the arguments added to the length of the full
path to the process must be less than 2080. On Windows 7 and later
versions, the length must be less than 32699.
Did you check the length of StartInfo.Arguments ?
On the other part, a quick look at this wiki page on Github,
-infile: The file to convert, assumes it's either a JSON file, the script checks for the input file to have the extension '.json', or
otherwise it assumes it to be an svg file.
So, -infile should be a file and not a JSON content. I suppose your chartoptions1 is quite large, so you have the error message string filename too long.
Highcharts may also check the length of the infile path (256 ?)
The running script should have at least read access to the infile and should be resolvable ; especially if you don't specified a working directory, you have to add the full path with infile and not just the filename.
To debug, a working directory issue, simply test with a constant path such as c:\test

Categories

Resources