Question

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

Was it helpful?

Solution

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");

OTHER TIPS

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?

I can only suggest that you double check the value of Directory.GetCurrentDirectory().

Update: Above might not work - try as one other poster says, How do you get the current directory in compact framework?.

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?

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()

 string logIOFilePath = 
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), 
                "Log4Net.config");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top