- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
C# 和 Windows 命令提示符方面的新手,所以请耐心等待。
这是我第一次编写代码来创建旨在更改注册表以及 Chrome 中的首选项的可执行文件。
先介绍一下背景。与我签约的公司的工程师使用一个名为 Cadkey 的旧程序,该程序用于查看公司自 40 年代以来一直在制造的东西的文件。
你们中的许多人可能都知道,出于安全考虑,Chrome 不再允许在浏览器中查看小程序和其他类型的文件,但该公司的大多数工程师宁愿使用 Chrome 而不是 IE。
因此,我负责让他们能够通过“应用程序链接”打开文件,有些人也将其称为“自定义 url 协议(protocol)”,如下例所示:
<a href="cadkey://<some domain>/<some drive>/<some directory>/<some file name>.prt">some file</a>
这允许工程师在浏览器中单击文件名,然后在程序 Cadkey 中打开文件。
为此,我必须在用户的注册表中注册 key ,并更改 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());
}
}
}
}
工程师有一个小的 Bootstrap 模式,它为他们提供了下载 exe 的说明,然后双击 exe 以安装注册表项并更改 Chrome 首选项。
那么问题是什么?该代码将 key 注册到用户注册表,并更改 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= !\"
我尝试过使用另一个变量
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”
关于c# - 如何通过 Windows 命令提示符和 C# 更改一个字符串中的两项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59007788/
有什么方法可以自定义javascript提示框吗? 我想让我的提示只接受数字输入...(我想绑定(bind) keyup 事件 - 具体来说) 这可能吗? 最佳答案 默认的 javascript 提示
对我来说,现在是 bash-prompt-fiddling-time(总有一天……)。 我正在尝试获取两行提示: 第一行包含位置信息,背景颜色到行尾 第 2 行包含时间和上一个命令的退出代码 我快到了
大家好,我只是在学习 javascript,不过我有一些 html 和 css 背景。 我不知道如何只允许在提示中输入数字值,或者检查它是否是数字,如果不是则重新提示。文本框也是如此。 最佳答案 这是
编辑:原标题说“bash prompt”,但我使用“zsh”。我接受了@Artur R. Czechowski 的回答,因为它在 bash 上下文中是正确的,并且我能够在他的帮助下完成我的工作。 PR
是否可以配置stack ghci类似于ghci的方式通过 ~/.ghci 配置文件? 目前stack ghci不接受 ~/.ghci 中的设置文件。 我正在研究将提示设置为 lambda 而不是加载的
我喜欢在我的 PowerShell 提示符中包含 Mercurial/Git 存储库状态。有时这会减慢 prompt()功能相当大,导致长时间等待,直到您可以开始在 shell 中输入。我想知道,是否
我正在编写一个用于与远程服务交互的 PowerShell 模块。连接到远程服务时(通过模块中的函数),我想在提示符前添加用户名。断开连接后,我想删除用户名。 我以为我可以通过复制全局 prompt 来
我用java开发了我的远程屏幕共享软件。它工作正常,但是当使用 UAC 时,屏幕上会出现允许/禁止提示,但远程屏幕看不到它。所以每次用户都必须手动允许它。我想知道如何摆脱这个问题。 最佳答案 这正是
我有一个初始化脚本,它设置了几个变量供以后使用。最后一行定义了一个“提示”函数,我希望它能改变我的提示,作为初始化已经完成的指示。变量已设置,因此它不作为子进程运行,但我的提示顽固地停留在“PS”。出
我有一个与此类似的模板提示: prompt = f""" Write a poem about the topic delimited by triple backticks if it starts
我有一个与此类似的模板提示: prompt = f""" Write a poem about the topic delimited by triple backticks if it starts
我需要一些帮助才能恢复提示,现在我正在执行此 block ,不要让我恢复提示,我在运行末尾使用 &将此命令作为后台任务。执行脚本后如何返回提示符。 #!/bin/sh sudo su - user <
如果我的 html 中有一个简单的按钮: Start Game 我想要在我的 JS 中提示: var userAdjective = prompt("Please provide an Adjecti
我想提示用户在网页上进行一些输入。 我无法使用内置的 Javascript prompt 函数,因为它会在 IE 中创建安全警告。 是否有人们用来重新创建此内容的最佳实践/方法/示例? Javascr
我想将我的 bash 提示设置为固定宽度,并弥补 $ 之前的空格差异,因此无论长短,我的提示都保持相同的宽度: [name@host] ~/Directory/Dir...Another/LastDi
我有一个脚本可以读取用户的输入。这是我的代码: if [ -z $volreadexists ]; then echo -e "\tThis will overwrite the en
我的 ZSH 主题使用 .zsh-theme 文件中的 RPROMPT='$(vbox_status)' 输出我的 Vagrant/VBox VM 的状态(其中 vbox_status调用 a scr
我经常使用 vim,并且经常发现使用 !bash 进入命令行很有用。但是,我需要键入 exit 才能返回到 vim,有时我不确定我是否在子 shell 中或者这是否会关闭我的 session 。 我真
问题很简单。我想在我的 bash 脚本中评估 PS1 的当前值。 Google 上的所有资料都指向有关如何改进它的教程,但我想评估一下我当前的终端或至少 一些 终端将如何呈现它。 是否有任何软件/功能
如何在打印后台函数的输出后自动返回到我的 bash 提示符? 例如,当我在 bash shell 中运行以下脚本时: fn(){ sleep 10 echo "Done"
我是一名优秀的程序员,十分优秀!