trying to pass file name from aspx page to console.exe - c#

i want to pass the value of a lable or textbox in an aspx page to a console.exe application
such that the if the value is sample.doc it changes to that.
i am calling from the aspx page with
string f = TextBox1.Text;
System.Diagnostics.Process.Start("C:/DocUpload/ConsoleApplication1.exe", f);
i have tried converting to string then using the string vatiable inplace of sample.doc but no luck
object FileName = System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, "sample.doc");
any help or ideas will be welcomed.
thank u

You're probably trying to process a file that is in a different folder.
If so, you need to pass the full path of the file, like this:
Process.Start(#"C:\DocUpload\ConsoleApplication1.exe",
Path.Combine(#"C:\path\to\folder", TextBox1.Text));

Here is what I use to start processes from calling applications. Since you are calling it from a web-app you are going to need to make sure you have appropriate permissions.
Process proc = new Process();
StringBuilder sb = new StringBuilder();
string[] aTarget = target.Split(PATH_SEPERATOR);
string errorMessage;
string outputMessage;
foreach (string parm in parameters)
{
sb.Append(parm + " ");
}
proc.StartInfo.FileName = target;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = sb.ToString();
proc.Start();
proc.WaitForExit
(
(timeout <= 0)
? int.MaxValue : (int)TimeSpan.FromMinutes(timeout).TotalMilliseconds
);
errorMessage = proc.StandardError.ReadToEnd();
proc.WaitForExit();
outputMessage = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
A link to MSDN:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx
You'll also need to check to make sure that the account the Web application is running under has the appropriate permissions to execute the program.

Related

Process.Start(processInfo) response is blank

I need to invoke an exe using System.Diagnostics.Process.Start(processInfo) and want to get some value return back.Based on return value i need to perform further operation. Exe is getting invoked and performing the task accurately, but i am not able to get return value back. Code gets stuck after process.Start() no exception or warning.
string arguments = arg[0]+ " " + arg[1] + " " + arg[2] + " " + arg[3];
string consoleExePath = #"C:\Test\Console.exe";
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.FileName = "cmd.exe";
processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = Path.GetDirectoryName(consoleExePath);
processInfo.Arguments = string.Format("/c START {0} {1}", Path.GetFileName(consoleExePath), arguments);
System.Diagnostics.Process p = System.Diagnostics.Process.Start(processInfo);
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
int result = p.ExitCode; // always 0
Code inside exe:
static int Main(string[] args)
{
var flag = 0;
flag= objTest.DoSomething(args[0],args[1], args[2], args[3]);
//Console.WriteLine("Process completed!");
//Console.Read();
//return flag;
return 44; // a non zero value.
}
Edit: Due to Console.Read();, code execution got stuck. thanks to schnaader for catching the silly mistake. But var output = p.StandardOutput.ReadToEnd(); is still empty. tried int result = p.ExitCode; and result is always 0.
Your code works for me. Note that in your .exe code, there is a line:
Console.Read();
So the program will wait for the user to enter a line, and only exit after that. If you don't do this, the other code will wait for the application to terminate like you described.
So you should remove that line and try again.
Another possibility that Christian.K noted in the comments is to redirect standard input using processInfo.RedirectStandardInput = true. This way, you won't have to modify the .exe code. After that, you can redirect things to the standard input of your .exe, see MSDN for a full example.

"The system cannot find the file specified" error on process.Start();

I am trying to get the process respond as a string so I can use it in different place in my code, this is the solution that I have so far:
const string ex1 = #"C:\Projects\MyProgram.exe ";
const string ex2 = #"C:\Projects\ProgramXmlConfig.xml";
Process process = new Process();
process.StartInfo.WorkingDirectory = #"C:\Projects";
process.StartInfo.FileName = "MyProgram.exe ";
process.StartInfo.Arguments = ex2;
process.StartInfo.Password = new System.Security.SecureString();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
try
{
process.Start();
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
}
catch (Exception exception)
{
AddComment(exception.ToString());
}
But when I'm running this I get:
"The system cannot find the file specified" error in process.Start(); without
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
The code runs fine but it just open console window and all the process response is trow there so I can't use it as string.
Does anyone know why I am getting this error or maybe a different solution to my problem?
I suspect the problem is that the filename you're specifying is relative to your working directory, and you're expecting Process.Start to look there when starting the process - I don't believe it works that way when UseShellExecute is false. Try just specifying the absolute filename of the process you want to start:
process.StartInfo.FileName = #"C:\Projects\MyProgram.exe";
Note that I've also removed the space from the end of the string you were assigning for the FileName property - it's entirely possible that was casuing the problem too.
For System32 access if you are trying to RUN an x86 Application on x64 then you must use the "Sysnative" keyword instead of "System32" in your filename.
EG: instead of:
C:\Windows\System32\whoiscl.exe
It should be:
C:\Windows\Sysnative\whoiscl.exe
Hope this helps someone

C# Git Command issue

I am developing an App where I need to Run Git Command from C#. I used Process to run Commands if I am passing user Name and password then Is says UserName or Password is Incorrect but it actual working Conventional. Below is my Code:-
public static void PullCode(string gitCommand)
{
string strPassword = "Password";
ProcessStartInfo gitInfo = new ProcessStartInfo();
Process gitProcess = new Process();
gitInfo.CreateNoWindow = true;
gitInfo.UserName = "UserNAme";
gitInfo.Password = Misc.ConvertPassword(strPassword);
gitInfo.UseShellExecute = false;
gitInfo.FileName = #"C:\Program Files (x86)\Git\bin\git.exe"; //git repostory directory path
gitInfo.Arguments = gitCommand; //git command such as "fetch orign"
gitInfo.WorkingDirectory = #"E:\Code Demo\testrepo"; //YOUR_GIT_REPOSITORY_PATH Where you want to Copy Data
gitInfo.RedirectStandardError = true;
gitInfo.RedirectStandardOutput = true;
using( var proc = new System.Diagnostics.Process() )
{
proc.StartInfo = gitInfo;
proc.Start();
var output = proc.StandardOutput.ReadToEnd();
var error = proc.StandardError.ReadToEnd();
var logRaw = string.IsNullOrEmpty( output ) && !string.IsNullOrEmpty( error )
? error.Split( '\n' ).ToArray()
: output.Split( '\n' ).ToArray();
proc.WaitForExit();
proc.Close();
}
}
You have two options. The first one is to set the configuration for your git repo (or globally) to use the git askpass witch is "git config core.askpass git-gui--askpass". Call this as the first command for your process. The drawback here is that each time you do a modifying command it will ask for password.
Or you can have your own executable witch will ask for password once, and have the option to store the password into your application. But here you will have to be careful since if you don't store it properly it will be a security risk.

Running Phantomjs using C# to grab snapshot of webpage

I'm trying to grab snapshots of my own website using phantomjs - basically, this is to create a "preview image" of user-submitted content.
I've installed phantomjs on the server and have confirmed that running it from the command line against the appropriate pages works fine. However, when I try running it from the website, it does not appear to do anything. I have confirmed that the code is being called, that phantom is actually running (I've monitored the processes, and can see it appear in the process list when I call it) - however, no image is being generated.
I'm not sure where I should be looking to figure out why it won't create the images - any suggestions? The relevant code block is below:
string arguments = "/c rasterize.js http://www.mysite.com/viewcontent.aspx?id=123";
string imagefilename = #"C:\inetpub\vhosts\mysite.com\httpdocs\Uploads\img123.png";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.FileName = #"C:\phantomjs.exe";
p.StartInfo.Arguments = arguments + " " + imagefilename;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I check the errors that phantomjs throws during its process.
You can read them from Process.StandardError.
var startInfo = new ProcessStartInfo();
//some other parameters here
...
startInfo.RedirectStandardError = true;
var p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit(timeToExit);
//Read the Error:
string error = p.StandardError.ReadToEnd();
It will give you an idea of what happened
The easiest way for executing phantomjs from C# code is using wrapper like NReco.PhantomJS. The following example illustrates how to use it for rasterize.js:
var phantomJS = new PhantomJS();
phantomJS.Run( "rasterize.js", new[] { "https://www.google.com", outFile} );
Wrapper API has events for stdout and stderr; also it can provide input from C# Stream and read stdout result into C# stream.

How can I invoke app on server from ASP.NET web app?

This question could be a can of worms, but I've always wondered if it were possible to invoke an executable application from inside a web application on the web server?
I can easily set this up as a scheduled task to run and check every few minutes, but it would be nice to be able to invoke the application on demand from the a web page.
The site is an ASP.NET 2.0 website using C# and the application is an executable console app written in C#.
You need to start a new process. This is an example how to do it.
Process proc = new Process();
StringBuilder sb = new StringBuilder();
string[] aTarget = target.Split(PATH_SEPERATOR);
string errorMessage;
string outputMessage;
foreach (string parm in parameters)
{
sb.Append(parm + " ");
}
proc.StartInfo.FileName = target;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = sb.ToString();
proc.Start();
proc.WaitForExit
(
(timeout <= 0)
? int.MaxValue : (int)TimeSpan.FromMinutes(timeout).TotalMilliseconds //Per SLaks suggestion. Thanks!
);
errorMessage = proc.StandardError.ReadToEnd();
proc.WaitForExit();
outputMessage = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
A link to MSDN:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start(VS.71).aspx
You'll also need to check to make sure that the account the Web application is running under has the appropriate permissions to execute the program.
Process.Start
I think you could do that using the following code.
Process p= new Process();
p.StartInfo.WorkingDirectory = #"C:\whatever";
p.StartInfo.FileName = #"C:\some.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();
where the Process type is from the namespace System.Diagnostics.Process

Categories