【发布时间】:2019-03-06 17:20:10
【问题描述】:
下面的代码在 Windows 下工作,但在 Linux 下出现异常,其中用户主驱动器似乎被添加到尝试打开文件时获得的 LocalPath 之前。 如果我使用 FileStream,也会出现同样的问题。
在 RHEL 7.6 下测试
有人知道解决方法或根本原因吗?
必须在下面的 sn-ps 中使实际的文件共享匿名,所以请忽略路径中的任何拼写错误,它在 windows 上工作的事实很重要。
输出是:
IsUnc True isFile True LocalPath=\\lnasvr001.partners\dfs\Analytics\suite\demo\externaldata\DemoDataset\temp\diagnose.json AbsolutePath=/dfs/Analytics/suite/demo/externaldata/DemoDataset/temp/diagnose.json
例外是:
Unhandled Exception: System.IO.FileNotFoundException: Could not find file '/home/appuser/testapp/\\lnasvr001.partners\dfs\Analytics\suite\demo\externaldata\DemoDataset\temp\diagnose.json'.
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.File.InternalReadAllText(String path, Encoding encoding)
at System.IO.File.ReadAllText(String path)
我的代码:
static void Main(string[] args)
{
var path = "file://lnasvr001.partners/dfs/Analytics/suite/demo/externaldata/DemoDataset/temp/diagnose.json";
Uri uri = new Uri(path);
Console.WriteLine($"IsUnc {uri.IsUnc} isFile {uri.IsFile} LocalPath={uri.LocalPath} AbsolutePath={uri.AbsolutePath}");
Console.WriteLine($"File Content Length {File.ReadAllText(uri.LocalPath).Length}");
}
【问题讨论】:
-
你对linux中的文件设置了正确的权限吗?什么斜线 / 而不是 \?
-
文件共享在 Linux 上如何可见?您在 Linux 中使用什么来使文件共享对系统可用?
-
看起来共享是使用稍微不同的路径安装的,但我需要这段代码在 windows 和 linux 下都可以工作
标签: c# linux file .net-core .net-core-2.2