I am trying to run a batch file that runs another batch file from my windows application.
when i click the button, everything is frozen and i do not know why.
The nested batch file was created to build html files from RST files
I have a feeling that I am in a dead lock situation.
Thank you so much guys for your help.
I have attached a screen shot of my form and included my code as well.
private void buttonMakeHtml_Click(object sender, EventArgs e)
{
ProcessStartInfo processInfo =
new ProcessStartInfo("cmd.exe", "/c" + "\"C:\\ReadTheDocs\\makeHtml.bat\"");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
textBoxOutPut.Text = output;
textBoxError.Text = error;
}
Make html Screen
Related
When users click on a button, I want it to run the logon script(launching from server), but each computer in different servers, so I get the server name. But the netlogon.StartInfo.Arguments = slnres + #"/c \netlogon\logon.cmd"; line is not working as it should be. It should run the logon.cmd on the PC(mapping network drivers, printers, etc), and then the CMD should close.
private void MapNetwork_Click(object sender, EventArgs e)
{
Process sln = new Process();
sln.StartInfo.UseShellExecute = false;
sln.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
sln.StartInfo.FileName = "cmd.exe";
sln.StartInfo.Arguments = "/c echo %logonserver%";
sln.StartInfo.RedirectStandardOutput = true;
sln.Start();
string slnres = sln.StandardOutput.ReadToEnd();
label1.Text = slnres;
Process netlogon = new Process();
netlogon.StartInfo.UseShellExecute = false;
netlogon.StartInfo.FileName = "cmd.exe";
netlogon.StartInfo.Arguments = slnres + #"/c \netlogon\logon.cmd";
netlogon.Start();
}
A couple things:
You don't need to run a command prompt to get an environment variable. You can use Environment.GetEnvironmentVariable.
Your Arguments property for your call to logon.cmd is being constructed into this:
\\myserver/c \netlogon\logon.cmd
When I think you want this:
/c \\myserver\netlogon\logon.cmd
So make sure you put slnres at the right place in your string. Your code should look like this:
private void MapNetwork_Click(object sender, EventArgs e)
{
string slnres = Environment.GetEnvironmentVariable("logonserver");
label1.Text = slnres;
Process netlogon = new Process();
netlogon.StartInfo.UseShellExecute = false;
netlogon.StartInfo.FileName = "cmd.exe";
netlogon.StartInfo.Arguments = "/c " + slnres + #"\netlogon\logon.cmd";
netlogon.Start();
}
i am a little confused about your question and i am not rly sure if i understand you correctly. some time ago i made a program where i had to run few powershell commands, so i made a class for it. redirected to your button it would look like that:
(and remember you need the fqdn to your file location => Reading File From Network Location)
using System.Diagnostics;
//class lvl scope vars
string output;
string ErrorOutput;
private void MapNetwork_Click(object sender, EventArgs e)
{
//define process arguments
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"FQDN path to your file on the server; exit";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
//start process
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
//outpunt handling
if (string.IsNullOrEmpty(ErrorOutput))
{
return output;
}
else
{
return ErrorOutput;
}
}
first of all i would check if your application is able to open the file one the shared network location. (server available? access rights to server? serer mapped?)
after that you can check if he is able to start the file locally. (does it need admin rights to run the *.cmd, *.bat file)
now you can check if your application runs it correctly.
I wrote a simple TCP Server application,
when the program gets the request it will call an exe file to do something.
The application works fine on my PC.
But when I run on another PC the thread will stuck because it stuck in process start, so I checked the task manager whether the exe file did run. and it really work.
I have no idea how to solve it.
Here is my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
string path = System.Environment.CurrentDirectory;
process.StartInfo.WorkingDirectory = path;
process.StartInfo.FileName = "FileTransferTool\\FileTransfer.exe";
process.StartInfo.Arguments = argument;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = false;
process.Start(); // the thread stuck here
I print out the message, it shows
System.ComponentModel.Win32Exception (0x80004005):
The system cannot find the file specified at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startIn
fo)
at System.Diagnostics.Process.Start() at ClassPackage.DsUploadFiletoNC.Start(Object[] param)
I have solved this problem by adding another process to call cmd.exe on the top of the main program, the code shown as below :
And the program will run as Administrator, but I don't know why
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
string path = System.Environment.CurrentDirectory;
process.StartInfo.WorkingDirectory = #"C:\Windows\System32";
process.StartInfo.FileName = "cmd.exe";
//process.StartInfo.FileName = "FileTransferTool\\circle.txt";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardInput = true;
//process.StartInfo.CreateNoWindow = false;
process.Start();
using (TCPServer server = new TCPServer())
{
server.Start();
Console.WriteLine("Socket Server Start...");
Console.ReadKey(true);
}
}
The below code is working fine for ping stackoverflow.com and most other cases but when I using 7z.exe it's not real time, it waits until directory is compressed and then shows the output. The argument that I used for compress is a test.7z dirpath. Can I do anything else?
private ProcessStartInfo GetProcessStartInfo(string filename, string arguments)
{
ProcessStartInfo ProcessStartInfo = new ProcessStartInfo();
ProcessStartInfo.CreateNoWindow = true;
ProcessStartInfo.UseShellExecute = false;
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ProcessStartInfo.RedirectStandardOutput = true;
ProcessStartInfo.RedirectStandardError = true;
ProcessStartInfo.RedirectStandardInput = true;
ProcessStartInfo.FileName = filename;
ProcessStartInfo.Arguments = arguments;
}
private void ProcessRun(string filename, string arguments)
{
Process Process = new Process();
Process.StartInfo = GetProcessStartInfo(filename, arguments);
Process.ErrorDataReceived += Process_OutputDataReceived;
Process.OutputDataReceived += Process_OutputDataReceived;
Process.EnableRaisingEvents = true;
Process.Start();
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
Process.WaitForExit();
}
public ObservableList<string> Output = new ObservableList<string>();
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Output.Add(e.Data);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OutputListBox.ItemsSource = Output;
ProcessRun("cmd.exe", "/c ping stackoverflow.com");
}
7zip Output:
7zip Progress:
If you're interested in the percentage, you're out of luck - that isn't done by standard output. Standard output only handles streams of data, while the percentage output is actually done by manipulating the console directly. It's not part of the output stream - there is no way to replicate the same effect using the standard I/O streams.
So it's not a problem of your code. If you want to see the same problem using just the Command prompt, try running this:
7z.exe yourarguments > log.txt
The > is an output redirect - instead of writing to the console, the standard output is redirected to a file. When you use it with ping, it immediately prints out standard output as it comes. When you use it with 7zip, you get the same result as with your C# application.
I'm trying to write a C# wrapper for Iperf server. After Iperf client is done with packet sending, the C# server application should dump the output data to the text file.
The problem is that this process (server) never exits, so it doesn't dump any data to the txt file. However, when I manually close the cmd window that runs iperf server, the text file is written with data (process exits). But this is clearly not the solution I'm looking for.
Any suggestions how can I write the data directly into the file, w/o need of manually closing the iperf server cmd window?
This is my code:
private void button1_Click(object sender, EventArgs e)
{
string commandline_server = " -s -u -i 1";
try
{
process = new Process();
process.StartInfo.FileName = "iperf.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
//process.StartInfo.RedirectStandardError = true;
process.StartInfo.Arguments = commandline_server;
process.StartInfo.CreateNoWindow = false;
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(process_Exited);
process.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
void process_Exited(object sender, EventArgs e)
{
//throw new NotImplementedException();
string outfile = process.StandardOutput.ReadToEnd();
System.IO.File.WriteAllText("test.txt", outfile);
}
Instead of shelling out to a terminal, have you considered using their API? iperf3 introduced "libiperf".
There are example C-programs in their source code tree.
I'm trying to connect to a linux machine via pLink and PuTTY to transfer some .txt file. Right now I'm just trying to make the connection work. I have a small window with 2 textboxes (username,linux1) and a password box (pwbox1), and a button that when you click, it should connect you to the linux machine!
Here's my code:
private void button1_Click(object sender, RoutedEventArgs e)
{
string user = username.Text;
string passw = pwbox1.Password;
string linuxHst = linux1.Text;
ProcessStartInfo psi = new ProcessStartInfo(#"C:\Program Files (x86)\PuTTY\plink.exe", user + "#" + linuxHst + " -pw " + passw);
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
psi.CreateNoWindow = false;
Process process = Process.Start(psi);
process.WaitForExit(5000);
}
The problem is that when I try this with the console application it works, without the textboxes! but I need it to work with WPF.
Can anyone please tell me what I'm doing wrong? am I missing something?