views:

453

answers:

5

Hello,

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

+1  A: 

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?

Michael Barth
It is the same name. That is just the mistake in my post. I have now corrected.
robUK
+1  A: 

Windows Mobile, just like Linux, doesn't use the concept of a current directory. So what you need to do is something like: http://stackoverflow.com/questions/284049/how-do-you-get-the-current-directory-in-compact-framework

HackerBaloo
+1  A: 

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

Update: Above might not work - try as one other poster says, http://stackoverflow.com/questions/284049/how-do-you-get-the-current-directory-in-compact-framework.

samjudson
+4  A: 

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");
Fiur
A: 
 string logIOFilePath = 
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), 
                "Log4Net.config");
robUK