我有一个使用 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/