在 File.ReadAllText 中找不到路径的一部分,但路径存在

编程入门 行业动态 更新时间:2024-10-25 06:29:15
本文介绍了在 File.ReadAllText 中找不到路径的一部分,但路径存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个引用库的网络服务.这个库项目 (C# fw v4.5) 读取文件之类的模板.在我的代码中,我有这个:

I have a webservice that reference a library. This library project (C# fw v4.5) reads files like templates. In my code i have this:

File.ReadAllText(applicationDir + @"EmailTemplates\Contact.cshtml")

我得到这样的 applicationDir:

string applicationDir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath) + "\\";

这会引发 找不到路径的一部分 异常,但该路径存在于我的光盘上.为什么会发生这种情况?我已经检查了路径、权限,但我不知道为什么会发生这种情况.唯一可能是在路径中有一个空间.这是路径:C:\Work\Main\Source\Mailing System\Source\MailBLL\EmailTemplates\Contact.cshtml 并且在代码(调试)中我是这样看的:

This is throwing the Could not find a part of the path exception but the path exists on my disc. Why this is happening? i have checked the path, the permissions and i dont have any idea why is this happening. The only thing that may be is that in the path there is a space. This is the path: C:\Work\Main\Source\Mailing System\Source\MailBLL\EmailTemplates\Contact.cshtml and in the code (debuging) i see it like this:

C:\\Work\\Main\\Source\\Mailing%20System\\Source\\MailBLL\\EmailTemplates\\Contact.cshtml

编辑/第一个解决方案:

我刚刚发现问题出在路径上的%20!我在 applicationDir 中添加了一个替换,并且完美运行!但我不认为这是一个永久的解决方案,有没有办法让代码不放置 %20 而是放置空格?

I just figured out that the problem is the %20 on the path! i have added a replace in the applicationDir and works perfect! but i dont found this as a permanent solution, is there a way that the code does not put that %20 and instead put the space?

推荐答案

可能是文件夹名称Mailing System"之间的空格导致了问题.

probably the space between the folder name "Mailing System" is causing the issue.

这篇关于在 File.ReadAllText 中找不到路径的一部分,但路径存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 06:10:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   中找   File   ReadAllText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!