本文介绍了在 File.ReadAllText 中找不到路径的一部分,但路径存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个引用库的网络服务.这个库项目 (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 中找不到路径的一部分,但路径存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:57