简体   繁体   English

使用Linux上运行的.Net 2.2内核读取文件共享上的文件内容

[英]Read file content on a file share using .Net 2.2 core running on Linux

Code below works under windows but get an exception under Linux where the user home drive seems to be prepended to the LocalPath obtained when trying to open the file. 下面的代码在Windows下工作,但在Linux下会出现异常,在该情况下,用户主驱动器似乎是试图打开文件时获得的LocalPath的前缀。 Same problem if I use a FileStream. 如果使用FileStream,也会出现同样的问题。

testing under RHEL 7.6 在RHEL 7.6下进行测试

Does anyone know of a workaround or root cause? 有人知道解决方法或根本原因吗?

Had to make the actual file share anonymous in snippets below so please ignore any typo in the path, the fact it works on windows is important bit. 必须在下面的代码片段中使实际文件共享匿名,因此请忽略路径中的任何错字,它在Windows上有效的事实很重要。

Output is: 输出为:

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

Exception is: 例外情况是:

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)

My code: 我的代码:

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}");
    }

The problem is the way that you build the path 问题是您构建路径的方式

You have to use Path.Combine 您必须使用Path.Combine

static void Main(string[] args)
    {
        //var path = "file://lnasvr001.partners/dfs/Analytics/suite/demo/externaldata/DemoDataset/temp/diagnose.json";
        var path = Path.Combine("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}");
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM