简体   繁体   English

从命令提示符运行控制台应用程序

[英]Run console application from the command prompt

I created a console application using Jetbrains rider.我使用 Jetbrains Rider 创建了一个控制台应用程序。 I then published the console application to a folder.然后我将控制台应用程序发布到一个文件夹。 I added the folder to my System path.我将该文件夹添加到我的系统路径中。 I can run echo %PATH% and see that the folder is in the path however I can only run the console application if I am inside the folder.我可以运行echo %PATH%并看到文件夹在路径中,但是如果我在文件夹中,我只能运行控制台应用程序。 It doesn't event work if I put the full path to the executable.如果我将完整路径放入可执行文件,它就不起作用。

C:\Program Files<Company Name><Program Name><files> C:\Program Files<公司名称><程序名称><文件>

I also checked the files permissions against another file and that seems to be the same.我还检查了另一个文件的文件权限,这似乎是一样的。 I disabled a virus protection and that didn't fix the issue either.我禁用了病毒保护,但也没有解决问题。

Any thoughts on why this will not run?关于为什么这不会运行的任何想法? I am running windows 10.我正在运行 windows 10。

Solution解决方案

Set the base path while creating the applications configuration.在创建应用程序配置时设置基本路径。

builder
    .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location)) // set the base path
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

Your application is not good coded.您的应用程序编码不好。 It most likely references other files stored in the directory of the executable without using any path which means with referencing the files using a path relative to current directory.它很可能在不使用任何路径的情况下引用存储在可执行文件目录中的其他文件,这意味着使用相对于当前目录的路径引用文件。

See the Microsoft documentation about Naming Files, Paths, and Namespaces .请参阅有关命名文件、路径和命名空间的 Microsoft 文档。

The current directory for your application can be by chance the directory containing the application executable, but there are thousands of other directories which can be the current directory on starting the application.您的应用程序的当前目录可能是包含应用程序可执行文件的目录,但还有数以千计的其他目录可能是启动应用程序时的当前目录。

Every programming/scripting language has a method to determine/get the full qualified path of itself, ie in which directory the currently executed executable or interpreted script file is stored which is called the application directory or the program files directory or the script directory .每种编程/脚本语言都有一种方法来确定/获取其自身的完全限定路径,即当前执行的可执行或解释的脚本文件存储在哪个目录中,称为应用程序目录程序文件目录脚本目录

This path must be concatenated with the file names of other files in same directory as the executable/script or a subdirectory of the application/script directory.此路径必须与可执行文件/脚本所在目录或应用程序/脚本目录的子目录中的其他文件的文件名连接。 Then it does no longer matter for the executable/script which directory is the current directory as it references itself all its files/folders with full qualified name.那么对于可执行文件/脚本来说,哪个目录是当前目录就不再重要了,因为它引用了它自己的所有文件/文件夹的全名。

The solution could be using in code:该解决方案可以在代码中使用:

builder.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location))

See also: How can I get the application's path in a .NET console application?另请参阅:如何在 .NET 控制台应用程序中获取应用程序的路径?

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

相关问题 在开发人员命令提示符下运行控制台应用程序 - Run Console application under developer command prompt 如何使用Visual Studio工具栏在命令提示窗口上运行控制台应用程序 - How to run console application on command prompt window with visual studio Toolbar 从visual studio命令提示符运行nuget包管理器控制台 - Run nuget package manager console from visual studio command prompt 从控制台应用程序获取响应(Windows中的命令提示符) - Get Response from Console Application (command prompt in windows) 在控制台应用程序中处理后退出命令提示符 - Exit command prompt after processing in a console application 从命令提示符运行控制台应用程序,而无需启动新的控制台窗口 - Running a console application from the command prompt without it launching a new console window 如何在Visual Studio输出窗口中运行控制台应用程序,而不是打开新的命令提示符? - How do you run a console application in the Visual Studio output window, instead of opening a new command prompt? 从命令提示符运行dotnet - dotnet run from Command Prompt 从Web应用程序调用(.exe)控制台应用程序时,命令提示符不会打开c# - command prompt do not opens up when calling (.exe) console application from web application c# C#-从命令行全局运行控制台应用程序 - C# - Run a console application from command line globally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM