【发布时间】:2020-01-25 06:48:18
【问题描述】:
我正在尝试通过从 .NET Core API 调用从 .NET Core (2.1) 控制台应用程序创建的 dll 来启动一个进程。我尝试使用创建一个流程来做到这一点。下面显示代码。
string filePath = @"C:\Projects\MyProject.Api\bin\Debug\netcoreapp2.1\";
var startInfo = new ProcessStartInfo
{
FileName = "dotnet",
WorkingDirectory = filePath,
Arguments = "ReportGeneratorApp.dll",
UseShellExecute = false,
RedirectStandardOutput = false,
RedirectStandardError = false,
CreateNoWindow = true,
};
using (Process process = new Process())
{
process.StartInfo = startInfo;
process.Start(); // process.WaitForExit();
}
在 ReportGeneratorApp 主方法中,我试图在文件系统中创建一个文件。
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine(args[i]);
}
string path = @"D:\MyTest.txt";
Console.ReadLine();
try
{
// Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
{
byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file." + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss"));
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
如果我从 cmd 运行 ReportGeneratorApp,它确实可以工作。但不是当我从 web api 调用它时。有什么线索吗?
【问题讨论】:
-
Web api 正在 IIS 或其他托管服务上运行。通常,那些托管应用程序以尽可能低的权限运行您的 Web api 和应用程序。因此,IIS 用户可能只是缺乏写入该 D 驱动器的访问权限。
标签: c# asp.net-core-webapi asp.net-core-2.1