Run Batch File

Experts _ I am using the following code to execute a batch file based upon the batch files that are in a set directory. I am getting an error that I can't seem to figure out:

The directory name is invalid
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: The directory name is invalid

Source Error:


Line 47:
Line 48:         // Start the process
Line 49:         System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
Line 50:
Line 51:         // Open the batch file for reading
 


Here is my code:

using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class Compiler : System.Web.UI.Page
{
    protected RadioButtonList ClassesToCompile;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        string BatchFileDir = @"C:\Documents and Settings\Administrator\Desktop\batch\";

        string[] dirFiles = Directory.GetFiles(BatchFileDir, "*.bat");

        int i = 0;

        foreach (string dirFile in dirFiles)
        {
            string strDisplay = dirFile.Replace(BatchFileDir, "");
            ClassesToCompile.Items.Insert(i, new ListItem(strDisplay, dirFile));
            i++;
        }

    }

    protected void page_selectClass(object sender, EventArgs e)
    {
        string strSelectedClass = ClassesToCompile.SelectedValue;

        // Get the full file path
        string strFilePath = strSelectedClass;

        // Create the ProcessInfo object
        System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"c:\windows\system32\cmd.exe");
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardError = true;
        psi.WorkingDirectory = "c:\\temp\\";

        // Start the process
        System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

        // Open the batch file for reading
        System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

        // Attach the output for reading
        System.IO.StreamReader sOut = proc.StandardOutput;

        // Attach the in for writing
        System.IO.StreamWriter sIn = proc.StandardInput;

        // Write each line of the batch file to standard input
        while (strm.Peek() != -1)
        {
            sIn.WriteLine(strm.ReadLine());
        }

        strm.Close();

        // Exit CMD.EXE
        string stEchoFmt = "# {0} run successfully. Exiting";

        sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
        sIn.WriteLine("EXIT");

        // Close the process
        proc.Close();

        // Read the sOut to a string.
        string results = sOut.ReadToEnd().Trim();

        // Close the io Streams;
        sIn.Close();
        sOut.Close();

        // Write out the results.
        string fmtStdOut = "<font face=courier size=0>{0}</font>";
        this.Response.Write(String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>")));

        // reset radio button list
        ClassesToCompile.Items.FindByValue(strSelectedClass).Selected = false;
    }
}


Any ideas on what I am missing? I have already set basic security access rights throughout the application. Thanks for your help...
LVL 1
clickclickbangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

salimaminCommented:
are you executing it from asp.net page?

I dont think you can do this from asp.net webpage.
clickclickbangAuthor Commented:
This is where I got the bulk of the code:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21173290.html?query=run+a+batch+file&topics=865

I would agree if it were a permissions problem, but why is it saying invalid directory?
salimaminCommented:
if you notice then the batch file path given in the code of the link is temp folder path. But you are trying to execute a file from @"C:\Documents and Settings\Administrator\Desktop\batch\";.

Try to keep this file on server itself and see whether it is executing or not. or else you create on file at .bat in the temp folder and send input lines to it and then execute it.

Hope it will work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clickclickbangAuthor Commented:
I am going to keep trying it out. Thanks for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.