.net - 在后台启动 Windows 应用程序

标签 .net c# winforms

我有一个使用 winforms 用 C# 编写的 Windows 应用程序。我想确保每当有人使用 process.start 从任何其他应用程序启动它时,UI 都不会显示,应用程序应该静默启动。

我无法控制其他应用程序,所以我不能使用:

var p = new Process();
p.StartInfo = new ProcessStartInfo(); 
p.StartInfo.UseShellExecute = true; 
p.StartInfo.WorkingDirectory = ConfigurationManager.AppSettings["exeFolder"].ToString(); p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; 
p.StartInfo.FileName = ConfigurationManager.AppSettings["exeName"].ToString(); 
p.Start();

最佳答案

感谢您的回复。很抱歉没有早点提供我的解决方案。我已经解决了问题并记录了解决方案供其他人将来使用。

您可以找到解决方案 here .

如何 · 新建一个windows项目,删除默认窗体(Form1)。 · 在 Program.cs 中创建一个新类并从 Form 继承它。 · 请引用下面的代码。 · 现在更改 Main 方法。在 Application.Run 中,将启动对象从 Form1 更改为 ApplicationStartUp。

using System;
using System.Drawing;
using System.Windows.Forms;
using BackgroundApp.Properties;

namespace BackgroundApp
{
    static class Program
    {
        /// <summary>


   /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new ApplicationStartUp());
    }
}

public class ApplicationStartUp : Form
{
    private NotifyIcon trayIcon;
    private ContextMenu trayMenu;

    private void InitializeComponent()
    {
        trayMenu = new ContextMenu();
        trayMenu.MenuItems.Add("Exit", OnExit);
        trayIcon = new NotifyIcon();
        trayIcon.Text = Resources.TrayIcon;
        trayIcon.Icon = new                            Icon(global::BackgroundApp.Properties.Resources.IntegratedServer, 40, 40);
        trayIcon.ContextMenu = trayMenu;
        trayIcon.Visible = true;
    }

    //Ctor
    public ApplicationStartUp()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        Visible = false;
        ShowInTaskbar = false;
        base.OnLoad(e);
    }

    private void OnExit(object sender, EventArgs e)
    {
        // Release the icon resource.
        trayIcon.Dispose();
        Application.Exit();
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            // Release the icon resource.
            trayIcon.Dispose();
        }
        base.Dispose(isDisposing);
    }
}

关于.net - 在后台启动 Windows 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7872302/

相关文章:

.net - Ftp GetFileSize 随机抛出 FTP 错误 503(命令序列错误)

c# - NCron 默认记录器写入事件日志,抛出 SecurityException

c# - 将 XML 数据导入组合框

.net - 从 CheckedListBox.Items 绑定(bind)启用属性 - Winforms

c# - 在 ASP.NET 后端查找方法

.net - 你能用 .net reactor 混淆 LinqToSql 程序集而不破坏它们吗?

c# - 构造函数/析构函数链接错误

c# - 需要来自苹果的 iOS 崩溃日志的建议

c# - 编程技能测试仪(问题)v2.0

c# - 在其他线程加载数据时显示加载动画