【问题标题】:ASP.NET Webforms:运行 .bat 文件时隐藏控制台窗口
【发布时间】:2022-01-22 18:47:32
【问题描述】:

我正在尝试在我的 .aspx 网页上执行 .bat 文件 我尝试的是这个

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;
            }
        }
    }
}

我确实尝试了所有这些方法和过程,但仍然弹出控制台窗口。

我正在尝试删除存储在我的 PC 中的所有临时文件,而 .bat 文件在我的 Crystal Report 加载超过 50 次后会删除。

问题是,如果我加载报告超过 50 次,我的报告确实崩溃了,并且在添加 .bat 文件后,它会删除 Crystal Report 存储的临时文件。

但这里的问题只是执行时弹出该批处理文件

【问题讨论】:

  • Web 服务器上没有可见的控制台窗口。如果您在自己的帐户上使用 IIS Express 运行网站,您只会得到一个控制台窗口。不过,您的代码根本不应该使用这样的批处理文件。这批在代码中无法完成的工作是什么?为什么还是 WebForms?
  • IIS 应用程序池帐户也没有 Web 应用程序文件夹之外的权限。这样,被劫持的网络应用程序将无法访问机器上的任何其他文件。您必须削弱安全性才能允许 Web 应用访问特定用户文件夹中的文件夹
  • 请查看我的编辑版本,这是我遇到的问题,而不是一切正常
  • 我已经解释过 Web 服务器上没有可见的控制台,所以没有什么需要修复的。只有登录用户才能看到在他们自己的帐户下打开的控制台窗口。您只能看到控制台,因为您在自己的帐户下运行 IIS Express。如果您将 IIS 与应用程序池帐户一起使用,您将看不到任何内容。您需要解决的问题是 CR 临时文件。我怀疑这一定是一个古老的应用程序,因为几年来没有人使用 CR:SSRS 几乎完全取代了它们,甚至 SSRS 也被 Power BI 取代

标签: c# asp.net batch-file


【解决方案1】:

System.Diagnostics.Process 您面临的问题是 你已经创建了ProcessStartInfo 的实例,但还没有使用它。

尝试以下方法:

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();

}

资源

【讨论】:

  • 感谢您的回复。它就像魅力一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 2019-07-22
  • 2012-10-14
  • 2011-04-03
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多