簡體   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