简体   繁体   中英

calling a bat file in C# IIS does nothing

I have a C# program that is trying to call a bat file in the project folder. Here is the code which is calling the bat file:

protected void btnHotFolder_Click(object sender, EventArgs e)
{
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.Verb = "runas";
        string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Abbyy_Script/restart-hotfolder.bat");
        startInfo.FileName = path;
        Process.Start(startInfo);
        
    }
    catch (Exception ex)
    {
        log.Error("Batch file error");
        log.Error(ex.InnerException.Message);
        log.Error(ex.InnerException.StackTrace);
    }
}

I have read on the forum that some have suggested giving the IIS user folder access rights to the folder containing the bat file. Tried but no avail..

This is part of the project structure showing where the bat file resides:

project - Abbyy_Script - restart-hotfolder.bat

In the bat file, I am currently testing it with notepad++ exe:

taskkill /im notepad++.exe
TIMEOUT 2
START "" "C:\Program Files\Notepad++\notepad++.exe"

You should improve your batch file:

taskkill /f /im notepad++.exe /t
timeout /t 2 /nobreak

IF EXIST "%ProgramFiles(x86)%\Notepad++\notepad++.exe" (
    start "" "%ProgramFiles(x86)%\Notepad++\notepad++.exe"
)
IF EXIST "%ProgramFiles%\Notepad++\notepad++.exe" (
    start "" "%ProgramFiles%\Notepad++\notepad++.exe"
)

The C# code needs to be improved too:

protected void btnHotFolder_Click(object sender, EventArgs e)
{
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Abbyy_Script/restart-hotfolder.bat");
        startInfo.FileName = path;
        Process.Start(startInfo);
    }
    catch (Exception ex)
    {
        log.Error("Batch file error");
        log.Error(ex.InnerException.Message);
        log.Error(ex.InnerException.StackTrace);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM