简体   繁体   English

不论可执行文件的位置在命令提示符下运行C#程序?

[英]Running C# Program in Command Prompt regardless of the location of the Executable?

I would like to integrate my program into the Command Interpreter in Windows 10 (preferably without registry edits). 我想将程序集成到Windows 10中的Command Interpreter中(最好不进行注册表编辑)。 I have done minimal work with Libraries, but I still could write a DLL if that is necessary in any way. 我对库所做的工作很少,但是如果有任何必要,我仍然可以编写DLL。 I would like to run my program, lets call it "speedtest" for this example. 我想运行我的程序,在本示例中将其称为“ speedtest”。 I would like to type the following in the Command Prompt to run it: 我想在命令提示符中键入以下内容以运行它:

speedtest

Much like entering "py" into the Command Prompt with Python installed will open the Shell environment, I would like to do the same thing with my programs. 就像在安装了Python的命令提示符中输入“ py”会打开Shell环境一样,我也想对我的程序做同样的事情。 Any way to do this easily? 有什么办法可以轻松做到这一点吗?

Add your utility's Path to the environment variables list. 将实用程序的路径添加到环境变量列表。

To do so, in Windows 10 and Windows 8 为此,请在Windows 10和Windows 8中

  • In Search, search for "System" and then select: System (Control Panel) Click the Advanced system settings link. 在搜索中,搜索“系统”,然后选择:系统(控制面板)单击“高级系统设置”链接。
  • Click Environment Variables. 单击环境变量。 In the section System Variables, find the PATH environment variable and select it. 在“系统变量”部分中,找到PATH环境变量并选择它。 Click Edit. 单击编辑。 If the PATH environment variable does not exist, click New. 如果PATH环境变量不存在,请单击“新建”。
  • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. 在“编辑系统变量(或新系统变量)”窗口中,指定PATH环境变量的值。 Click OK. 单击确定。 Close all remaining windows by clicking OK. 单击确定关闭所有剩余的窗口。

All python and similar programs are doing is adding the location of the executable to the PATH environment variable (while that may or may not be stored in the registry, its not exactly a registry edit, you can do it through the Windows GUI). 所有python和类似程序正在做的就是将可执行文件的位置添加到PATH环境变量中(尽管该位置可能会或可能不会存储在注册表中,但它并不完全是注册表编辑,您可以通过Windows GUI来执行此操作)。

You will discover this when the automatic process fails for some reason because you have to add it to PATH yourself. 当自动处理由于某种原因失败时,您将发现此错误,因为您必须自己将其添加到PATH。

The command interpreter is very simple, it looks in the current directory for the command you input and then checks PATH. 命令解释器非常简单,它在当前目录中查找您输入的命令,然后检查PATH。 If there isn't a program with the same name as your command in either of those places, it will throw an error. 如果在任何一个地方都没有与您的命令同名的程序,它将引发错误。

There just isn't any other way to hack it. 只是没有其他方法可以破解它。 Either add your program's location to PATH or write an installer to do it for you. 您可以将程序的位置添加到PATH或编写安装程序来为您完成此操作。

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

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