简体   繁体   中英

How can I change two items in one string via Windows command prompt and C#

Total newbie at C# and Windows command prompt, so please be patient with me.

This is my first time to write code to create an executable designed to alter the registry, as well as, the preferences in Chrome.

A little background first. The engineers at the company that I am contracted to, use an old program called Cadkey, which is used to view files of things that the company has been manufacturing since the 40s.

As many of you probably know, Chrome no longer allows for applets and other type of files to be viewed in the browser for security purposes, but most engineers at this company would rather use Chrome than IE.

As a result, I have been charged with the task of giving them the ability to open the file via an "application link" and some have also referred to this as "custom url protocol" like the following example:

<a href="cadkey://<some domain>/<some drive>/<some directory>/<some file name>.prt">some file</a>

This allows the engineers to click on the file name in the browser, which then opens the file in the program Cadkey.

To accomplish this, I have to register the key within the user's registry, as well as, alter the preference file of Chrome, so that they are not bothered with the little window that alerts them that this file is about to use the Windows command prompt. I had no issue with the later, but my boss wanted the process to be as smooth as possible.

With all of this said, I was able to accomplish this with the following code:

using System;
using System.IO;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;

namespace CadkeyRegAndPrefs
{
    class Program
    {
        static void Main()
        {
            try
            {
                RegistryKey hkCurUsr = Registry.CurrentUser.OpenSubKey("Software\\Classes", true);
                // Create a subkey named cadkey under HKEY_CURRENT_USER.
                RegistryKey cadkey = hkCurUsr.CreateSubKey("cadkey", true);
                cadkey.SetValue("", "URL:cadkey Protocol");
                cadkey.SetValue("URL Protocol", "");

                // Create data for the defltIcn subkey.
                RegistryKey cadkeyDefltIcn = cadkey.CreateSubKey("DefaultIcon", true);
                cadkeyDefltIcn.SetValue("", "");
                cadkeyDefltIcn.SetValue("C:\\CK19\\Ckwin.exe", "-1");

                // Create data for the cadkeyShell subkey.
                RegistryKey cadkeyShell = cadkey.CreateSubKey("shell", true);
                RegistryKey cadkeyShellOpen = cadkeyShell.CreateSubKey("open", true);

                // Create data for the cadkeyCommand subkey.
                RegistryKey cadkeyCommand = cadkeyShellOpen.CreateSubKey("command", true);
                cadkeyCommand.SetValue("", "");
                cadkeyCommand.SetValue("", "cmd /V:ON /C \"SET r=%1 & start C:\\CK19\\Ckwin.exe !r:cadkey:=!\"");

                // Retrieve path of the current user
                string path = System.Environment.ExpandEnvironmentVariables("%userprofile%");
                string pathToPrefs = path + "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Preferences";

                // Have to create a JObject of the json file
                JObject jsonObj = JObject.Parse(File.ReadAllText(pathToPrefs));

                // Determine if the user has a protocol handler set and append cadkey set to false, otherwise, create node and set cadkey to false
                var isExlcudedSchemes = jsonObj.SelectToken("protocol_handler.excluded_schemes");
                if (isExlcudedSchemes != null)
                {
                    jsonObj["protocol_handler"]["excluded_schemes"]["cadkey"] = false;
                } else {
                    jsonObj.Add(new JProperty("protocol_handler", new JObject(
                        new JProperty("excluded_schemes", new JObject(
                            new JProperty("cadkey", new JObject()))))));

                    jsonObj["protocol_handler"]["excluded_schemes"]["cadkey"] = false;
                }

                // set the variable output and write the json content to the preferences file for Chrome
                string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(pathToPrefs, output);

                // Let the end user know that the operation was successful
                Console.WriteLine("Cadkey registration installed successfully");
                Console.WriteLine("\nPress any key to exit");
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e.ToString());
            }
        }
    }
}

The engineers have a little bootstrap modal, which gives them the instructions to download the exe, followed by double clicking the exe to install the registry keys and alter the Chrome prefs.

So what's the problem? The code registers the keys to the user's registry, as well as altering the Chrome preferences file, along with informing the user at the end that it was successful.

The problem is that some of the files have a space in the name, which then makes Cadkey prompt the user that the file cannot be found. Mainly because "%20" appears in the place of the space. I guess Cadkey was made at a time when urlencoding was not part of the design.

I have tried to alter the url via the command prompt, as I do with removing the string "cadkey:" from the param being passed to the command prompt:

cmd /V:ON /C \"SET r=%1 & start C:\\CK19\\Ckwin.exe !r:cadkey:=! & !r:%20= !\"

I have tried:

cmd /V:ON /C \"SET r=%1 & start C:\\CK19\\Ckwin.exe !r:cadkey:=! | !r:%20= !\"

I have tried:

cmd /V:ON /C \"SET r=%1 & start C:\\CK19\\Ckwin.exe !r:cadkey:=! & !r:%%20= !\"

I have tried using another var

cmd /V:ON /C \"SET r=%1 & !r:cadkey:=! & SET s=r start C:\\CK19\\Ckwin.exe !s:%20= !\"

While the command is successful in replace the string "cadkey:" - I have yet to replace both strings at the same time. I have tried too many things, but I am a newbie at this, any help would be appreciated.

Thanks in advance

After working with my boss on this last night, we finally found an answer, which is the following:

Change

cadkeyCommand.SetValue("", "cmd /V:ON /C \"SET r=%1 & start C:\\CK19\\Ckwin.exe !r:cadkey:=!\"");

To:

cadkeyCommand.SetValue("", "cmd /V:ON /C \"SET r=%1 & SET s=!r:cadkey:= ! & SET t=!s:%%20= ! & start C:\\CK19\\Ckwin.exe !t!\"");

The result is that the both the string "cadkey:" and the string "%20" are removed, with the string "%20" replaced by a space, which results in the following being passed to cadkey where you see the variable "t"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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