Home > Net >  ASP.NET Webforms : hide console window while running .bat file
ASP.NET Webforms : hide console window while running .bat file

Time:12-22

I am trying to execute .bat file on my .aspx web pages what I tried is this

protected void Page_Load(object sender, EventArgs e)
{        
    if (!Page.IsPostBack)
    {
        if (Application["Loaded"] == null)
        {
            Application["Loaded"] = 1;
        }
        else
        {
            int visits = (int)Application["Loaded"];
            visits  ;
            Application["Loaded"] = visits;

            if (visits > 50)
            {
                ProcessStartInfo ps = new ProcessStartInfo();
                //ps.RedirectStandardOutput = true;
                ps.UseShellExecute = false;
                ps.WindowStyle = ProcessWindowStyle.Hidden;
                Process P = new Process();
                P.StartInfo.CreateNoWindow = true;
                Process.Start(@"C:\Users\percoid it\Documents\Tempremover.bat");                    
                Application["Loaded"] = 1;
            }
        }
    }
}

I did try all this method and process but I still get the pop up console window.

I am trying to remove all the temp file that has been stored in my PC and .bat file does remove after loading my Crystal Report more than 50 times.

The issue is if I load report more than 50 time my report does crash and after adding .bat file it removed the temp files stored by Crystal Report.

But the issue here is only the pop up of that batch file while executing

CodePudding user response:

The issue that you're facing with System.Diagnostics.Process is that you've created an instance of ProcessStartInfo, but haven't used it.

Try the following:

string exePath = @"C:\Users\percoid it\Documents\Tempremover.bat";
string arguments = null;

//create new instance
ProcessStartInfo startInfo = new ProcessStartInfo(exePath);
startInfo.Arguments = arguments; //arguments
startInfo.CreateNoWindow = true; //don't create a window
startInfo.RedirectStandardError = true; //redirect standard error
startInfo.RedirectStandardOutput = true; //redirect standard output
startInfo.RedirectStandardInput = false;
startInfo.UseShellExecute = false; //if true, uses 'ShellExecute'; if false, uses 'CreateProcess'
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.ErrorDialog = false;

//create new instance
using (Process p = new Process { StartInfo = startInfo, EnableRaisingEvents = true })
{
    //subscribe to event and add event handler code
    p.ErrorDataReceived  = (sender, e) =>
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            //ToDo: add desired code 
            Debug.WriteLine("Error: "   e.Data);
        }
    };

    //subscribe to event and add event handler code
    p.OutputDataReceived  = (sender, e) =>
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            //ToDo: add desired code
            Debug.WriteLine("Output: "   e.Data);
        }
    };

    p.Start(); //start

    p.BeginErrorReadLine(); //begin async reading for standard error
    p.BeginOutputReadLine(); //begin async reading for standard output

    //waits until the process is finished before continuing
    p.WaitForExit();

}

Resources:

  • Related