How to run vbs file c# - c#

help me please! I've script which displays the windows activation key. When i use this:
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = #"cscript";
scriptProc.StartInfo.WorkingDirectory = #"C://....";
scriptProc.StartInfo.Arguments = "//B //Nologo name_of_script";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.Start();
Nothing happens, but if i run VBS file from cd, like this:
ProcessStartInfo cmdStart = new ProcessStartInfo();
cmdStart.FileName = "cmd.exe";
cmdStart.WindowStyle = ProcessWindowStyle.Normal;
cmdStart.Arguments = #"/k cd C://.... && start wininfo.vbs";
Process.Start(cmdStart);
I get an error that the registry key cannot be opened for reading
P.S. the script works if I just run it on the desktop

Related

C# CMD with command not executed [duplicate]

In C# WPF: I want to execute a CMD command, how exactly can I execute a cmd command programmatically?
Here's a simple example :
Process.Start("cmd","/C copy c:\\file.txt lpt1");
As mentioned by the other answers you can use:
Process.Start("notepad somefile.txt");
However, there is another way.
You can instance a Process object and call the Start instance method:
Process process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.WorkingDirectory = "c:\temp";
process.StartInfo.Arguments = "somefile.txt";
process.Start();
Doing it this way allows you to configure more options before starting the process. The Process object also allows you to retrieve information about the process whilst it is executing and it will give you a notification (via the Exited event) when the process has finished.
Addition: Don't forget to set 'process.EnableRaisingEvents' to 'true' if you want to hook the 'Exited' event.
if you want to start application with cmd use this code:
string YourApplicationPath = "C:\\Program Files\\App\\MyApp.exe"
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.FileName = "cmd.exe";
processInfo.WorkingDirectory = Path.GetDirectoryName(YourApplicationPath);
processInfo.Arguments = "/c START " + Path.GetFileName(YourApplicationPath);
Process.Start(processInfo);
Using Process.Start:
using System.Diagnostics;
class Program
{
static void Main()
{
Process.Start("example.txt");
}
}
How about you creat a batch file with the command you want, and call it with Process.Start
dir.bat content:
dir
then call:
Process.Start("dir.bat");
Will call the bat file and execute the dir
You can use this to work cmd in C#:
ProcessStartInfo proStart = new ProcessStartInfo();
Process pro = new Process();
proStart.FileName = "cmd.exe";
proStart.WorkingDirectory = #"D:\...";
string arg = "/c your_argument";
proStart.Arguments = arg;
proStart.WindowStyle = ProcessWindowStyle.Hidden;
pro.StartInfo = pro;
pro.Start();
Don't forget to write /c before your argument !!
Argh :D not the fastest
Process.Start("notepad C:\test.txt");
Are you asking how to bring up a command windows? If so, you can use the Process object ...
Process.Start("cmd");
You can do like below:
var command = "Put your command here";
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.WorkingDirectory = #"C:\Program Files\IIS\Microsoft Web Deploy V3";
procStartInfo.CreateNoWindow = true; //whether you want to display the command window
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
label1.Text = result.ToString();
In addition to the answers above, you could use a small extension method:
public static class Extensions
{
public static void Run(this string fileName,
string workingDir=null, params string[] arguments)
{
using (var p = new Process())
{
var args = p.StartInfo;
args.FileName = fileName;
if (workingDir!=null) args.WorkingDirectory = workingDir;
if (arguments != null && arguments.Any())
args.Arguments = string.Join(" ", arguments).Trim();
else if (fileName.ToLowerInvariant() == "explorer")
args.Arguments = args.WorkingDirectory;
p.Start();
}
}
}
and use it like so:
// open explorer window with given path
"Explorer".Run(path);
// open a shell (remanins open)
"cmd".Run(path, "/K");

How do I execute a shell script in C#?

I have one file which contains a Unix shell script. So now I wanted to
run the same in .NET. But I am unable to execute the same.
So my point is, is it possible to run the Unix program in .NET? Is there any API like NSTask in Objective-C for running Unix shell scripts so any similar API in .NET?
It has been answered before. Just check this out.
By the way, you can use:
Process proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "program.exe",
Arguments = "command line arguments to your executable",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
After that start the process and read from it:
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
// Do something with line
}
ProcessStartInfo frCreationInf = new ProcessStartInfo();
frCreationInf.FileName = #"C:\Program Files\Git\git-bash.exe";
frCreationInf.Arguments = "Test.sh";
frCreationInf.UseShellExecute = false;
var process = new Process();
process.StartInfo = frCreationInf;
process.Start();
process.WaitForExit();

bash pipes - I am trying to call script from c#

I have a cygwin bash scripts that works:
#!/bin/sh
cd myc
cp Stats.txt Stats.txt.cpy;
cat Stats.txt.cpy | sort -n -k1 | gawk '{sum+=$2; print $0,sum}' > Stats.txt
I want to "call" it from C#:
string cmd="myscript.sh";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"C:\Cygwin\bin\bash.exe");
psi.Arguments = cmd;
psi.WorkingDirectory = "C:\\cygwin\\home\\Moon";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
proc.StartInfo = psi;
proc.Start();
string error = proc.StandardError.ReadToEnd();
string output = proc.StandardOutput.ReadToEnd();
this.textBox1.AppendText(error);
this.textBox1.AppendText(output);
It works fine from cygwin terminal BUT from C# I get:
Input file specified two times.
I suspect this is a pipes thing - can anyone help?
It was a path issue.
You need to set path in the script - otherwise it uses a different "non-cygwin" path and gets wrong commands.

Starting Weka from command line through C#

I am trying to open Weka from cmd line, using C#. This is the code that I'm using. It's giving me an error for Weka.Start() line, and the error is : Win32 exception was unhandled. System cannot find the file specified. Please help me out. Thanks
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(#"C:\Program Files\Weka- 3-6\java -Xmx1536m -jar weka.jar");
WekaStartInfo.UseShellExecute = false;
WekaStartInfo.RedirectStandardOutput = true;
WekaStartInfo.RedirectStandardError = true;
WekaStartInfo.CreateNoWindow = false;
Process Weka = new Process();
Weka.StartInfo = WekaStartInfo;
Weka.Start();
string output = Weka.StandardOutput.ReadToEnd();
Weka.WaitForExit();
There are two options to start WEKA from a
C# application.
In the WEKA install directory there is a
batch file called RunWeka.bat. To start WEKA
using this batch file use the following
code:
ProcessStartInfo wekaStartInfo =
new ProcessStartInfo(#"c:\Program Files\Weka-3-6\runweka.bat", "default");
wekaStartInfo.WorkingDirectory = #"c:\Program Files\Weka-3-6";
wekaStartInfo.UseShellExecute = false;
wekaStartInfo.RedirectStandardOutput = true;
wekaStartInfo.RedirectStandardError = true;
wekaStartInfo.CreateNoWindow = false;
using(Process weka = new Process())
{
weka.StartInfo = wekaStartInfo;
weka.Start();
}
To start WEKA without using the batch file
use the following code:
ProcessStartInfo wekaStartInfo =
new ProcessStartInfo(#"javaw", #"-classpath . RunWeka -i .\RunWeka.ini -w .\weka.jar -c default");
wekaStartInfo.WorkingDirectory = #"c:\Program Files\Weka-3-6";
wekaStartInfo.UseShellExecute = false;
wekaStartInfo.RedirectStandardOutput = true;
wekaStartInfo.RedirectStandardError = true;
wekaStartInfo.CreateNoWindow = false;
using(Process weka = new Process())
{
weka.StartInfo = wekaStartInfo;
weka.Start();
}
In both cases you have to set the working directory.
You've probably specified incorrect or inexistent location for your process based on the error description. Check that the path specified in ProcessStartInfo is correct.
Maybe, there are unnecessary spaces in the declaration here:
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(#"C:\Program Files\Weka-3-6\java -Xmx1536m -jar weka.jar");
In the constructor of ProcessStartInfo you must either enter just the application name, or specify the arguments separate;
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(
#"C:\Program Files\Weka-3-6\java.exe",
#"-Xmx1536m -jar weka.jar");

Execute command line using C#

All I am trying to do is send a command that opens a model with the program.exe
Supposed to be super simple!
Ex:
"C:\Program Files (x86)\River Logic\Enterprise Optimizer 7.4 Developer\EO74.exe" "C:\PauloXLS\Constraint Sets_1.cor"
The line above works well if pasted on the command prompt window.
However, when trying to pass the same exact string on my code it gets stuck on C:\Program
string EXE = "\"" + #tbx_base_exe.Text.Trim() + "\"";
string Model = "\"" + #mdl_path.Trim()+ "\"";
string ExeModel = EXE + " " + Model;
MessageBox.Show(ExeModel);
ExecuteCommand(ExeModel);
ExeModel is showing te following line on Visual Studio:
"\"C:\\Program Files (x86)\\River Logic\\Enterprise Optimizer 7.4 Developer\\EO74.exe\" \"C:\\PauloXLS\\Constraint Sets_1.cor\""
To me looks like it is the string I need to send in to the following method:
public int ExecuteCommand(string Command)
{
int ExitCode;
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
Process = Process.Start(ProcessInfo);
Process.WaitForExit();
ExitCode = Process.ExitCode;
Process.Close();
return ExitCode;
}
Things I've tried:
Pass only one command at a time (works as expected), but not an option since the model file will open with another version of the software.
Tried to Trim
Tried with # with \"
Can anyone see any obvious mistake? Thanks.
It's pretty straightforward. You just create a command line object then write to it, then to execute it you read back from it using SR.ReadToEnd():
private string GETCMD()
{
string tempGETCMD = null;
Process CMDprocess = new Process();
System.Diagnostics.ProcessStartInfo StartInfo = new System.Diagnostics.ProcessStartInfo();
StartInfo.FileName = "cmd"; //starts cmd window
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.CreateNoWindow = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.UseShellExecute = false; //required to redirect
CMDprocess.StartInfo = StartInfo;
CMDprocess.Start();
System.IO.StreamReader SR = CMDprocess.StandardOutput;
System.IO.StreamWriter SW = CMDprocess.StandardInput;
SW.WriteLine("#echo on");
SW.WriteLine("cd\\"); //the command you wish to run.....
SW.WriteLine("cd C:\\Program Files");
//insert your other commands here
SW.WriteLine("exit"); //exits command prompt window
tempGETCMD = SR.ReadToEnd(); //returns results of the command window
SW.Close();
SR.Close();
return tempGETCMD;
}
Why are you opening a command prompt (cmd.exe)? Just pass the name of the executable.

Categories

Resources