What I want is creating a new Winforms project using dotnet new, and I have tried the following code but not worked.
ProcessStartInfo psi = new ProcessStartInfo();
//psi.FileName = "cmd.exe";
//psi.Arguments = #"/C cd C:\Users\Administrator\Desktop\test & dotnet new winforms –n Test1";
psi.FileName = "dotnet.exe";
psi.Arguments = #"/C dotnet new winforms –-name Test3";
Process p = Process.Start(psi);
In addition, how to specify the location of the new project, such as C:\Users\Administrator\Desktop\test?
Try the following:
Process p = Process.Start("dotnet", "new winforms --name Test3 --output C:\\Users\\Administrator\\Desktop\\test");
Process.Start with arguments Documentation
Dotnet new documentation for the -o |--output flag
Also make sure that you are running with the right permissions to access the folder.
Related
I need to run a bunch of commands in C# in order for my program to run correctly. Previously, this script ran flawlessly and I used to get the desired output my program needs in order to compile and run correctly.
This is the script I have currently
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = #"/C mkdir C:\IL & del Output.il & cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools & ildasm /out=C:\IL\Output.il " + filePath;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardError = true;
cmd.Start();
Filepath is a parameter I pass back to this method. This parameter contains the file's URL.
When the filepath is loaded into the application now, this set of commands does not run whatsoever. There is no output file or anything. However, these commands run when I enter them manually into Command Prompt.
I've tried this process from this answer, and implemented the following code to try and get this batch file to work.
ProcessStartInfo cmd = new ProcessStartInfo( "cmd", #"/C mkdir C:\IL & del Output.il & cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools & ildasm /out=C:\IL\Output.il" + filePath );
cmd.CreateNoWindow = true;
cmd.UseShellExecute = false;
Process.Start( cmd );
The above method hasn't worked out, and the other posts on here have the same code as my own script, which doesn't help at all.
Is there anything wrong with my script? Or is there a setting that I can enable in Visual Studio?
Thanks for viewing
I am struggling to use Chromes print to PDF feature via headless browsing.
My code is very simple
var command = "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf=\"D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\" http://localhost/mypage";
Process.Start(command);
When I view my command and copy the string, and paste that into a command prompt, it works fine.
This is all on one system so I don't understand why it works in command prompt and not in my C# web app.
Try this
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = #"C:\Program Files(x86)\Google\Chrome\Application\chrome.exe";
proc.Arguments = #"--headless --disable-gpu --print-to-pdf=\""D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\"" http://localhost/mypage";
Process.Start(proc);
The system is now searching for the file "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf=\"D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\" http://localhost/mypage", but you want it to launch "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe" with some arguments. What you need to do is:
Process process = new Process();
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = "arguments";
process.Start();
I am trying to stop an application pool by issuing the below command from PowerShell
Stop-WebAppPool "Test1234"
This worked. I checked that the "Test1234" Application Pool has stopped.
However, when I made it as a test.ps1 file and invoked the same from my C# code, it didnot worked.
C# Code
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = #"& 'c:\test.ps1'";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
It didn't throw any error but didn't worked. Why this happened and how to get that work?
If you need to invoke powershell from C#, you may consider using System.Management.Automation nuget package. As described in msdn docs, this nuget package will allow you to run powershell as follows:
using System.Management.Automation;
// ...
PowerShell ps = PowerShell.Create();
ps.AddCommand("Stop-WebAppPool");
ps.AddArgument("Test1234");
ps.Invoke();
I am using the following code to run a Linux console command via Mono in a C# application:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c ls");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
String result = proc.StandardOutput.ReadToEnd();
This works as expected. But, if i give the command as "-c ls -l" or "-c ls /path" I still get the output with the -l and path ignored.
What syntax should I use in using multiple switches for a command?
You forgot to quote the command.
Did you try the following on the bash prompt ?
bash -c ls -l
I strongly suggest to read the man bash.
And also the getopt manual as it's what bash use to parse its parameters.
It has exactly the same behavior as bash -c ls
Why? Because you have to tell bash that ls -l is the full argument of -c, otherwise -l is treated like an argument of bash.
Either bash -c 'ls -l' or bash -c "ls -l" will do what you expect.
You have to add quotes like this:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c 'ls -l'");
I am trying to delete a directory using C#. The first method I tried was
Directory.Delete(#"C:\Program Files (x86)\Qmuzki32");
I get an exception stating that the directory is not empty. I then found a cmd command which I can use to delete the directory quietly regardless of the fact that the directory is empty or not. I ran the following command in cmd:
rmdir /s /q "C:/Program Files (x86)/Qmuzik32"
This worked and did exactly what I wanted it to do. With my first attempt I tried building this command into a C# process like so:
if (Directory.Exists(#"C:\Program Files (x86)\Qmuzik32"))
{
string sQM32Folder = #"C:\Program Files (x86)\Qmuzik32";
Process del = new Process();
del.StartInfo.FileName = "cmd.exe";
del.StartInfo.Arguments = string.Format("rmdir /s /q \"{0}\"", sQM32Folder);
del.WaitForExit();
}
This did not work and then I tried it like this:
if (Directory.Exists(#"C:\Program Files (x86)\Qmuzik32"))
{
string sQM32Folder = #"C:\Program Files (x86)\Qmuzik32";
Process del = new Process();
del.StartInfo.FileName = "rmdir.exe";
del.StartInfo.Arguments = string.Format("/s /q \"{0}\"", sQM32Folder);
del.WaitForExit();
}
Same problem. I get the exception:
No process is associated with this object.
I do think I am on the right track; maybe the code above just requires some tweaking.
Just use Directory.Delete(string, bool).
While the low-level filesystem APIs of course require you to make sure the directory is empty first, any half-decent framework abstracting them allows you do do a recursive delete. In fact, existence of such a method would be the first thing I'd check before even trying to resort to external programs.
If you want to use the cmd way you can use this:
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C rd /s /q \"C:\\Program Files (x86)\\Qmuzik32\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
del.Start();
del.WaitForExit();
you didn't start the procces so it doesn't have a PID so it dies