c# - 如何使用 .NET Core 2.1 中的 Process.Start 使用默认程序打开网络共享驱动器上的文件
问题描述
我有点困惑。我已经构建了一个 Intranet 应用程序,该应用程序具有允许用户将文件“上传”到网络共享驱动器的功能。这不是问题,因为我能够成功地将文件复制到指定文件夹。复制文件后,下一个要求是让用户单击附件的链接并使用默认程序打开文件(即 Notepad.exe 用于 .txt 扩展名等)。使用 Process.Start 我已经在我的 localhost 机器上成功演示了这一点,但是,当应用程序发布到主机服务器时会出现问题,因为它最初给出了“访问被拒绝”错误。设置网络文件夹后' s 接受服务帐户的权限并将应用程序池标识更改为该服务帐户,它不再引发权限错误。事实上,它根本不会引发错误。就像在本地主机上一样,它一直通过函数,但它显然没有打开文件,而不是什么都不做。至少在我看来,好像应用程序“认为”它已经打开了程序,因此继续前进,好像没有任何问题。我在这里真的很茫然。我显然错过了一些东西,但我一生都无法弄清楚。
这是打开文件的函数:
ActivityLog activityLog = new ActivityLog();
FileInfo fileInfo = new FileInfo(filePath);
Process process = new Process();
try
{
if (fileInfo.Exists)
{
//I've used both UseShellExecute and the
//command line arguments with the same results.
/*
process.StartInfo = new ProcessStartInfo
{
FileName = fileInfo.FullName,
WorkingDirectory = fileInfo.DirectoryName,
UseShellExecute = true
};
*/
process.StartInfo = new ProcessStartInfo("cmd", $"/c start " + fileInfo.FullName.FileStringToUri());
process.Start();
}
else throw new Exception("FileInfo not found | File Path: " + filePath);
}
catch (Exception e)
{
if (fileInfo.Exists)
activityLog.LogErrorAsync(new Exception("File Path: " + fileInfo.FullName + " | " + e.Message, e));
else
activityLog.LogErrorAsync(e);
throw e;
}
FileStringToUri()
是一个自定义扩展,它本质上创建了一个类似于 的 uri 字符串,但如果使用该方法file://{file}/{path}
,则不需要此扩展。UseShellExecute
解决方案
收盘票。按原样回答,在评论中。谢谢@damien-the-unbeliever
推荐阅读
- class - Flutter Hot Restart - 怀疑它错误地填充了类实例
- lightweight-charts - 如何更改直方图条的粗细?
- javascript - 如何在 Javascript 中将形状移动到前景或背景?
- python - SQLITE_MAX_VARIABLE_NUMBER 将 sql 查询增加或分解成块
- javascript - 无法为我的 javascript 代码读取未定义的属性“rowIndex”
- python - uwsgi无法安装
- xpath - 选择 1) 在给定节点之前但 2) 也是另一个给定节点的后代的 `text()`
- javascript - ResponsiveVoice 中的“onend”并不意味着在播放文本之后?
- algorithm - 查找字符串中包含的所有子字符串的最快方法是什么?
- unity3d - 辅助“呼吸”字体大小(使用协程增加和减少)