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");
Related
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
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");
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();
I have following code
using (StreamWriter outfile = new StreamWriter(#"f:\trial.cpp"))
{
outfile.Write(txtCode.InnerText);
}
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(#"cl.exe", #" 'trial.cpp'");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.UserName = "asdasd";
SecureString secureString = new SecureString();
foreach (char c in "abcded")
{
secureString.AppendChar(c);
}
procStartInfo.Password = secureString;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = #"f:\";
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
How to pass file name as parameter? Above code doesn't run and I have tried all full path, different path options.
can anyone help?
The argument is set incorrectly. You have:
var procStartInfo = new ProcessStartInfo(#"cl.exe", #" 'trial.cpp'");
Where there are spaces and single quotes in the name. Try:
var procStartInfo = new ProcessStartInfo(#"cl.exe", #"trial.cpp");
EDIT:
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "CL.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "trial.cpp";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// error handling
}
The point here is that CL is a command line executable, not a windows GUI application.
http://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx
http://msdn.microsoft.com/en-us/library/kezkeayy.aspx
http://msdn.microsoft.com/en-us/library/9s7c9wdw.aspx
If the cl.exe is not in the system PATH (which by default it is not) then the start process will not find the executable and it will fail to run.
So I suspect you are seeing the fact that the cl.exe is not in the system PATH.
I want to call from a c# application a command line starting from it an application and retrieve the stat from it.
I did this but something is missing:
ProcessStartInfo psf = new ProcessStartInfo("cmd.exe", "/C time");
psf.WindowStyle = ProcessWindowStyle.Hidden;
psf.RedirectStandardOutput = true;
psf.UseShellExecute = false;
psf.CreateNoWindow = true;
Process p = Process.Start(psf);
StreamReader sr = p.StandardOutput;
p.WaitForExit();
What is wrong ?
Try passing "/c time /t" instead of "/c time".
To get the system time I would recommend you using the DateTime structure:
string time = DateTime.Now.ToString("hh:mm:ss.fff");
Console.WriteLine(time);