系统相关
首页 > 系统相关> > .netcore 在centos上读取文件异常

.netcore 在centos上读取文件异常

作者:互联网

项目开发、测试、演示的时候都是在windows下,一切正常;当转移到centos上读取文件则报错

Could not find file '/app/wwwroot/xxx/xxx/xxx.txt'.

原始代码:

在windows下读取文件一切正常。

string path= $"{Environment.CurrentDirectory}/wwwroot/xxx/xxx/xxx.txt";

using (StreamReader sr = new StreamReader(path, Encoding.Default))
{}

修改代码:

var file_url = new Uri($"{Environment.CurrentDirectory}/wwwroot/xxx/xxx/xxx.txt");
using (Stream stream = WebRequest.Create(file_url).GetResponse().GetResponseStream())
{
    using (StreamReader sr = new StreamReader(stream, Encoding.Default))
    {
           while (!sr.EndOfStream)
           {
                  string str = sr.ReadLine();
                   ...
            }
     }
}

使用Uri的方式,把路径转为绝对路径,centos才认

windows下长这样,centos下就不清楚了

file:///F:/Work/new/aspnet-core/src/APIHost/wwwroot/xxx/xxx/xxx.txt

标签:wwwroot,读取,centos,netcore,xxx,file,new,txt
来源: https://blog.csdn.net/cmhdl521/article/details/122846103