如何在从进程中调用EXE时隐藏应用程序窗口?

时间:2012-07-13 10:54:05

标签: c# .net .net-services

我正在使用服务中的类库运行应用程序的EXE。 但我试图做的是隐藏应用程序EXE的窗口。 这是我的代码:

在我的班级图书馆的功能中: -

public class MyClassLibrary
{
    public void MyFunction()
    {
        Process process = new Process();
        process.StartInfo.FileName = "C:\Program Files (x86)\MyFolder\MyApp.exe";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
    }
}

这就是我从以下地方打电话的地方:

class MyClass : ServiceBase
{
    ...
    ...
    ...
    protected override void OnStart()
    {
        MyClassLibrary obj = new MyClassLibrary();
        obj.MyFunction();
    }
}

尽管如此,窗外还没有看到。 有人可以建议一个解决方案吗?

谢谢和问候, Siddhant

3 个答案:

答案 0 :(得分:0)

我已经尝试过您的代码无效但

但是当我以这种方式尝试它时它可以正常工作

string filePath = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";    
ProcessStartInfo pStartInfo = new ProcessStartInfo(filePath );
pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Process.Start(startInfo);
  

它适用于Process.Start(ProcessStartInfo)的原因,因为它   将给定信息与新组件相关联,如上所述   MSDN

答案 1 :(得分:0)

我得到了答案的人,感谢this评论,我从Arne的评论中得到了这个问题。显然,似乎是进程。 StartInfo.UseShellExecute 应该设置为 true

感谢大家的帮助!干杯!

答案 2 :(得分:0)

string filePath = @"C:\Windows\System32\notepad.exe";
ProcessStartInfo pStartInfo = new ProcessStartInfo(filePath);

**pStartInfo.UseShellExecute = true;** 

pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;   
Process.Start(pStartInfo);

注意:pStartInfo.UseShellExecute设置为true,否则会出错