如何通过 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.
因此,我负责让他们能够通过 "application link" 打开文件,有些人也将其称为 "custom url protocol",如下例所示:
<a href="cadkey://<some domain>/<some drive>/<some directory>/<some file name>.prt">some file</a>
这允许工程师在浏览器中单击文件名,然后在程序 Cadkey 中打开文件。
为此,我必须在用户注册表中注册密钥,并更改 Chrome 的首选项文件,这样他们就不会被 window提醒他们该文件即将使用 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());
}
}
}
}
工程师们有一点 bootstrap 模式,它为他们提供了下载 exe 的说明,然后双击 exe 以安装注册表项并更改 Chrome 首选项。
所以问题是什么?该代码将密钥注册到用户的注册表,并更改 Chrome 首选项文件,并在最后通知用户它已成功。
问题是某些文件的名称中有space,这使得Cadkey 提示用户找不到该文件。主要是因为“%20”出现在 space 的位置。我想 Cadkey 是在 url编码不是设计的一部分的时候制作的。
我试图通过命令提示符更改 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= !\"
我试过使用另一个变量
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" 替换为 space,结果如下传递给 cadkey,您会在其中看到变量 "t"
C# 和 Windows 命令提示符的新手,所以请耐心等待。
这是我第一次编写代码来创建旨在更改注册表以及 Chrome 中的首选项的可执行文件。
先介绍一下背景。与我签约的公司的工程师使用一个名为 Cadkey 的旧程序,该程序用于查看公司自 40 年代以来一直在制造的东西的文件。
你们中的许多人可能都知道,出于安全考虑,Chrome 不再允许在浏览器中查看小程序和其他类型的文件,但该公司的大多数工程师宁愿使用 Chrome 比 IE.
因此,我负责让他们能够通过 "application link" 打开文件,有些人也将其称为 "custom url protocol",如下例所示:
<a href="cadkey://<some domain>/<some drive>/<some directory>/<some file name>.prt">some file</a>
这允许工程师在浏览器中单击文件名,然后在程序 Cadkey 中打开文件。
为此,我必须在用户注册表中注册密钥,并更改 Chrome 的首选项文件,这样他们就不会被 window提醒他们该文件即将使用 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());
}
}
}
}
工程师们有一点 bootstrap 模式,它为他们提供了下载 exe 的说明,然后双击 exe 以安装注册表项并更改 Chrome 首选项。
所以问题是什么?该代码将密钥注册到用户的注册表,并更改 Chrome 首选项文件,并在最后通知用户它已成功。
问题是某些文件的名称中有space,这使得Cadkey 提示用户找不到该文件。主要是因为“%20”出现在 space 的位置。我想 Cadkey 是在 url编码不是设计的一部分的时候制作的。
我试图通过命令提示符更改 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= !\"
我试过使用另一个变量
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" 替换为 space,结果如下传递给 cadkey,您会在其中看到变量 "t"