简体   繁体   English

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

[英]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. C#和Windows命令提示符下的新手,所以请耐心等待。

This is my first time to write code to create an executable designed to alter the registry, as well as, the preferences in Chrome. 这是我第一次编写代码来创建旨在更改注册表以及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. 与我签约的公司的工程师使用的是一个名为Cadkey的旧程序,该程序用于查看自40年代以来该公司一直在制造的东西的文件。

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. 众所周知,Chrome浏览器不再允许出于安全目的在浏览器中查看小程序和其他类型的文件,但是该公司的大多数工程师宁愿使用Chrome而不是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. 这使工程师可以在浏览器中单击文件名,然后在程序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. 为此,我必须在用户注册表中注册密钥,以及更改Chrome的首选项文件,以使它们不会被提醒他们该文件即将使用Windows命令提示符的小窗口所困扰。 。 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. 工程师有一些引导程序模式,可以为他们提供下载exe的说明,然后双击该exe安装注册表项并更改Chrome偏好设置。

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. 该代码将密钥注册到用户的注册表,并更改Chrome偏好设置文件,并在最后通知用户成功。

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. 问题在于某些文件的名称中有一个空格,这会使Cadkey提示用户找不到该文件。 Mainly because "%20" appears in the place of the space. 主要是因为在空间的位置出现了“%20”。 I guess Cadkey was made at a time when urlencoding was not part of the design. 我猜Cadkey是在urlencoding不在设计中的时候制作的。

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

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 我尝试使用另一个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. 当命令成功替换字符串“ cadkey:”时-我尚未同时替换两个字符串。 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" 结果是删除了字符串“ cadkey:”和字符串“%20”,并用空格替换了字符串“%20”,这导致将以下内容传递给cadkey,在此您看到变量“ t” ”

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

相关问题 通过命令提示符“ C#”安装和卸载Windows服务 - Install and uninstall windows service via command prompt “C#” 如何与C#代码中的命令提示符进行交互? - How can I interact with the command prompt from C# code? 我可以通过命令提示符向C#程序提供的最大输入长度是多少? - what is the Max length of input I can provide to a C# program via command prompt? [C#] [XNA 3.1]如何在一个Windows窗体中托管两个不同的XNA窗口? - [C#][XNA 3.1] How can I host two different XNA windows inside one Windows Form? 如何通过c#传递多个命令行参数 - How can I pass more than one command line argument via c# 如何打开命令提示符并通过C#运行CasperJS脚本? - How do I open up Command Prompt and run a CasperJS script via C#? 如何在Windows 8应用程序的C#中的SelectionChanged方法中通过按钮更改Uri? - how can i change a Uri via button in a SelectionChanged method in c# for windows 8 application? 如何在C#for .NET中从Windows窗体应用程序发送命令提示符参数? - How Do I Send Command Prompt Arguments from a Windows Form Application in C# for .NET? 如何通过C#向Linux系统发出命令? - How can I issue a command to a linux system via c#? 如何使用C#更改命令提示符中的目录位置? - how to change the directory location in command prompt using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM