繁体   English   中英

如何通过Windows命令提示符和C#在一个字符串中更改两个项目

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

C#和Windows命令提示符下的新手,所以请耐心等待。

这是我第一次编写代码来创建旨在更改注册表以及Chrome中的首选项的可执行文件。

首先有一点背景。 与我签约的公司的工程师使用的是一个名为Cadkey的旧程序,该程序用于查看自40年代以来该公司一直在制造的东西的文件。

众所周知,Chrome浏览器不再允许出于安全目的在浏览器中查看小程序和其他类型的文件,但是该公司的大多数工程师宁愿使用Chrome而不是IE。

结果,我承担了使他们能够通过“应用程序链接”打开文件的任务,有些人也将其称为“自定义网址协议”,例如以下示例:

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

这使工程师可以在浏览器中单击文件名,然后在程序Cadkey中打开文件。

为此,我必须在用户注册表中注册密钥,以及更改Chrome的首选项文件,以使它们不会被提醒他们该文件即将使用Windows命令提示符的小窗口所困扰。 。 稍后我没有问题,但老板希望过程尽可能顺利。

综上所述,我可以使用以下代码完成此任务:

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());
            }
        }
    }
}

工程师有一些引导程序模式,可以为他们提供下载exe的说明,然后双击该exe安装注册表项并更改Chrome偏好设置。

所以有什么问题? 该代码将密钥注册到用户的注册表,并更改Chrome偏好设置文件,并在最后通知用户成功。

问题在于某些文件的名称中有一个空格,这会使Cadkey提示用户找不到该文件。 主要是因为在空间的位置出现了“%20”。 我猜Cadkey是在urlencoding不在设计中的时候制作的。

我试图通过命令提示符更改URL,就像从传递给命令提示符的参数中删除字符串“ cadkey:”一样:

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

我试过了:

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

我试过了:

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

我尝试使用另一个var

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

当命令成功替换字符串“ cadkey:”时-我尚未同时替换两个字符串。 我尝试了太多事情,但是我是一个新手,任何帮助将不胜感激。

提前致谢

在昨晚与老板一起工作之后,我们终于找到了答案,如下所示:

更改

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

至:

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

结果是删除了字符串“ cadkey:”和字符串“%20”,并用空格替换了字符串“%20”,这导致将以下内容传递给cadkey,在此您看到变量“ t” ”

暂无
暂无

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

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