Process.Start runs on Windows 8 but crashes on Windows 7 - c#

So I'm using the following code to perform a process using cmd.exe. It works fine on Windows 8 (my dev machine). However, when the executable is run from a Windows 7 computer, instead of getting the black pop up box with print out, its just a black box that flashes really fast and disappears with no error. There also seems to be nothing in the title of the cmd window except cmd.exe.
StandardOutput when the window closes, is blank.
wgetLocation is C:\Users\Kevin M\Desktop\wget\wget.exe
Also, when trying to catch the exception, there wasn't one.
Any help is appreciated.
//Make Folder to Store files
string downloadedFiles = Path.Combine(Directory.GetCurrentDirectory(), txt_Site.Text);
Directory.CreateDirectory(downloadedFiles);
//Get List of Sites
//Directory.SetCurrentDirectory(wgetLocation);
wgetLocation = Path.Combine(wgetLocation, "wget.exe");
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/K " + wgetLocation + " --spider -r --adjust-extension -A html -A aspx " + txt_Site.Text;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
Console.Write(p.StandardOutput.ReadToEnd());
p.WaitForExit();
Working:
Not Working:

Related

Changing windows theme in C#

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

Run File on Server from WPF Button Press

Some application code using Process.Start() and PsExec.exe to start a video on a serverpc is running OK when run from a Console application, but not when run from a button press in a WPF application. This sent me nuts today.
So:
I am running a small WPF app on a PC which, once a button is pressed, will send a command to a server PC to run a video file. I am using PsExec.exe to run the process on the server interactively (did not manage with WMI)
This is the code I'm using:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = #"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4";
p.StartInfo.CreateNoWindow = true;
p.Start();
No, this exact same code can open video file Photoslideshow.mp4 on the server PC (192.168.1.3) when run from a normal console App. However when I try to run it after pressing a button in a WPF app, p.Start() gives me a "The system cannot find the file specified" error. Here is the WPF code snippet (it's the same as above):
private void Video1_btn_Click(object sender, RoutedEventArgs e)
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = #"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4 //fullscreen";
p.StartInfo.CreateNoWindow = true;
p.Start();
}
}
When I tried to use p.Start on a file that is local to my PC, that opened as expected. It's just the server that is not 'seeing' the FileName. As I said originally, the same code only fails on p.Start when accessing it after a button click.
What am I doing wrong? Please someone tell me that this is the result of hours in front of the PC and that it's only a stupid mistake which I cannot see!.
EDIT:
After more playing around,I realised that the error "The system cannot find the file specified" related to this line:
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
enter image description here
Upon changing this line to:
p.StartInfo.FileName =#"C:\Windows\System32\Notepad.exe";
and removing the next line, Notepad opens up on my local PC. However, when I change the 2 lines back to something like:
p.StartInfo.FileName = #"Notepad.exe";
p.StartInfo.Arguments = #"\192.168.1.3 -u Administrator -p pass-i cmd.exe /c START C:\realtek.txt";...
notepad opens on my local PC but an error "Network path not found". (which is a similar error to when I run the 'non-button- code.)
Thus I know the problem is something to do with either the WPF/Button application. But I dont know what the problem is!
thanks a lot,
Mario
I managed to figure it out.
For some reason, the system was not finding PSExec from this path (even though it existed in that folder).
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
Upon copying the File to another directory and using the full path, it finally worked.

Call to Process works fine with Debug, but it doesn't work in the installed application

I am developing a Windows Form program that has callings to ffmpeg library through the class Process.
It works fine when I run it with the Debug in Visual Studio 2013. But when I install the program and I invoke the operation that call to the ffmpeg Process, it doesn't work. The cmd screen appears an disappears and nothing happens.
I have tried to know what can be happening getting a log file with the output of ffmpeg, in case it was a problem in the ffmpeg libraries. However, after executing it the log is empty, what means that the ffmpeg command has not been executed.
Can someone help me, please?
The code is this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + ffmpegPath + " " + commandArguments;
using (Process processTemp = new Process())
{
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
processTemp.Start();
processTemp.WaitForExit();
}
I am invoking to cmd.exe (not directly ffmpeg.exe) because in the arguments sometimes there can be a pipe (that is why the command starts with "/c").
Are you sure this isn't a privileges issue when trying to execute the cmd.exe (e.g you need administrator privileges)
try adding
startInfo.Verb = "runas";
Paul
Hmm its not a path issue with spaces in file/directory names is it? For ffmpegPath or one of your command parameters (if a file path). Surround all file paths with ' like below.
Try surrounding any file paths with '
startInfo.Arguments = "/c '" + ffmpegPath + "' " + commandArguments;
Also you could try adding /K to the cmd command call to stop if from closing the command prompt when it finishes. It might tell you the error before it closes the window but you wont see it if it closes so quickly
Good luck :)
Paul

Getting handle is invalid message in startprocess cmd.exe on windows10

Hello i have a c# programm that opens a cmd.exe redirect its standardinput and runs a command, after that i take a screenshot of the console and close the window.
This works fine on windows 7 but on windows 10 i get lots of "The handle is invalid" messages in the console instead of my command.
This occurs only when i do that in a windowsforms application a console application works fine even on windows 10
Here is a sample code that reproduces the problem
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/K";// chcp 65001";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.WorkingDirectory = #"c:\";
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
StreamWriter encodedWriter = new StreamWriter(myProcess.StandardInput.BaseStream, Encoding.GetEncoding(850));
encodedWriter.WriteLine("abcdef gä öü");
encodedWriter.Flush();
Thread.Sleep(2500);
myProcess.Kill();
it is in some way connected between redirecting the input, windows forms and windows 10. If one of these conditions is false their are no invalid handle messages

Visual Studio x86 tools, normal cmd.exe, Process.Start()

I need to write some code to invoke tesseract OCR in C#. I installed it and use the follow code. But it doesn't work:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = tempDir.FullName;
// doesn't work
startInfo.Arguments = string.Format("/C tesseract {0} {1}", imgName, textName);
// this works
//startInfo.Arguments = string.Format("/C copy {0} {1}", imgName, textName);
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
p.Close();
No exception or error is thrown. I just can't get the output file in the directory. I try a built-in command like the copy which commented and it works. I tried to get the stdout of the process but it always throw a "Process exit" exception.
After that I try to call tesseract in a command window. I cd into the temp dir, run tesseract img.png output and here the interest thing happens:
When start the command window via Start->Run->cmd, it works fine.
When start the command window in Visual Studio solution explorer, right click-> Open Command Prompt (It's a feature of VS Productivity Power Tools), it shows "tesseract is not recognized as an internal or external command".
I check the PATH in environment variable, it is correct. The only difference I can see is that the VS prompt shows "Setting environment for using Microsoft Visual Studio 2010 x86 tools." on the top. Doesn't it search the PATH variable to find command? Aren't they the same thing? Is it somehow related to my C# code's failure?
My OS is Windows Server 2008 64-bit.
I used it differently, as follows:
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "tesseract.exe";
p.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" -l {2} -psm {3} {4}", imageFile, outputFileName, language, PageSegMode, Hocr ? "hocr" : string.Empty);
p.Start();
p.WaitForExit();
if (p.ExitCode == 0)
{
// read output text file
}
p.Close();

Categories

Resources