I have a button in my form that when its clicked, it will open command prompt and automatically run a javascript file. My code so far only opens the command prompt. How do you run a javascript file?
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.Start();
Now that we know that you're trying to launch a Node.js app, try this new version.
And don't forget that C# is pretty well documented on MSDN.
If the rest of your code is working as is, and you don't actually want/need to run CMD.exe, this should do the trick:
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\nodejs\node.exe";
process.StartInfo.Arguments = #"P:\ath\To\Your\File.js";
process.Start();
As a holdover from the original question: If you had been trying to launch a JScript script, you would want to use #"C:\Windows\System32\Cscript.exe", or if you want to launch a JScript script without seeing a command prompt window, replace Cscript with Wscript.
Try this:
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("Cscript.exe \"PathToYourFile\\file.js\"");
//OR
//process.StandardInput.WriteLine("Wscript.exe \"PathToYourFile\\file.js\"");
process.StandardInput.Flush();
process.StandardInput.Close();
Related
I don't know how to explain...Custom windows themes, I can apply them from any of this:
Open the .theme file easily
From CMD : call %windir%\Resources\Themes\Shady.theme
From Powershell : Invoke-Expression "C:\Windows\Resources\Themes\Shady.theme"
but when I use any of this with C# it's breaking the theme - not applying it completely. I tried Verb run as , Tried Making a ps1, bat files and run it with C# but still the same problem... See these screen Shots to understand what I mean
my code:
string tokyo1 = "call " + '\u0022' + #"%windir%\Resources\Themes\Tokyo Night.theme" + '\u0022';
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c " + tokyo1;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Verb = "runas";
process.Start();
With C#
With cmd,powershell,theme file
I assume you are just using shell execute from cmd.exe so that you don't need to decide what program should open the .theme file. But that could be done more easyly without calling cmd.exe before just do this:
var windir = Environment.GetEnvironmentVariable ("windir");
var filename = Path.Combine (windir, "Resources\\Themes\\Shady.theme");
System.Diagnostics.Process.Start (filename);
Thanks for all who tried to help.
I Fixed it !
Just Used Windows Forms App
Not the old one
I'm trying to start a local instance of notepad with a text file to try out c# cmd line arguments for eventual use in a remote connection script. I'm using System.Diagnostics.Process, but the StartInfo.Arguments doesn't actually run completely and open the notepad instance.
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"cd\ start notepad C:\test\testcmdline.txt";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
The window opens at root, which tells me the cd\ is working, but the "start notepad" does not seem to be running.
Am I missing something about the structure of StartInfo.Arguments?
EDIT: I'm trying to figure out how to run a python script on a remote server, and using this as a test for running things in cmd in c#. While it's fine to run this in notepad, I'm not sure if the principle would carry over to the eventual implementation of running a python script remotely so I'm attempting to learn how to run items through cmd in C# in general.
I ended up using the more simple 2 arg Process.Start.
string cmdText;
cmdText = #"/C C:\test\testcmdline.txt";
Process.Start("cmd.exe", cmdText);
Try adding /c in the beginning of the Arguments.
Or the above task can be done as below
var process = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = #"/c start notepad C:\test\testcmdline.txt"
};
Process.Start(process );
I am developing a Visual Studio plugin. It will automatically generate and run a command line. If I run the command in the shell, it could generate some logs during running.
However, I want to hide the shell window and display the logs in the Visual Studio Output Window. Is there a way to implement this?
Here's my code to run the command:
var process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c"+command;
process.Start();
according to this similar question
Change application type to Windows before debugging. Without Console window, Console.WriteLine works like Trace.WriteLine. Don't forget to reset application back to Console type after debugging.
This might help:
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.startInfo.RedirectStandardOutput = true;
process.startInfo.RedirectStandardError = true;
StreamReader stringBackFromProcess = process.StandardOutput;
Debug.Write(stringBackFromProcess.ReadToEnd());
// or
Console.Write(stringBackFromProcess.ReadToEnd());
I'm working on making a tech-toolkit program, and included in this 'toolkit' will be a button which runs a defrag on the local disk. Currently the batch file I've made for this is simple, it just runs a basic fragmentation analysis:
defrag C: /A
The C# code behind the button that triggers this is:
System.Diagnostics.ProcessStartInfo procInfo =
new System.Diagnostics.ProcessStartInfo();
procInfo.Verb = "runas";
procInfo.FileName = "(My Disk):\\PreDefrag.bat";
System.Diagnostics.Process.Start(procInfo);
This code does exactly what I want, it calls UAC then launches my batch file with Administative Privledges. Though once the batch file is ran, the output I recieve to the command console is:
C:\Windows\system32>defrag C: /A
'defrag' is not recognized as an internal or external command,
operable program or batch file.
What causes this Error and how do I fix it?
Check to make sure your defrag.exe file actually exists in C:\Windows\System32.
Try fully qualifying the 'defrag' command as:
C:\WINDOWS\system32\defrag.exe C: /A
Open up a cmd prompt and run this command: defrag.exe /? and post in the question what you get.
First of all: set yout project Platform Target property to Any CPU and untick the Prefer 32-bit option (99,9% this is the issue). Then... why starting a batch that invokes the command when you can just do this?
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = "/C defrag C: /A";
info.FileName = "cmd.exe";
info.UseShellExecute = false;
info.Verb = "runas";
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Works like a charm on my machine. For multiple commands:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
Process cmd = Process.Start(info);
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(command1);
sw.WriteLine(command2);
// ...
I'm trying to run a Jscript task from a C# console application.
The Jscipt file is not mine so I can't change it. The script moves some files and this is what is causing the issues.
When I run the script manually, i.e. form the shell it executes correctly. When I try and run the script from my console application the bulk of the process runs but I get a ":Error = Permission denied" error when it tries to move the files.
I've tried every permutation of the Diagnostics.Process class that I can think of but I've had no luck.
My current code:
Process process = new Process();
process.StartInfo.WorkingDirectory = Path.GetDirectoryName((string)path);
process.StartInfo.FileName = #"cmd.exe";
process.StartInfo.Arguments = "/C " + (string)path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Verb = "runas";
process.StartInfo.LoadUserProfile = true;
process.StartInfo.Domain = "admin";
process.StartInfo.UserName = #"cardax_sync_test";
process.StartInfo.Password = GetSecureString("abc123");
process.Start();
process.WaitForExit();
Any ideas?
Thanx
Rookie Mistake!
I forgot to close the text reader that creates one of the input files for the jscript.
I'll submit this question for deletion when it get's old enough. Don't want more useless info clogging up the net!