10

I'm trying to access the command line and execute a command, and then return the output to my aspx page. A good example would be running dir on page load of an aspx page and returning the output via Response.Write(). I have tried using the code below. When I try debugging this it runs but never finishes loading and no output is rendered. I am using C# and .NET Framework 3.5sp1. Any help much appreciated.

Thanks, Bryan

public partial class CommandLine : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Diagnostics.Process si = new System.Diagnostics.Process();
        si.StartInfo.WorkingDirectory = @"c:\";
        si.StartInfo.UseShellExecute = false;
        si.StartInfo.FileName = "cmd.exe";
        si.StartInfo.Arguments = "dir";
        si.StartInfo.CreateNoWindow = true;
        si.StartInfo.RedirectStandardInput = true;
        si.StartInfo.RedirectStandardOutput = true;
        si.StartInfo.RedirectStandardError = true;
        si.Start();
        string output = si.StandardOutput.ReadToEnd();
        si.Close();
        Response.Write(output);
    }
}

4 Answers 4

9

You have a problem with the syntax of commandline arguments to cmd.exe. This is why cmd never exits.
In order to have cmd.exe run a program and then quit, you need to send it the syntax "/c [command]". Try running the same code with the line

        si.StartInfo.Arguments = "dir";

replaced with

        si.StartInfo.Arguments = "/c dir";

and see if it works.

1
  • Thanks Much! Works great. I'm actually using to interact w/ perforce.
    – user32474
    Oct 29, 2008 at 18:54
3

Most likely your problem is with the permissions. The user under which ASP.NET process runs is with very limited rights.

So, either you have to set the proper permissions for that user, or run ASP.NET under some other user.

This hides a security risks though, so you have to be very careful.

1
  • 1
    If permissions were the problem, the program wouldn't hang - it would cause an exception. What he's saying is that the program hangs and never finishes running. Oct 29, 2008 at 17:33
0

This is madness! Use the System.IO namepace to create your file list from inside your C# program! It's very easy to do; although this technique also has authorization issues.

2
  • 1
    I think that was just an example, not really the intended use. Oct 29, 2008 at 17:45
  • 1
    That made me laugh - perceptive!
    – pfeds
    Sep 26, 2013 at 2:29
0

Use System.Diagnostics.Process.

Here is some ASP.NET code shelling out to run subversion commands on the command line.

    ///////////////////////////////////////////////////////////////////////
    public static string run_svn(string args_without_password, string svn_username, string svn_password)
    {
        // run "svn.exe" and capture its output

        System.Diagnostics.Process p = new System.Diagnostics.Process();
        string svn_path = Util.get_setting("SubversionPathToSvn", "svn");
        p.StartInfo.FileName = svn_path;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;

        args_without_password += " --non-interactive";
        Util.write_to_log ("Subversion command:" + svn_path + " " + args_without_password);

        string args_with_password = args_without_password;

        if (svn_username != "")
        {
            args_with_password += " --username ";
            args_with_password += svn_username;
            args_with_password += " --password ";
            args_with_password += svn_password;
        }

        p.StartInfo.Arguments = args_with_password;
        p.Start();
        string stdout = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        stdout += p.StandardOutput.ReadToEnd();

        string error = p.StandardError.ReadToEnd();

        if (error != "")
        {
            Util.write_to_log(error);
            Util.write_to_log(stdout);
        }

        if (error != "")
        {
            string msg = "ERROR:";
            msg += "<div style='color:red; font-weight: bold; font-size: 10pt;'>";
            msg += "<br>Error executing svn.exe command from web server.";
            msg += "<br>" + error;
            msg += "<br>Arguments passed to svn.exe (except user/password):" + args_without_password;
            if (error.Contains("File not found"))
            {
                msg += "<br><br>***** Has this file been deleted or renamed? See the following links:";
                msg += "<br><a href=http://svn.collab.net/repos/svn/trunk/doc/user/svn-best-practices.html>http://svn.collab.net/repos/svn/trunk/doc/user/svn-best-practices.html</a>";
                msg += "<br><a href=http://subversion.open.collab.net/articles/best-practices.html>http://subversion.open.collab.net/articles/best-practices.html</a>";
                msg += "</div>";
            }
            return msg;
        }
        else
        {
            return stdout;
        }
    }
1
  • Thanks, I'm actually doing something similiar, using cmd line to run commands against perforce.
    – user32474
    Oct 29, 2008 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.