[英]C# Open Same Application From Different Url and pass parameters
The steps of my application are: 我的应用程序的步骤是:
The problem is: how could find out the different situations of my application - whether it is opened or not - and then use the parameter correctly? 问题是:如何找出应用程序的不同情况(无论是否打开),然后正确使用参数?
The part of registry( in setting page): 注册表部分(在设置页面中):
Registry.ClassesRoot.CreateSubKey("MyApp").SetValue("", "URL:MyApp Protocol");
Registry.ClassesRoot.CreateSubKey("MyApp").SetValue("URL Protocol", "");
Registry.ClassesRoot.CreateSubKey("MyApp\\DefaultIcon").SetValue("", "\"" + Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\" + "MyApp.exe" + ",1\"");
Registry.ClassesRoot.CreateSubKey("MyApp\\shell\\open\\command").SetValue("", "\"" + Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\" + "MyApp.exe" + "\" \"%1\"");
%1 is the parameter I will get( from url to my application). %1是我将获取的参数(从url到我的应用程序)。
And the web link may be: Web链接可能是:
<a href="MyApp://function1">Call for Function 1</a>
<a href="MyApp://function2">Call for Function 2</a>
So there are many links in the web page to call same application. 因此,网页中有许多链接可以调用同一应用程序。
But I cannot let my application be opened every time (that is, there should be only one application opened, and other clicks on links will only send parameters to the app). 但是我不能每次都打开我的应用程序(也就是说,应该只打开一个应用程序,其他单击链接只会将参数发送到该应用程序)。
I know how to find out whether the application is opened or not by the code: 我知道如何通过代码查找是否打开了应用程序:
Mutex mul = null;
bool is_createdNew;
try
{
string mutexName = Process.GetCurrentProcess().MainModule.FileName.Replace(Path.DirectorySeparatorChar, '_');
mul = new Mutex(true, "Global\\" + mutexName, out is_createdNew);
if (!is_createdNew)
{
// application be opened already, I close the application originally
Environment.Exit(Environment.ExitCode);
}
else
{
// the application is first run, open my MainWindow
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
Is it possible to send the parameter as a method of registry when the application is opened? 打开应用程序时是否可以将参数作为注册方法发送?
I even think about reading registry by Registry.GetValue
, when my application starts up, 我什至考虑过通过
Registry.GetValue
读取注册表,当我的应用程序启动时,
to use timer to read registry value per second...... 使用计时器每秒读取注册表值...
This is my first time face this situation of user's request, 这是我第一次面对用户要求的情况,
hope someone can give me any direction! 希望有人可以给我任何指示!
Thanks in advance. 提前致谢。
When you find out that another instance of your application is already running (which you do in your code above using Mutex
), you can programatically pass the parameter (of the second app instance) to the first, already running app instance and then just close the second app instance. 当发现应用程序的另一个实例已经在运行时(您在上面的代码中使用
Mutex
),可以以编程方式将(第二个应用程序实例的)参数传递给第一个已经运行的应用程序实例,然后关闭第二个应用实例。 This code for passing parameter to the first app instance would then be just before Environment.Exit(Environment.ExitCode);
然后,此用于将参数传递给第一个应用程序实例的代码就在
Environment.Exit(Environment.ExitCode);
之前Environment.Exit(Environment.ExitCode);
(Presuming that your app is relatively small and does not loads lots of libraries on startup - in that case it would be better to create a separate small launcher app) (假设您的应用相对较小,并且在启动时不会加载很多库-在这种情况下,最好创建一个单独的小型启动器应用)
The problem is, how to pass the parameter between two independent processes - two instances of your app.exe
. 问题是,如何在两个独立的进程(
app.exe
两个实例)之间传递参数。 There are of course several options, look here: 当然,有几种选择,请看这里:
Send/Receive message To/From two running application 向两个正在运行的应用程序发送/接收消息
I would use FileWatcher
or Memory mapped file
as it is specified in that answer. 我将使用在该答案中指定的
FileWatcher
或Memory mapped file
。 The solution with timer and changing registry values is not good (registry operations require admin access, registry operations are not so fast etc.). 具有计时器和更改注册表值的解决方案不好(注册表操作需要管理员访问权限,注册表操作不是那么快等)。
Here is a nice library that shows, how to pass parameters between 2 processes using Memory mapped file
. 这是一个不错的库,它展示了如何使用
Memory mapped file
在两个进程之间传递参数。
http://code.msdn.microsoft.com/windowsdesktop/Inter-process-communication-e96e94e7 http://code.msdn.microsoft.com/windowsdesktop/Inter-process-communication-e96e94e7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.