I found some command line arguments to run generate the CSSLint report in xml format. It is working fine while running through command prompt.
Arguments:
csslint --format=csslint-xml "{SourceDir}\bootstrap.css" > "C:\temp\csslint.xml"
I want to execute it through C# application. I tried the below code.
Process process = new Process()
{
StartInfo =
{
FileName = "cmd.exe",
Arguments = "csslint --format=csslint-xml " + #"""{SourceDir}\bootstrap.css""" + #" > ""C:\Temp\CssLint.xml""",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
process.Start();
process.WaitForExit();
But it is not working. Can i have a solution or idea for this issue?
Also is there any way to generate the CSSLint report for the specified directory? I want to give the directory path instead of file name.
You need to add /Kor /C to cmd to execute a process passed as a parameter, thus:
Arguments = "/C csslint --format=csslint-xml " + #"""{SourceDir}\bootstrap.css""" + #" > ""C:\Temp\CssLint.xml""",
From the documentation:
Options
/C Run Command and then terminate
/K Run Command and then return to the CMD prompt.
This is useful for testing, to examine variables
One caveat... the piping (the > "C:\temp\csslint.xml" part of your command line) is not an argument, it's a redirection.
If you are redirecting your stdout (the RedirectStandardOutput = true) from your app, you can capture it directly from C#, no need to pipe it to a file like you are trying to do: you'd need to handle the Process.OutputDataReceived event between your Start and WaitForExit calls, or read from the Process.StandardOutput stream).
As for your second question, the csslint CLI allows passing in a directory instead of a file
Related
I'm trying to write a console application that starts wsl in a background process and streams different commands to it for some automation stuff.
Important: I cannot just do wsl -- mycommand and then exit as I need wsl to stay running in the background thus I chose to keep the process alive as long as my application runs.
To start the process I'm using Process.Start
var startInfo = new ProcessStartInfo
{
FileName = "wsl",
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
var process = Process.Start(startInfo);
To execute commands I just feed them into process.StandardInput followed by \n
process.StandardInput.Write("uname -a\n");
process.StandardInput.Flush();
After that I'm able to read the commands output using e.g. process.StandardOutput.ReadLine.
The problem with the code is that I'm not able to detect when the command finished i.e. there will be no more output to capture.
uname -a outputs a single line so that case is easy to handle but what if the command writes multiple lines to stdout?
Is there a way to solve this or is my approach to do this already wrong to start with?
(I need a synchronous way to get the output after executing the command as I need to evaluate it after execution)
I’m suffering with a problem for a month, I want to run the cmd command "sfc / scannow", before that I tried to read the result of this command via StandartOutput, but it’s not working with this command, I had an idea to press the button to call the console with this command and watch the result is in cmd, but I have a problem again, I just have cmd and everything is open, the command is not executed.I need to run the sfc / scannow command and see the process and the result of the check, in any way, but using C # (the project was created on win.forms)Please, help me
Also work with other cmd commands, I read through StandartOutput, but it doesn’t work with this command
string strCmdText;
strCmdText = "sfc/scannow";
Process cmdSFC = Process.Start(new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = #"C:\Windows\System32",
FileName = #"C:\Windows\System32\cmd.exe",
Arguments = "/c " + strCmdText,
});
cmdSFC.WaitForExit();
Set UseShellExecute to false. As described in MSDN:
Setting this property to false enables you to redirect input, output, and error streams.
I am trying to run logman.exe for a elevated CMD, for this below code I tried,
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"C:\Windows\System32\cmd.exe",
Arguments = "cmd /k logman.exe PerfCounterCustom | findstr \"Root\"",
Verb = "runas",
UseShellExecute = true,
}
};
try
{
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
}
Console.WriteLine("Successfully elevated!");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
And it's giving error output like,
System.InvalidOperationException: StandardOut has not been redirected or the process hasn't started yet.
at System.Diagnostics.Process.get_StandardOutput()
2 Questions,
when I am running application exe, it's showing 2 CMD window, the 1st one showing error and 2nd one showing result for argument "cmd /k logman.exe PerfCounterCustom | findstr \"Root\"" [Root Path]
how to disable showing both window?
Why I am getting this error?
To your 1st Question: In the ProcessStartInfo set WindowStyle to ProcessWindowStyle.Hidden
An alternative solution to read the output of the command is to write the output to a text file. Therefore you have to add >> "[Name or Path of file].txt" to the end of your command. Then just read the file from C# e.g. with File.ReadAllLines.
Two things to consider here:
If you do that often at Runtime and the command delivers huge amounts of text don't write it to an SSD.
Please check that the file is empty / not existing before, because Windows just appends the output to the end of the file. If you run that in multiple threads use a thread identifier in the file name.
You have to set RedirectStandardOutput of the ProcessStartInfo to true and you have to run proc.WaitForExit() before reading the output.
Please note that this solution causes incompatibilities with running the process as administrator via runas.
I would like to execute a git diff command. The following command results in an 'fatal - path is outside repository' error. It is implemented in a C# application, using the Process class.
git diff HEAD -- "Folder\TestFile.cs" >
"C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff"
fatal: C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff:
C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff is outside repository.
The command 'works', because I could successfuly execute it in a command prompt (cmd).
Could you explain what is wrong with the command and how to solve it in the C# application?
It looks like you're trying to redirect standard output to a file. Redirection is handled by the shell, and the Process class does not support it. Instead, it's passing the > C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff to the git process. That's why git is complaining that the path C:\Users\Name\AppData\Local\Temp\tmpEA7C.diff is not in a repository.
If you're invoking a process and want to read its standard output, it's your responsibility to do that.
You can capture the output with the Process class. For example:
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "git.exe",
Arguments = "diff HEAD -- "Folder\TestFile.cs"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
var line = proc.StandardOutput.ReadLine();
}
Of course, you could also use LibGit2Sharp and not have to worry about process creation and screen scraping the output.
Am a Newbie in C# and I have 3 commands(command2, command3 and command4) I need to execute in the elevated command prompt and I will also like to view the execution process as it happens. Currently, the problem is that the code below just opens the elevated command prompt and without executing the commands. I also seek better interpretations of the lines if wrong.
My code and Interpretation/Understanding of each line based on reviews of similar cases: ConsoleApp1
class Program
{
static void Main(string[] args)
{
string command2 = #"netsh wlan";
string command3 = #" set hostednetwork mode=true ssid=egghead key=beanhead keyusage=persistent";
string command4 = #" start hostednetwork";
string maincomm = command2.Replace(#"\", #"\\") + " " + command3.Replace(#"\", #"\\") ; //I merged commands 2 and 3
ProcessStartInfo newstartInfo = new ProcessStartInfo();
newstartInfo.FileName = "cmd"; //Intend to open cmd. without this the newProcess hits an error saying - Cannot run process without a filename.
newstartInfo.Verb = "runas"; //Opens cmd in elevated mode
newstartInfo.Arguments = maincomm; //I intend to pass in the merged commands.
newstartInfo.UseShellExecute = true; //
newstartInfo.CreateNoWindow = true; // I intend to see the cmd window
Process newProcess = new Process(); //
newProcess.StartInfo = newstartInfo; //Assigns my newstartInfo to the process object that will execute
newProcess.Start(); // Begin process and Execute newstartInfo
newProcess.StartInfo.Arguments = command4; //I intend to overwrite the initial command argument hereby passing the another command to execute.
newProcess.WaitForExit(); //
}
}
This is what I did to overcome the challenge and It gave me exactly what I wanted. I modified my code to use the System.IO to write directly to the elevated command prompt.
ProcessStartInfo newstartInfo = new ProcessStartInfo();
newstartInfo.FileName = "cmd";
newstartInfo.Verb = "runas";
newstartInfo.RedirectStandardInput = true;
newstartInfo.UseShellExecute = false; //The Process object must have the UseShellExecute property set to false in order to redirect IO streams.
Process newProcess = new Process();
newProcess.StartInfo = newstartInfo;
newProcess.Start();
StreamWriter write = newProcess.StandardInput ; //Using the Streamwriter to write to the elevated command prompt.
write.WriteLine(maincomm); //First command executes in elevated command prompt
write.WriteLine(command4); //Second command executes and Everything works fine
newProcess.WaitForExit();
Referrence: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx
I think an understanding of some properties of the ProcessStartInfo might clear things.
The verb - Gets or sets the verb to use when opening the application or document specified by the FileName property.,
+The UseShellExecute - Gets or sets a value indicating whether to use the operating system shell to start the process.
+The FileName - Gets or sets the application or document to start MSDN Docs
When you use the operating system shell to start processes, you can start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, by using the Process object. When UseShellExecute is false, you can start only executables by using the Process object Documentation from MSDN.
In my case, cmd is an executable. the verb property is some thing that answers the question "How should my I run my FileName(for executables e.g cmd or any application)?" for which I answered - "runas" i.e run as administrator. When the FileName is a document (e.g `someFile.txt), the verb answers the question "What should I do with the file for which answer(verb) could be -"Edit","print" etc. also?"
use true if the shell should be used when starting the process; false if the process should be created directly from the executable file. The default is true MSDN Docs - UserShellInfo.
Another thing worth noting is knowing what you are trying to achieve. In my case, I want to be able to run commands via an executable(cmd prompt) with the same process - i.e starting the cmd as a process I can keep track of.