Run "sw-precache" command from c# - c#

I want to generate "service-worker.js" using sw-precache
from command line (cmd.exe) I run this command and output is :
Total precache size is about 145 kB for 35 resources.
service-worker.js has been generated with the service worker contents.
When I run this command from C# output is empty, here is my code:
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
Arguments = #"/c sw-precache",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WorkingDirectory = Path.GetFullPath(myDir)
}
};
proc.Start();
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
If I run command like "dir" in Arguments = #"/c dir", output contains message from dir command,
what I do wrong?
Edit:
In Console Application, output is returned normally, but not in Web Application

WaitForExit first then read the output. It stays after the process has exited.

Related

How can we use a command with pipe in C#

I'm trying to run a command with a pipe that uses findstr.
Following is a simple example of the command. (In actual case my command is more complicated than this, using this command for explanation)
dir | findstr /i /c:"web"
following is the function I use to run the process in C#.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo()
{
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C dir | findstr /i /c:\"web\"",
RedirectStandardError = true,
RedirectStandardOutput = true
};
process.Start();
string sResponse = process.StandardOutput.ReadToEnd();
process.WaitForExit();
I haven't figured out how to run this command using this process.
Above will work for commands without | (Pipe)
Please let me know if there are any workarounds.

Executing batch file located on remote machine

I'm trying to execute a batch file that is located on a remote machine with the following line of code:
System.Diagnostics.Process.Start(\\10.0.24.103\somePath\batchFile.bat);
And it blocks on this line of code. When I try to run it manually (by writing that address in Windows Explorer), it works, but I have to accept a security warning message first. I'm assuming this is why it's blocking when it's done through code...is there any way to force it to execute through code?
I solved my problem by adding more detail to the ProcessStartInfo object:
var process = new Process();
var startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = "cmd.exe",
Arguments = "/c \"\"" + batchFile + "\"\"",
WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(30000);
I needed to specify to use cmd.exe, as well as surrounding the batchFile path in double quotes in case there are spaces in the path.
Try prefacing it with cmd /c(that's a space after /c).
Is this IP address a Windows machine on your domain etc.

Command Line Process

So after scouring the web I found a few articles (some on stackoverflow) which described how to execute a command line prompt by starting a new process in c#. The second argument, which I've commented out, works just fine, but the one I actually need (the first one) doesn't. It returns the error "Could not find or load main class edu.stanford.nlp.parser.lexparser.LexicalizedParser" When I open up a command line (non-programatically) and then execute the same command (aside from the escaped quotations) it works great. Any idea's about what the problem could be? Thanks!
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "CMD.exe",
Arguments = "/c java -mx100m -cp \"*\" edu.stanford.nlp.parser.lexparser.LexicalizedParser edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz libtest.txt",
// Arguments = "/c echo Foo",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Console.WriteLine(proc.StandardError.ReadToEnd());
Ensure that the executing path where you start your process is correct!
You can use Process Monitor from SysInternals to figure out where that class is looked for.

Start command windows and run commands inside

I need to start the command window with some arguments and run more commands inside.
For example, launch a test.cmd and run mkdir.
I can launch the test.cmd with processstartinfo , but i am not sure how to run further commands. Can I pass further arguments to the test.cmd process?
How do I go about this?
Unable to add comments to answer... SO writing here.
Andrea, This is what I was looking for. However the above code doesnt work for me.
I am launching a test.cmd which is new command environment (like razzle build environment) and I need to run further commands.
psi.FileName = #"c:\test.cmd";
psi.Arguments = #"arg0 arg1 arg2";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.StandardInput.WriteLine(#"dir>c:\results.txt");
p.StandardInput.WriteLine(#"dir>c:\results2.txt");
You can send further commands to cmd.exe using the process
standard input. You have to redirect it, in this way:
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine(#"dir>c:\results.txt");
process.StandardInput.WriteLine(#"dir>c:\results2.txt");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
Remember to write "exit" as your last command, otherwise the cmd process doesn't terminate correctly...
The /c parameter to cmd.
ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause");
Process.Start(start);
(pause is just an example of what you can run)
But for creating a directory you can do that and most other file operations from c# directly
System.IO.Directory.CreateDirectory(#"c:\foo\bar");
Start a cmd from c# is useful only if you have some big bat-file that you don't want to replicate in c#.
What are you trying to achieve? Do you actually need to open a command window, or do you need to simply make a directory, for example?
mkdir is a windows executable - you can start this program in the same way you start cmd - there's no need to start a command window process first.
You could also create a batch file containing all the commands you want to run, then simply start it using the Process and ProcessStartInfo classes you're already using.
How come this doesn't work?
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = false
};
var process = new Process { StartInfo = startInfo };
process.Start();
process.StandardInput.WriteLine(#" dir");
process.WaitForExit();

How do I use ProcessStartInfo to run a batch file?

But it doesn't work -meaning the java code is not executed.
Although the batch file runs fine when clicked in Windows explorer or when run in command line ..
Since this works fine when the batch file is a single DOS command, I think this is somehow related to the fact that the Java code needs ~20 minutes to run.
I'm using the following code
var si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = batchFileName;
si.UseShellExecute = false;
Process.Start(si);
What am I doing wrong?
Set UseShellExecute to true, so it loads cmd.exe to run the batch file.
Check this - a batch file wrapper of ProcessStartInfo:
C:\>ProcessStartJS.bat "cmd.exe" -arguments "/c pause" -style Minimized -priority High -newWindow yes -useshellexecute yes
Started: cmd.exe /c pause
PID:6540
As Lucas Jones mentioned in the comments, if you don't want to use ShellExecute, do it like this:
string fullBatPath = #"C:\path with space\file.bat";
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"cmd /C \"{fullBatPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();

Categories

Resources