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
Related
I am currently trying to open a PDF file on page 16 using System.Diagnostics.Process.Start, but it won't pick up my file path. Here is my file path that I wish to open from C:\Users\ipadc\Desktop\projek\Bookstore Bargainer System (4 Sept)\BookstoreBargainerSystem\bin\Debug\Pdfs\User_Manual. Iit is stored in my Application.StartupPath.
Here is the code i have tried but it says that it cannot find the path.
System.Diagnostics.Process.Start(
"Acrobat.exe /A \"page=16\" \""+Application.StartupPath+ "\\Pdfs\\User_Manual.pdf");
but it simply says The file does not exist.
Anybody knows why it cannot find this file. The file is an Adobe Acrobat type saved as .pdf.
Your arguments cannot be part of the process.start. This takes in the executable name.
The file name and arguments are different and need to be separated.
Process acro = new Process();
acro.StartInfo.FileName = "Acrobat.exe"
acro.StartInfo.Arguments = "/A \"page=16\" \""+Application.StartupPath+ "\Pdfs\User_Manual.pdf\""
acro.Start();
I also added an extra quote at the end of your path.
You can also do this instead, notice the arguments are a 2nd parameter of start here:
Process.Start("Acrobat.exe", "/A \"page=16\" \""+Application.StartupPath+ "\Pdfs\User_Manual.pdf\"");
And path the Path.Combine recommendation added (Marco's comment is right).
Process.Start("Acrobat.exe",
"/A \"page=16\" \"" +
System.IO.Path.Combine(Application.StartupPath, "Pdfs", "User_Manual.pdf")
+ "\"");
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, "\"");
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 + "\"";
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
I am trying to zip/7z folders using the command line of 7zG.exe. The code I have works for files but not folders. Could someone please show me the correct way using 7z command line to compress folders? Here is the sample code that works for files only. Whenever I try running this code 7zip shows a messagebox saying "Invalid Parameter"
string sourceName = "Folder\Folder1";
string targetName = "Example.gz";
// 1
// Initialize process information.
//
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7zG.exe";
// 2
// Use 7-zip
// specify a=archive and -tgzip=gzip
// and then target file in quotes followed by source file in quotes
//
p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
// 3.
// Start process and wait for it to exit
//
Process x = Process.Start(p);
x.WaitForExit();
as stated in to comment section, you are supposed to use 7za.exe
This link gives you a complete example line
Your code will look like this:
string sourceName = "Folder\Folder1";
string targetName = "Example.gz";
ProcessStartInfo p = new ProcessStartInfo();
//first change
p.FileName = "7za.exe";
//second change
p.Arguments = "a -tzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
gzip as well as bzip2 are only compression algorithms and cannot be used to compress a file-system structure (e.g. folders, folders with files etc.).
In fact, they are usually preceded by tar compression (that support folders), to get the famous (in particular in unix-based systems) tar.gz and tar.bz2 archives.
In your case you can use -tzip or -t7zip to directly compress a folder:
p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
By the way, you should use 7za.exe instead of 7zG.exe since the latter is the GUI module, while the former is the command-line standalone version of 7zip (i.e. it does not depend on any other dll), as stated in 7zip manual:
7z.exe is the command line version of 7-Zip. 7z.exe uses 7z.dll from
the 7-Zip package. 7z.dll is used by the 7-Zip File Manager also.
7za.exe (a = alone) is a standalone version of 7-Zip. 7za.exe supports
only 7z, lzma, cab, zip, gzip, bzip2, Z and tar formats. 7za.exe
doesn't use external modules.
You can find 7za.exe in the extra package, for example for version 9.22 you can find it in the archive called 7z922_extra.7z (link).
try with this command:
7za -tzip <archive-name> <folder-name>