4

我有一个启动另一个应用程序的 AutoUpdater 应用程序。是否有强制第二个应用程序仅在由 AutoUpdater 启动的情况下运行?

问题是,一些最终用户会在他们的桌面上创建主应用程序文件的快捷方式。这成为一个问题,因为它应该在启动之前检查应用程序的更新,并且他们没有收到我们的更新。

我的一个想法是通过 WCF 创建一个 IPC 通道,然后从 AutoUpdater 向另一个应用程序发出一个非常简单的命令。如果其他应用程序在 2 或 3 秒内没有收到该命令,它将自行关闭。

这似乎比应该需要的更多的代码/开销。有没有更简单的方法?

谢谢!

4

2 回答 2

6

Windows forms applications also have a main method, which can take parameters. You could read some parameter, and if it doesn't conform to your rules, you can either keep the form from opening (therefore the user wouldn't see a thing), or you can chastise the user, I mean, give a message that they shouldn't open your app that way. I think this is simpler than using WCF.

于 2013-05-17T22:29:56.837 回答
2

您可以使用运行您的应用程序

System.Diagnostics.Process.Start("", "TestString");

第一个字符串是您要启动的应用程序的路径,例如“C:/AppName.exe”,第二个字符串是您想要的任何文本,但它必须与您在下面的“if”中检查的字符串相同。在应用程序中,您需要检查通过第二个字符串给出的文本。

static void Main(string[] args)
{
    if(!args.Length == 1 || !args[0]=="TestString") Environment.Exit(0);
    //↑ lenght check to avoid exception, then check for the string itself
    //the OS gives the string to args, you don't need to take care about that

    //rest of your code
}

我希望我能帮助解决它。

于 2013-05-17T23:07:15.107 回答