FileInfo紧凑框架3.5 - 找不到文件

发布于 2024-07-24 07:48:29 字数 634 浏览 6 评论 0原文

我正在使用 FileInfo 类。 但是,文件信息找不到该文件。

该文件名为 log4Net.config,我已将其添加到我的项目中。 我已将属性设置为构建操作 = '内容' 和复制输出 = '始终复制'

当我运行以下代码时:

 FileInfo logfileInfo = new FileInfo("Log4Net.config");

 if (!logfileInfo.Exists)
 {
     Console.WriteLine("Cannot find file: " + logfileInfo.FullName);
     return; 
 }
 else
 {
     XmlConfigurator.Configure(logfileInfo);
 }

Exists 始终为 false。 例外情况是:找不到文件“Log4Net.config”。

我检查了我的 PDA,Log4Net.config 已复制到 PDA 并与可执行文件位于同一目录中。 所以不确定为什么找不到它。

只是一些额外的信息,configure 方法需要 FileInfo 作为参数。

难道我做错了什么。

非常感谢您的建议,

史蒂夫

I am using the FileInfo class. However, the file info cannot find the file.

The file is called log4Net.config and I have added it to my project. I have set the properties to build action = 'Content' and copy output = 'copy always'

When I run the following code:

 FileInfo logfileInfo = new FileInfo("Log4Net.config");

 if (!logfileInfo.Exists)
 {
     Console.WriteLine("Cannot find file: " + logfileInfo.FullName);
     return; 
 }
 else
 {
     XmlConfigurator.Configure(logfileInfo);
 }

Exists is always false. The exception is: Could not find file 'Log4Net.config'.

I have checked on my PDA and the Log4Net.config has been copied the PDA and is in the same directory as the executable. So not sure why it cannot find it.

Just some extra info the configure method expects a FileInfo as a parameter.

Am I doing something wrong.

Many thanks for any advice,

Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

关于从前 2024-07-31 07:48:30

您必须指向项目的根目录。 FileInfo 指向操作系统的根目录而不是 exe 的根目录。 所以你应该像这样更改代码:

FileInfo logfileInfo = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Log4Net.config");

You have to point to the root of your project. FileInfo points to the root of the OS not the root of the exe. So you should change the code like this :

FileInfo logfileInfo = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Log4Net.config");
简美 2024-07-31 07:48:30

当程序没有访问文件的安全权限时,也会引发文件未找到异常,那么也许就是这种情况?

顺便说一句,您的帖子中有一个矛盾:首先您说该文件名为“logPDA.config”,然后它突然被称为“Log4Net.config”。 只是为了确保该文件的名称始终相同吗?

A file not found exception is also thrown when the program does not have the security permission to access the file, so perhaps that's the case?

btw, there's a contradiction in your Post: First you say the file is called "logPDA.config", then it's suddenly called "Log4Net.config". Just to make sure, is the file always named the same?

幽蝶幻影 2024-07-31 07:48:30

您假设程序文件夹是当前目录。 事实并非如此。 您可以从 System.IO.Directory.GetCurrentDirectory() 开始调查

You are assuming that the Program Folder is the current directory. Afaik that is not the case. You can investigate starting with System.IO.Directory.GetCurrentDirectory()

岁月染过的梦 2024-07-31 07:48:30
 string logIOFilePath = 
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), 
                "Log4Net.config");
 string logIOFilePath = 
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), 
                "Log4Net.config");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文