.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值