Location>code7788 >text

Bringing up native C# programs on a web page

Popularity:654 ℃/2024-11-14 17:14:14

preamble

I've recently started organizing my inventory drafts in my notes, and this article was created in May '23 (probably more than that since I moved to onedrive halfway through the year)

Web page to start the computer program is often used in the scene, such as Baidu.com disk to download, join the QQ group and so on.

I have done a project to manage movies deployed on NAS for my own use, I need to realize the web page to call the Potplayer on the computer to play the movie with one click, this time directly out of C# to write a client is very convenient!

registry operation

On Windows, the way to do this is to add the Scheme and the corresponding program to the registry. I haven't looked into it for other systems because I don't need it at the moment, but I guess it's similar.

It needs to be configured.SchemePrefix The example in this article is demo

On the web, use thedemo:// The link at the beginning will evoke the local program~

using ;
using ;
using Microsoft.Win32;

const string AppName = "DemoApp";
const string SchemePrefix = "demo";

// Initialize the registry
void InitReg() {
    if (!()) return;

    var path1 = AppName;
    var path2 = $@"{path1}\shell\open\command";

    // Setting the protocol name
    var key1 = (path1, true);
    if (key1 == null) {
        key1 = (path1);
    }

    ("URL Protocol", "");
    (null, $"URL:{SchemePrefix}");

    var key2 = (path2, true);
    if (key2 == null) {
        key2 = (path2);
    }

    var exePath = ?? "";

    (null, $"\"{exePath}\" \"%1\"");
}

parameter analysis

Since I wrote the widget on the fly, I didn't use the libraries parsed from the command line either

It would be more elegant to use third-party library code

There are two commands, install and open.

Manually executing install will add configuration to the registry, after which the program file should not be moved, and the program should be executed for subsequent web page launches.

The open command is executed when the web page is launched. Note that the characters in the command parameters need to be URL-escaped.

string action = "", value = "";
string[] cmdArgs = ();
if ( > 1) {
    var arg = cmdArgs[1];
    ($"cmd args: {arg}");

    if (($"{SchemePrefix}://")) {
        arg = ($"{SchemePrefix}://", "");
    }

    if (("/")) {
        arg = (0, - 1);
    }

    var split = ("//");
    action = split[0];
    value = > 1 ? split[1] : "";
    ($"action: {action}, value: {value}");
}

switch (action) {
    case "install":
        ("init reg...");
        InitReg();
        ("init reg finished.");
        break;
    case "open":
        var path = (value);
        ($"open file/dir: {path}");
        if (())
            ($"C:\\Windows\\", path);
        if (())
            ("xdg-open", path);
        break;
    default:
        ("I don't know what to do.~");
        break;
}

bibliography

  • How to open local apps on the web -/a/1190000040237895
  • C# to perform registry and key operations -/p/403162888