简体   繁体   English

C#从不同的网址打开同一应用程序并传递参数

[英]C# Open Same Application From Different Url and pass parameters

The steps of my application are: 我的应用程序的步骤是:

  1. Go to the setting page first, and the setting page will register the Registry Log (as 'regedit' in command line) in the background (people may seldom go to the setting page). 首先转到设置页面,然后设置页面将在后台注册注册表日志(在命令行中为“ regedit”)(人们很少会进入设置页面)。
  2. When users clicks the URL in a web page, it will trigger the registry and open my application. 当用户单击网页中的URL时,它将触发注册表并打开我的应用程序。
  3. The appplication reads the parameter that it gets and does things depending on the parameter value. 该应用程序读取它获取的参数,并根据参数值执行操作。
  4. User may click on different links to send different parameters to my application 用户可以单击不同的链接将不同的参数发送到我的应用程序
  5. That is, if the application is not opened, it should be launched it and reads the parameter. 也就是说,如果未打开应用程序,则应将其启动并读取参数。 If the application is already opened, it should just read the parameter. 如果应用程序已经打开,则应只读取参数。

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. 我将使用在该答案中指定的FileWatcherMemory 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

I found another way to solve my problem, 我找到了解决问题的另一种方法,

just simply add 'static' before Mutex . 只需在Mutex之前添加“ static”即可。

See the detail: C# static 查看详细信息: C#静态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM