I have a c# console application that copies files from different directories on the network and place them in one single location on the server (Win Server 2008 R2). When I run the application, I get "File not found - System.String[] 0 File(s) copied." message.
static void Main(string[] args)
{
string[] srcPath =
{
#"\\sharedloc1\HR\Org Docs",
#"\\sharedloc2\MKT\Org Docs"
};
string desPath = #"C:\Users\James\Desktop\my docs";
foreach (string d in srcPath)
{
xcopy(srcPath, desPath + #"\");
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
private static void xcopy(string[] SrcLoc, string FnlLoc)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "copyFiles";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "\"" + SrcLoc + "\"" + " " + "\"" + FnlLoc + "\"" + #" /d /y";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch (Exception exp)
{
throw exp;
}
}
We have around 15 directories to loop through.
This is the problem:
foreach (string d in srcPath)
{
xcopy(srcPath, desPath + #"\");
}
You should be using d inside the foreach:
foreach (string d in srcPath)
{
xcopy(d, desPath + #"\");
}
You then need to change the xcopy method to take in a string instead of a string[].
When you do the following:
startInfo.Arguments = "\"" + SrcLoc + "\"" + " " + "\"" + FnlLoc + "\"" + #" /d /y";
You are converting the String[] to a String (the runtime will call .ToString() on SrcLoc). This is how it ends up with System.String[] in your process arguments.
Also, this block of code does nothing except destroy the stack trace.
catch (Exception exp)
{
throw exp;
}
If you want to re-throw and error, you should just do throw;.
Related
I am running doors.exe using Process class. When I run it in the console application, there is no problem, but the program hangs in the web application. What could be the reason for this?
here is my code below;
public string ExecuteDoorsScript(string dxlScriptPath, string userName, string password)
{
//Start the process
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = #"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe";
info.Arguments = "-b \"" + dxlScriptPath + "\" -u " + userName + " -P " + password + "-W -d 1234567#databasepath.it";
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
var process = Process.Start(info);
//Wait for the window to finish loading.
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
//Wait for the process to end.
process.WaitForExit();
if (!String.IsNullOrEmpty(error))
{
throw new Exception("Doors run dxl error: " + error);
}
return output;
}
my dxl script;
string topfoldername = "/Test"
void listIndices(Folder parent, int level)
{
Item i
Folder f
for i in parent do {
f = folder(i)
cout << (uniqueID i) ";" (name i) ";" ((type i)) ";" "\n"
}}
listIndices(folder topfoldername, 1)
I tried to call a .exe program I have calling cmd from a c# script that generates a command. When I run the code I get the output the process tried to write to a nonexistent pipe. But when I run said command using a cmd open from Windows it runs perfectly. What I am doing wrong? I am using Visual Studio 2022.
SilentMkdir(baseDir + "/00_CameraInit");
string binName =pre + binDir + "\\aliceVision_cameraInit.exe";
string dstDir =pre + baseDir + "/00_CameraInit\\";
string cmdLine = binName;
cmdLine = cmdLine + " --defaultFieldOfView 45.0 --verboseLevel info --sensorDatabase \"\" --allowSingleView 1";
cmdLine = cmdLine + " --imageFolder \"" + pre + srcImageDir + "\"";
cmdLine = cmdLine + " --output \"" + dstDir + "cameraInit.sfm\"";
Console.WriteLine(cmdLine);
var processInfo = new ProcessStartInfo("cmd.exe", cmdLine);
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.CreateNoWindow = false;
//processInfo.RedirectStandardOutput = !string.IsNullOrEmpty("output.txt");
int exitCode = -1;
string output = null;
try
{
var process = Process.Start(processInfo);
process.Start();
output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
process.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine("Run error" + e.ToString()); // or throw new Exception
}
I am making a program that seeks out secured PDFs in a folder and converting them to PNG files using ImageMagick. Below is my code.
string WorkDir = #"C:\Users\rwong\Desktop\TestFiles";
Directory.SetCurrentDirectory(WorkDir);
String[] SubWorkDir = Directory.GetDirectories(WorkDir);
foreach (string subdir in SubWorkDir)
{
string[] filelist = Directory.GetFiles(subdir);
for(int f = 0; f < filelist.Length; f++)
{
if (filelist[f].ToLower().EndsWith(".pdf") || filelist[f].EndsWith(".PDF"))
{
PDFReader reader = new Pdfreader(filelist[f]);
bool PDFCheck = reader.IsOpenedWithFullPermissions;
reader.CLose();
if(PDFCheck)
{
//do nothing
}
else
{
string PNGPath = Path.ChangeExtension(filelistf], ".png");
string PDFfile = '"' + filelist[f] + '"';
string PNGfile = '"' + PNGPath + '"';
string arguments = string.Format("{0} {1}", PDFfile, PNGfile);
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe");
startInfo.Arguments = arguments;
Process.Start(startInfo);
}
}
}
I have ran the raw command in command prompt and it worked so the command isn't the issue. Sample command below
"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" "C:\Users\rwong\Desktop\TestFiles\Test_File File_10.PDF" "C:\Users\rwong\Desktop\TestFiles\Test_File File_10.png"
I looked around SO and there has been hints that spaces in my variable can cause an issue, but most of those threads talk about hardcoding the argument names and they only talk about 1 argument. I thought adding double quotes to each variable would solve the issue but it didn't. I also read that using ProcessStartInfo would have helped but again, no dice. I'm going to guess it is the way I formatted the 2 arguments and how I call the command, or I am using ProcessStartInto wrong. Any thoughts?
EDIT: Based on the comments below I did the extra testing testing by waiting for the command window to exit and I found the following error.
Side note: I wouldn't want to use GhostScript just yet because I feel like I am really close to an answer using ImageMagick.
Solution:
string PNGPath = Path.ChangeExtension(Loan_list[f], ".png");
string PDFfile = PNGPath.Replace("png", "pdf");
string PNGfile = PNGPath;
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\ImageMagick-6.9.2 Q16\convert.exe";
process.StartInfo.Arguments = "\"" + PDFfile + "\"" +" \"" + PNGPath +"\""; // Note the /c command (*)
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string err = process.StandardError.ReadToEnd();
Console.WriteLine(err);
process.WaitForExit();
It didn't like the way I was formatting the argument string.
This would help you to run you command in c# and also you can get the result of the Console in your C#.
string WorkDir = #"C:\Users\rwong\Desktop\TestFiles";
Directory.SetCurrentDirectory(WorkDir);
String[] SubWorkDir = Directory.GetDirectories(WorkDir);
foreach (string subdir in SubWorkDir)
{
string[] filelist = Directory.GetFiles(subdir);
for(int f = 0; f < filelist.Length; f++)
{
if (filelist[f].ToLower().EndsWith(".pdf") || filelist[f].EndsWith(".PDF"))
{
PDFReader reader = new Pdfreader(filelist[f]);
bool PDFCheck = reader.IsOpenedWithFullPermissions;
reader.CLose()l
if(!PDFCheck)
{
string PNGPath = Path.ChangeExtension(filelistf], ".png");
string PDFfile = '"' + filelist[f] + '"';
string PNGfile = '"' + PNGPath + '"';
string arguments = string.Format("{0} {1}", PDFfile, PNGfile);
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.EnableRaisingEvents = true;
p.StartInfo.CreateNoWindow = true;
p.startInfo.FileName = "C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe";
p.startInfo.Arguments = arguments;
p.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
//You can receive the output provided by the Command prompt in Process_OutputDataReceived
p.Start();
}
}
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string s = e.Data.ToString();
s = s.Replace("\0", string.Empty);
//Show s
Console.WriteLine(s);
}
}
This question already has answers here:
Unzip a file in c# using 7z.exe
(6 answers)
Closed 8 years ago.
string path = #"C:\Users\<user>\Documents\Visual Studio\Projects\7ZipFile\RequiredDocs\";
ProcessStartInfo zipper = new ProcessStartInfo(#"C:\Program Files\7-Zip\7z.exe");
zipper.Arguments = string.Format("a -t7z {0}.7z {0} *.txt -mx9", path);
zipper.RedirectStandardInput = true;
zipper.UseShellExecute = false;
zipper.CreateNoWindow = true;
zipper.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(zipper);
Goal: Zip all *.txt file(s) within "path" and save that zipped file within "path" and these .txt files should not be present in the "path" after zipping
When I run the code, nothing seems to happen (0 error)...
Please help!
Thank you
UPDATE: I am using 7Zip and have installed 7Zip application on Windows where this code will be used w/ .NET 3.5.
The normal way of using 7Zip from a program is to invoke 7za.exe (not the installed 7z program) and include 7za with your application.
This page has a good tutorial on how to use it. Works great every time I have needed to zip/7zip programmatically.
You could also use the ZipArchive class if you want normal zip functionality in a pure .NET way (requires .NET 4.5)
Also, your path should be in quotes in case there is a space. Note that the quotes are escaped with '\'. "" is also a valid escape sequence for a quote in C#:
string.Format("a -t7z \"{0}.7z\" \"{0}\" *.txt -mx9", path);
Here's an example from my application. This example extracts an archive but it shows you how to set up the process. Just change the command to 7z and the arguments. This example assumes you're shipping 7za.exe with your application. Good luck.
public static bool ExtractArchive(string f) {
string tempDir = Environment.ExpandEnvironmentVariables(Configuration.ConfigParam("TEMP_DIR"));
if (zipToolPath == null) return false;
// Let them know what we're doing.
Console.WriteLine("Unpacking '" + System.IO.Path.GetFileName(f) + "' to temp directory.");
LogFile.LogDebug("Unpacking '" + System.IO.Path.GetFileName(f) + "' to temp directory '" + tempDir + "'.",
System.IO.Path.GetFileName(f));
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
if (pid == PlatformID.Win32NT || pid == PlatformID.Win32S || pid == PlatformID.Win32Windows || pid == PlatformID.WinCE) {
p.StartInfo.FileName = "\"" + Path.Combine(zipToolPath, zipToolName) + "\"";
p.StartInfo.Arguments = " e " + "-y -o" + tempDir + " \"" + f + "\"";
} else {
p.StartInfo.FileName = Path.Combine(zipToolPath, zipToolName);
p.StartInfo.Arguments = " e " + "-y -o" + tempDir + " " + f;
}
try {
p.Start();
} catch (Exception e) {
Console.WriteLine("Failed to extract the archive '" + f + "'.");
LogFile.LogError("Exception occurred while attempting to list files in the archive.");
LogFile.LogExceptionAndExit(e);
}
string o = p.StandardOutput.ReadToEnd();
p.WaitForExit();
string[] ls = o.Split('\n');
for (int i = 0; i < ls.Count(); i++) {
string l = ls[i].TrimEnd('\r');
if (l.StartsWith("Error")) {
LogFile.LogError("7za: Error '" + ls[i + 1] + "'.", f);
Console.WriteLine("Failed to extract the archive '" + f + "'.");
return false;
}
}
return true;
}
I'm using c# code for making .rar file of a folder.
Code
string zipFileToWrite, folderPath;
zipFileToWrite = #"D:\jack.zip";
folderPath = #"D:\New folder";
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
MyProcess.StartInfo.WorkingDirectory = #"D:\NetworkPathChecking\FileBackup\FileBackup\bin\Debug\App_Files\";
MyProcess.StartInfo.FileName = "winrar.exe";
MyProcess.StartInfo.Arguments = "a -r " + "\"" + zipFileToWrite + "\"" + " " + "\"" + folderPath + "\"";
MyProcess.Start();
MyProcess.WaitForExit();
Now I need to specify multipule extension as filter for files to make .rar from folder.
How can I do this?
Problem is solved by using files list with *.FileExtension.
Procedure
public static string RarFilesT(string rarPackagePath, Dictionary<int, string> accFiles)
{
string error = "";
try
{
string[] files = new string[accFiles.Count];
int i = 0;
foreach (var fList_item in accFiles)
{
files[i] = "\"" + fList_item.Value;
i++;
}
string fileList = string.Join("\" ", files);
fileList += "\"";
System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
string cmdArgs = string.Format("A {0} {1} -ep1 -r",
String.Format("\"{0}\"", rarPackagePath),
fileList);
sdp.ErrorDialog = true;
sdp.UseShellExecute = true;
sdp.Arguments = cmdArgs;
sdp.FileName = rarPath;//Winrar.exe path
sdp.CreateNoWindow = false;
sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
process.WaitForExit();
error = "OK";
}
catch (Exception ex)
{
error = ex.Message;
}
return error;
}
Call Procedure
private void btnSave_Click(object sender, EventArgs e)
{
Dictionary<int, string> accFiles = new Dictionary<int, string>();
accFiles.Add(1, #"D:\New folder\New folder\*.txt");
accFiles.Add(2, #"D:\New folder\*.html");
RarFilesT(#"D:\test.rar",accFiles );
}
Now this procedure works fine.It takes full backup of folder with selected extension.