Click here to Skip to main content
16,012,082 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I want to use the findstr utility of the CMD into C# Console Application
But I don't know where and how to start.

Please Help.
Thanks
Posted
Comments
govardhan4u 21-Feb-13 1:45am    
want to use String class or not?

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C findstr /c:" + "\"" + textfilter + "\"" + " " + sourcefilename + " > " + outputfilename;
startInfo.WorkingDirectory = directory;
process.StartInfo = startInfo;
process.Start();
 
Share this answer
 
you can execute CMD commands using Process class in C#

C#
Process p = new Process();
p.StartInfo = new ProcessStartInfo("cmd.exe");
p.StartInfo.Arguments = 'findstr /x /c:"computer help" *.txt';
p.StartInfo.WorkingDirectory = workingDirectory;
//p.StartInfo.CreateNoWindow = true;
p.StartInfo.ErrorDialog = true;
//p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
p.Close();
 
Share this answer
 
Comments
lee gee 21-Feb-13 21:49pm    
Hi I have tried the one you post but it won't create a file.
Can you help me what's wrong?

string textfilter;
string directory;
string sourcefilename;
string outputfilename;
string temp;


Console.Write("Enter directory of the file: ");
directory = Console.ReadLine();

Console.Write("Enter source filename: ");
sourcefilename = Console.ReadLine();

Console.Write("Enter text filter: ");
textfilter = Console.ReadLine();

Console.Write("Enter output filename: ");
outputfilename = Console.ReadLine();

Process p = new Process();
p.StartInfo = new ProcessStartInfo("cmd.exe");
p.StartInfo.Arguments = @"findstr /c:""" + textfilter + "\"" + " " + sourcefilename + " > " + outputfilename;
p.StartInfo.WorkingDirectory = directory;
//p.StartInfo.CreateNoWindow = true;
p.StartInfo.ErrorDialog = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
p.Close();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900