I create simple unity application, there is button that give user print a picture. When button is clicked, it will run a command in cmd but I get this message,
'cd' is not recognized as an internal or external command,
operable program or batch file.
I search the web, it says I need to change encoding type but I have do it in Notepad++ (I change it from UTF-8 BOM to ANSI) but it still no work.
Here is my code, in case I am wrong.
string fullCommand = "rundll32 C:/WINDOWS/system32/shimgvw.dll,ImageView_PrintTo " + filePath + printerName
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = "/c " + fullCommand;
process.Start();
This is my reference
How to direct printing of photo or text using Unity without preview
I expect it to print the expected file but it do not. I hope someone can guide me. Thank you for helping.
Cheers
Kev
Related
I'm trying to read the output of a process to string. For some reason, it sort of looks like the one line in the middle of the output seems to get outputted (ie, it's displayed on the screen, and NOT saved to the string).
string strOutput = "";
Process process = new Process();
process.StartInfo.FileName = "nslookup";
process.StartInfo.Arguments = "-type=mx uic.edu";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
strOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine("xxxxxxxxxxxxxxxxxxx");
Console.WriteLine(strOutput);
Console.WriteLine("yyyyyyyyyyyyyyyyyyy");
I get output that looks like this:
Non-Authoritative answer:
xxxxxxxxxxxxxxxxxxxx
Server: aaa.myserver.com
Address: 111.222.111.222
uic.edu MX preference = 10, mail exchanger - ...
...
yyyyyyyyyyyyyyyyyyyy
When I run the command via command line, "Non-Authoritative answer:" comes after "Address: ..."
Can someone explain why it's outputted, and not stored as part of the string? I'm probably missing something obvious, but I'm boggled.
Thanks
That line is probably going to STDERR rather than STDOUT. Try redirecting standard error as well as standard output.
process.StartInfo.RedirectStandardError = true;
I am trying to execute a bat file through my WPF application on a button click.
I want the output of the batch file to be displayed in a TextBlock(with vertical scroll) of WPF application.
I am able to execute a bat file using Process.Start
Here is my Code
Process process = new Process();
process.StartInfo.FileName = #"C:\bin\run.bat";
process.StartInfo.Arguments = #"-X";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
process.WaitForExit();
How to proceed further?
Thanks
I guess I will flesh out my comment with an answer. You need to redirect the output of your bat file, you do that by using Process.RedirectStandardOutput. Taking your code and the MSDN Library page's code will give you something like this.
Process process = new Process();
process.StartInfo.FileName = #"C:\bin\run.bat";
process.StartInfo.Arguments = #"-X";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.UseShellExecute = false; //Changed Line
process.StartInfo.RedirectStandardOutput = true; //Changed Line
process.Start();
string output = process.StandardOutput.ReadToEnd(); //Changed Line
process.WaitForExit(); //Moved Line
you need to redirect standard output to your text file using the gtr symbol ">". e.g
command>myfile.extension
and the opposite executes the commands in a file one line at a time e.g
command
I´m trying to create a small console app in c#. I want to run the program and save all pending changes in TFS to a .txt file. But I cant get the arguments to work. Can someone help me?
Here is my code i haved done so far:
string argument = "#tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt";
try
{
Process process = new Process();
process.StartInfo.Arguments = "#call" + " " + "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\Tools\\VsDevCmd.bat";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Verb = "runas";
process.StartInfo.Arguments = argument;
process.StartInfo.CreateNoWindow = false;
process.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
aI'm not really sure that I understand what you're trying to call, exactly.
Let's assume you want to run the following command line from a C# application, as if you would call it from a command line:
tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt"
I would use this code:
string arguments = #"/C tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt";
this.process = new Process();
this.process.StartInfo.FileName = #"cmd.exe";
this.process.StartInfo.Arguments = arguments;
this.process.Start();
Edit:
If that's all your console app does, why not consider creating a batch (.BAT / .CMD) file instead of a C# application?
Instead of running a command line tool you could leverage the TFS API.
There are many articles out there, e.g. Code project article on topic
and
Sample code directly from the MSDN
I suppose you have to read standard error and output from process started:
Process process = new Process();
process.StartInfo.Arguments = #"status PATH /recursive";
process.StartInfo.FileName = "tf.exe";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
var st = process.StandardOutput.ReadToEnd();
var err = process.StandardError.ReadToEnd();
But parsing tf output is not easy and I'd like to suggest to use TFS API as #Mare said
You do not need to create an application in C # to save in a text file. Just use the parameters (...) > [file name].txt at the end of the command.
The ">" symbol send the result of any command to a file.
i need to install a device driver (INF file) through c#. I used the function UpdateDriverForPlugAndPlayDevices. However, it returns me FALSE, but the GetLastError() returns a value 0, which indicates Success message for the installation.
Not sure if i am proceeding in the correct way or not.
Can anyone help?
Thanks in advance,
P
You should look at the source for devcon. It is available in the WDK and is exactly what you need. Specifically look for the way that devcon will install an INF file. I still use the Windows 7 WDK, and it's located at C:\WinDDK\7600.16385.1\src\setup\devcon.
You'll probably find it's using the SetupCopyOEMInf() function, which you should try using from your C# application too.
This simple code worked for me
private void driverInstall()
{
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c C:\\Windows\\System32\\InfDefaultInstall.exe " + driverPath; // where driverPath is path of .inf file
process.Start();
process.WaitForExit();
process.Dispose();
MessageBox.Show(#"ADB / Fastboot / Google Android Driver has been installed");
}
I'm trying to launch an application from c# code. Below is the code.. But the exe gives the error "Application has encountered a problem and needs to close. Sorry for the inconvenience".
I'm passing the command values as
command = "\"C:\\Program Files\\Nimbuzz\\Nimbuzz.exe\"";
code:
private int ExecuteSystemCommand(string command)
{
procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
return proc.Id;
}
But the exe opens normally when opened from the desktop short cut. I dont know whats wrong. Please suggest.
You must specify the EXE you want to execute.
Process.Start("cmd.exe", ...)
It would appear that these articles answer the question:
ProcessStartInfo.UseShellExecute
ProcessStartInfo.FileName
Well I just found out that, I need to set the Working directory first before calling the Process.Start()
Directory.SetCurrentDirectory("C:\\Program Files\\Nimbuzz\\");