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

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

Related

how to give address of parts to copy command in c#

when i run copy command in my c# code it produce no error or exception because it is not finding the parts path i do not know how to give full directory path or path of every part which i am joining.actually i am merging file parts to a single file using coy/b by using this code...
string strCmdText;
strCmdText = "/C copy/b test.txt.10485760.0000.part" +
"test.txt.10485760.0001.part" +
"test.txt.10485760.0002.part" +
"test.txt.10485760.0003.part" +
"test.txt.10485760.0004.part" +
"test.txt.10485760.0005.part test.txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
You can specify the path of your files as the working path of the process. For example:
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = #"THE\PATH\OF\FILES",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
Arguments = "YOUR COMAND HERE";
};
Process process = Process.Start(startInfo);
For your command, note that you can copy all files into one using a wildcard for the parts:
copy *.part test.txt
/b is for binary data, so i think is not needed in your case.
You can also set other properties for a process, for more info check the doc: ProcessStartInfo.
For such a complex task, I would use the process as a bash rather then just an execution tool.
Create a Process
Redirect it's streams (Input, Output)
With a StreamReader and StreamWriter u can now access the cmd
Now just control it the way u need it, like setting the working directory to the path and do ur command.
If this is not what u want, u can allways set the working directory on the ProcessStartInfor -> Link

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 + "\"";

Having an issue with running an external programs arguments in C#

I have the following command which works fine in Perl
my $bugcmd = `"C:\\Program Files (x86)\\McAfee\\VirusScan Enterprise\\scan32.exe"
$source /all /archive /loguser /prompt /log $path$itemID.txt /autoexit`;
However in C# I cannot for the life of me get the damn thing to create a log file in C# it scans it just doesn't create a log, any help would be appreciated.
string log = txtSource.Text + itemID + ".txt";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files (x86)\McAfee\VirusScan Enterprise\scan32.exe";
startInfo.Arguments = drive + " /all /archive /loguser /prompt /autoexit /log" + log;
Process.Start(startInfo);
The path and the itemID look fine in the debugger. C:\Temp\itemID.txt
Using string.Format might help expose where the problem is (you're missing a space after \log). I think this might help:
startInfo.Arguments =
string.Format("{0} /all /archive /loguser /prompt /autoexit /log {1}", drive, log);
Sometimes it's a good idea to ensure that the log path is surrounded by quotes as well, if you're reading it from a .config or some other place that you don't have control, since it may have spaces in it. This might also apply to drive?
To handle that case:
startInfo.Arguments =
string.Format("\"{0}\" /all /archive /loguser /prompt /autoexit /log \"{1}\"",
drive, log);

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

7z command line to compress folders

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>

Categories

Resources