I am using the FileInfo class. However, the file info cannot find the file.
我正在使用FileInfo类。但是,文件信息找不到该文件。
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'
该文件名为log4Net.config,我已将其添加到我的项目中。我已将属性设置为构建action ='Content'并复制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'.
存在总是错误的。例外是:找不到文件'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.
我已经检查了我的PDA并且Log4Net.config已被复制到PDA并且与可执行文件位于同一目录中。所以不确定为什么它找不到它。
Just some extra info the configure method expects a FileInfo as a parameter.
只需要一些额外的信息,configure方法需要FileInfo作为参数。
Am I doing something wrong.
难道我做错了什么。
Many thanks for any advice,
非常感谢任何建议,
Steve
6 个解决方案
#1
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指向OS的根,而不是exe的根。所以你应该改变这样的代码:
FileInfo logfileInfo = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Log4Net.config");
#2
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?
顺便说一句,你的帖子中有一个矛盾:首先你说这个文件名为“logPDA.config”,然后它突然被称为“Log4Net.config”。只是为了确保,文件总是命名相同?
#3
I can only suggest that you double check the value of Directory.GetCurrentDirectory()
.
我只能建议您仔细检查Directory.GetCurrentDirectory()的值。
Update: Above might not work - try as one other poster says, How do you get the current directory in compact framework?.
更新:上面可能不起作用 - 尝试另一张海报说,你如何获得紧凑框架中的当前目录?
#4
Windows Mobile, just like Linux, doesn't use the concept of a current directory. So what you need to do is something like: How do you get the current directory in compact framework?
Windows Mobile与Linux一样,不使用当前目录的概念。所以你需要做的是:如何获得紧凑框架中的当前目录?
#5
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()
您假设程序文件夹是当前目录。 Afaik并非如此。您可以从System.IO.Directory.GetCurrentDirectory()开始进行调查
#6
string logIOFilePath =
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase),
"Log4Net.config");
#1
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指向OS的根,而不是exe的根。所以你应该改变这样的代码:
FileInfo logfileInfo = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Log4Net.config");
#2
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?
顺便说一句,你的帖子中有一个矛盾:首先你说这个文件名为“logPDA.config”,然后它突然被称为“Log4Net.config”。只是为了确保,文件总是命名相同?
#3
I can only suggest that you double check the value of Directory.GetCurrentDirectory()
.
我只能建议您仔细检查Directory.GetCurrentDirectory()的值。
Update: Above might not work - try as one other poster says, How do you get the current directory in compact framework?.
更新:上面可能不起作用 - 尝试另一张海报说,你如何获得紧凑框架中的当前目录?
#4
Windows Mobile, just like Linux, doesn't use the concept of a current directory. So what you need to do is something like: How do you get the current directory in compact framework?
Windows Mobile与Linux一样,不使用当前目录的概念。所以你需要做的是:如何获得紧凑框架中的当前目录?
#5
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()
您假设程序文件夹是当前目录。 Afaik并非如此。您可以从System.IO.Directory.GetCurrentDirectory()开始进行调查
#6
string logIOFilePath =
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase),
"Log4Net.config");