【发布时间】:2020-12-20 14:49:48
【问题描述】:
我有一个在 IIS 中运行的 Web 应用程序,它正在使用 .cmd 文件启动一个进程。 .bat 文件启动 dotnet 核心应用程序。
这在我的本地安装上运行良好,但是当我在服务器上部署时出现以下错误:
未能将“C:\Users\Default.dotnet\tools”添加到 PATH 环境 多变的。将此目录添加到您的 PATH 以使用安装的工具 'dotnet 工具安装'。
dotnet core 和 dotnet core sdk 已安装。如果我从命令提示符运行 .cmd 文件,它可以在服务器上正常工作。
我已经搜索过,但找不到任何有关如何解决此问题的提示。
private void RunMyCmd(string runMyCmdPath)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo(runMyCmdPath);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardInput = true;
process = Process.Start(processInfo);
StreamWriter writer = process.StandardInput;
//string output = reader.ReadToEnd();
writer.WriteLine("a");
// Write the redirected output to this application's window.
// Console.WriteLine(output);
string output1 = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
// *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2
exitCode = process.ExitCode;
if (exitCode != 0)
errorLogger.Error(string.Format("RunMyCmd: ExitCode({0}) - {1}", exitCode, error));
process.Close();
}
编辑:.cmd 文件代码:
@echo off
rem switch to on to test
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
rem echo datestamp: "%datestamp%"
rem echo timestamp: "%timestamp%"
rem echo fullstamp: "%fullstamp%"
rem Go to script's directory
cd %~dp0
rem Read filenames if needed
set rules=%~dp0rules.json
set testcases=%~dp0testcases.csv
if not exist %rules% (
echo file not found. Type in full file name and Enter
set /p rules="Rules file: "
)
if not exist %rules% (
echo We did not find rules file: %rules%
exit /B 1
)
if not exist %testcases% (
echo testcases not found. Type in full file name and Enter
set /p testcases="Testcases file: "
)
if not exist %testcases% (
echo We did not find testcase file: %testcases%
exit /B 1
)
rem Create filename for results csv
set filename="results_%fullstamp%.csv"
rem Run
cd AppFolder
dotnet run --no-build %rules% %testcases% %filename%
move %filename% ../results/
PAUSE
问题解决了。
我在另一台服务器上安装了 Web 应用程序并收到以下异常:
Failed to add 'C:\Windows\system32\config\systemprofile\.dotnet\tools' to the PATH environment variable. Add this directory to your PATH to use tools installed with 'dotnet tool install'.
因为我遇到了 2 个类似的异常,所以我认为问题可能出在文件夹的权限上。
所以我将服务帐户的修改权限添加到文件夹
C:\Windows\system32\config\systemprofile
这解决了这个问题。运行应用程序后,创建了这些新文件夹:
C:\Windows\System32\config\systemprofile\.dotnet
C:\Windows\System32\config\systemprofile\.nuget
然后我在我的 prod 服务器上添加了相同的权限,但针对文件夹
C:\Users\Default
剩下的问题是为什么两台服务器将 .dotnet 文件夹定位在不同的路径中?
【问题讨论】:
-
看看here,尤其是“.NET Core CLI 尝试将默认位置添加到 PATH 环境变量....”
-
您还应该包含定义
.cmd文件的代码,并最好提供其内容。如果您不愿意这样做,请删除 [batch-file] 标签,在其当前状态下,它是无关紧要的,因为它没有被任何地方引用。 -
分配给
runMyCmdPath的字符串是什么?调用它时的工作目录是什么?另外,当可以在 .NET 中本地完成时,为什么还要使用批处理文件来生成日期/时间戳? (以及检查文件是否存在的相同问题?请求输入?运行带有参数的可执行文件?以及移动文件?). -
您无法在您的帐户 docs.microsoft.com/en-us/dotnet/core/tools/… 下安装 .NET Core Global 工具并期望它适用于所有用户(例如应用程序池标识)。如果未检测到该用户帐户,请修改您的批处理文件以安装该工具。
-
问题是你的工具没有安装,或者安装路径不对。
标签: c# batch-file iis .net-core