How to save output of FINDSTR from C# to a text file? - c#

I am trying to run the findstr command from C# (Windows Forms).
I have tried this normally in Command Prompt it works fine.
string CD = #"P:\FIles";
Process p = new Process();
p.StartInfo = new ProcessStartInfo("findstr.exe");
p.StartInfo.Arguments = "-M -S" + " " + quote + txtSearch.Text + quote + " " + quote+"dummy.txt"+quote + " > " + "C:\\Temp\\results.txt" ;
p.StartInfo.WorkingDirectory = CD;
p.StartInfo.ErrorDialog = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
I would like to save the output to another text file with a specific location.
It would be even better if I could somehow return the result directly back to the form it self and maybe copy each line to a list box.

You can read the output of a console application with reading the "StandardOutput" stream. But you have to set the StartInfo.RedirectStandardOutput property to "true" before.
In your case:
string CD = #"P:\FIles";
Process p = new Process();
p.StartInfo = new ProcessStartInfo("findstr.exe");
p.StartInfo.Arguments = "-M -S" + " " + quote + txtSearch.Text + quote + " " + quote+"dummy.txt"+quote + " > " + "C:\\Temp\\results.txt" ;
p.StartInfo.WorkingDirectory = CD;
p.StartInfo.ErrorDialog = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
string sTest = p.StandardOutput.ReadToEnd();

Related

How can I catch all exceptions in C# from libtiff.net TIFFCP.exe

How can I catch all exceptions in C# from libtiff.net TIFFCP.exe
I would like to get exceptions from TTIFFCP.exe(Merge, Split) such as DirectoryNotFoundException, FileNotFoundException ...
I can see these errors on visula studio debugger window but it does not pass catch block.
I tried like these(I made an error deliberately)
<pre><code>
<hr/>
Code A :
string[] arguments =
{
#"Sample Data\EEEEEEE.tif",
#"Sample Data\marbles.tif",
"MergeTiffImages.tif"
};
TiffCP.Program.Main(arguments);
===> nothing return
<hr/>
Code B :
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = #"TiffCP.exe";
string path1 = #"Sample Data\EEEEEEE.tif";
string path2 = #"Sample Data\marbles.tif";
string path3 = "MergeTiffImages.tif";
p.StartInfo.Arguments = "\"" + path1 + "\"" + ", \"" + path2 + "\"" + ", \"" + path3 + "\"";
p.Start();
string t = p.StandardOutput.ReadToEnd();
===> string t => ""
<hr/>
Code C :
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(#"TiffCP.exe");
string path1 = #"Sample Data\EEEEEEE.tif";
string path2 = #"Sample Data\marbles.tif";
string path3 = "MergeTiffImages.tif";
myProcessStartInfo.StartInfo.Arguments = "\"" + path1 + "\"" + ", \"" + path2 + "\"" + ", \"" + path3 + "\"";
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardError;
string t = myStreamReader.ReadLine();
myProcess.Close();
===> string t => Open: Failed to open "Sample Data\EEEEEEE.tif"
</code></pre>
but it does not pass catch block
Is it possible to get Errors from TIFFCP.exe? thanks in advance.
As I can see you tried two different methods. As others said, Code B won't give you any exception as you can't catch those of a different process.
Code A if you look at the source returns nothing. So it is normal. From there you have two choices: either you copy the code from the main method of this source and manage yourself exceptions OR you get the output and manage errors from there.

cmd process won't execute correct command

I'm working on a project where you "send" a command to the cmd.exe and receive the output. For this command you need a file path -k and an url.
I have the following code (names and values changed):
string path = "C:\Users\program.exe"
string pathcustom = "\"" + path + "\""; //the path needs to be in quotation marks
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
string Address = "1.2.3"
string command = pathcustom + " " + "-k" + " " + "https://username:passwort#serveradress" + Address; //Serveradress is the URL
p.StartInfo.Arguments = "/C " + command;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string ReturnValue = p.StandardOutput.ReadToEnd();
This Code is working fine like I want it to be, but I need another methode thats exactly similar except that the Address looks different. In the code above it would look something like 1.2.3 but int the following method the Address has to look like this (including the backslashes and quotation marks) \"ab:cd:de\" so let's pretend this is
string path = "C:\Users\program.exe"
string pathcustom = "\"" + path + "\"";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
string Address = #"\""ab:cd:de\""";
string command = pathcustom + " " + "-k" + " " + "https://username:passwort#serveradress" + Address;
p.StartInfo.Arguments = "/C " + command;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string ReturnValue = p.StandardOutput.ReadToEnd();
When I rewrite the code so that the cmd stays open, with the first method I get the ouput I want/expect. But with the second, not working, method, it sends the command to the cmd and executes it, but it writes as "message" that the command was either written wrong or it couldn't be found. But when i take exactly the same code (via streamwriter I write the command for the cmd into a textfile) and copy it into the cmd, it executes it like it should. So basically, it just doesn't work if I execute the command via c#. Please help
You have to wait for the application to exit
Use something like p.WaitForExit(milliseconds)
Or check p.HasExited
According to this MSDN Post, in order for an argument in StartInfo.Arguments to keep the quotes, you need to "triple escape it" , like this:
string Address = "\\\"\"\"ab:cd:de\\\"\"\";
string command = pathcustom + " " + "-k" + "https://username:passwort#serveradress" + Address;

How can I archive multiple Folders into a single Zip File using 7zip C#?

I have Multiple folder path:
SourceFilePath="C:\Users\Anuj\Desktop\PSI"
SourceFilePath1="C:\Users\Anuj\Desktop\Google"
SourceFilePath2="C:\Users\Anuj\Desktop\Isp"
I want to Compress These path Using 7zip Command line code along with a Zip Password.
Edit:
//Declare and instantiate a new process component.
System.Diagnostics.Process proc;
proc = new System.Diagnostics.Process();
//Do not receive an event when the process exits.
proc.EnableRaisingEvents = false;
//The "/C" Tells Windows to Run The Command then Terminate
string strCmdLine;
strCmdLine = "/C cd c:\\Program Files\\7-Zip\\ ";
strCmdLine += " & 7z a "// here i need help
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
proc.Close();
What I actually Did:
var MultiplePathFolders=SourceFilePath+SourceFilePath1+SourceFilePath2
System.Diagnostics.Process proc;
proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = "/C cd c:\\Program Files\\7-Zip\\ ";
strCmdLine += " & 7z a " + SyncPath + "\\" + ZipName + "-FileName.7z " + MultiplePathFolders + " -p" + DecryptedPassword + "";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
proc.Close();
You can do like this:
string yourPassWord = "Hello";
string yourZipPath="D:\\my7z.7z"; // Your 7z file path
//string yourZipPath="D:\\myZip.zip"; // Or your zip file path
System.Diagnostics.Process proc;
proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
List<string> foldersAndFiles = new List<string>();
foldersAndFiles.Add("D:\\F1"); // Add folder
foldersAndFiles.Add("D:\\F2"); // Add folder
foldersAndFiles.Add("D:\\file.txt"); // Add file
string cmd = "a -tzip -p\"" + yourPassWord + "\" \"" + your7zPath + "\"";
foreach(var item in foldersAndFiles)
{
cmd += " \"" + item + "\"";
}
proc.StartInfo.FileName
= "c:\\Program Files\\7-Zip\\7z.exe"; // Be sure this file exist.
proc.StartInfo.Arguments = cmd;
proc.Start();
proc.Close();

process.StandardOutput.ReadToEnd returns null

I have this code:
in c# windows form works right...but in asp.net its return null
var tempCadena = HttpContext.Current.Server.MapPath("~/" + HttpContext.Current.Session["db"] + "/" + "cadena");
System.IO.File.WriteAllText(tempCadena, cadena);
// Digestion SHA1
var tempSha = HttpContext.Current.Server.MapPath("~/" + HttpContext.Current.Session["db"] + "/" + "sha");
var opensslPath = HttpContext.Current.Server.MapPath("~/openssl/bin/openssl.exe");
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.StartInfo.FileName = opensslPath;
process.StartInfo.Arguments = "dgst -sha1 " + tempCadena;
process.StartInfo.UseShellExecute = false;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string codificado = "";
codificado = process.StandardOutput.ReadToEnd();
process.WaitForExit();
codificado = null I tried many things and nothing...can someone help me please?
I tried to change Process to ProcessStartInfo and doesn't work
I found it:
the: tempCadena has the value to the directory:
D:\ShamTec MySQL\
and it has a space in the folder name
that's why the process returns error
thanks

ffmpeg c# asp.net help on ProcessStartInfo

I wrote the code to convert the file in c#asp.net using ffmpeg. While executing it shows error as "The filename, directory name, or volume label syntax is incorrect" but my paths are correct. So what is the solution for this?
ProcessStartInfo info = new ProcessStartInfo("e:\ffmpeg\bin\ffmpeg.exe", "-i cars1.flv -same_quant intermediate1.mpg");
process.Start();
I tried another way as shown below. But that is also won't works.
ProcessStartInfo info = new ProcessStartInfo("ffmpeg.exe", "-i cars1.flv -same_quant intermediate1.mpg");
Please help me with sample code for converting one video file format to another using ffmpeg in c# asp.net. Thanks in advance.
There is wrapper library available
http://www.ffmpeg-csharp.com/
Also refer
http://www.codeproject.com/Articles/24995/FFMPEG-using-ASP-NET
https://stackoverflow.com/questions/tagged/asp.net+c%23+video
string apppath = Request.PhysicalApplicationPath;
string outf = Server.MapPath(".");
string fileargs = "-i" + " " + "\"C:/file.mp4 \"" + " " + "\"C:/files/test.flv \"";
Process myProcess = new Process();
myProcess.StartInfo.FileName = Server.MapPath(".")+"//ffmpeg.exe";
myProcess.StartInfo.Arguments = fileargs;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardOutput = false;
myProcess.Start();
myProcess.WaitForExit(50 * 1000);
Thanks
Deepu
The backslashes (\) in your path are considered to start escape sequences. To prevent this, either use double backslashes (\\), or prefix the string with #.
ProcessStartInfo info = new ProcessStartInfo("e:\\ffmpeg\\bin\\ffmpeg.exe",
"-i cars1.flv -same_quant intermediate1.mpg");
Or:
ProcessStartInfo info = new ProcessStartInfo(#"e:\ffmpeg\bin\ffmpeg.exe",
"-i cars1.flv -same_quant intermediate1.mpg");
You need to have backslashes in your path see below this is how I create videos using ffmpeg.exe
string sConverterPath = #"C:\ffmpeg.exe";
string sConverterArguments = #" -i ";
sConverterArguments += "\"" + sAVIFile + "\"";
sConverterArguments += " -s " + iVideoWidth.ToString() + "x" + iVideoHeight.ToString() + " ";
sConverterArguments += " -b " + iVideoBitRate.ToString() + "k -ab " + iAudioBitRate.ToString() + "k -ac 1 -ar 44100 -r 24 -absf remove_extra ";
sConverterArguments += "\"" + sOutputFile + "\"";
Process oProcess = new Process();
oProcess.StartInfo.UseShellExecute = true;
oProcess.StartInfo.Arguments = sConverterArguments;
oProcess.StartInfo.FileName = sConverterPath;

Categories

Resources