System.IO.File.Exists Default Directory with ASP.NET App

June 16, 2006 at 11:02 am 4 comments

Derek Pinkerton and I recently ran into this working on the latest QualTrax code!

We have a class library that reads from a custom .config file— aptly named qualtrax.config.  One of the first things we do in that class library when we are retrieving a setting is verifying the file exists.   It’s a simple call something like:

if (!System.IO.File.Exists(“qualtrax.config”))
{
   //Our Error Handling
}

Because we are not specifying a directory, it is checking for the file in the current directory.  With debugging in our test Windows application, this “current directory” is pretty intuitive.  It’s looking for the qualtrax.config file in the \bin\Debug folder.

When we started using the library in an ASP.NET application, however, the expected location of the qualtrax.config file was not as intuitive.

Right off the bat, it was not able to find the file.   So I copied it into the website root so it sat side by side with web.config.  Still the file could not be found.  I put it in the bin directory.  No luck.  App_Data directory.  No cigar.  We knew it wasn’t permission errors because when Derek hardcoded the path, it was able to read the file just fine.  We just didn’t what directory it was trying to look into by default.

I was about to embark on a pretty inefficient journey when Derek said, “I’ve been meaning to try using filemon to see where it is looking.”

So it was FileMon to the rescue.  We installed it and turned on capture right before our System.File.IO.File.Exists call and then checked the log.

353 4:12:26 PM aspnet_wp.exe:5412 QUERY INFORMATION C:\WINDOWS\system32\qualtrax.config NOT FOUND Attributes: Error

This won’t be the end of our efforts on the matter, but there are two morals of the story thus far:

1) If you are doing a File.Exists call in an ASP.NET application without specifying an explicit directory and things seem to be missing– it very well may be looking in the Windows\System32 directory! 🙂

2) FileMon proves time and time again to be a very valuable troubleshooting tool.  In the past, we’ve found it especially helpful with permission problems.  Shame on me for not thinking of it sooner.

Entry filed under: .NET, C#, Derek P, FileMon, QualTrax, System.IO.File.

Oral Traditions in Action A Great Deed… With Motive

4 Comments Add your own

  • 1. Clint  |  June 16, 2006 at 1:12 pm

    Another possible solution would be to write a file and see where it ends up, but that would require doing a search for the new file, or just using filemon anyway! 🙂

    In looking around, I am discovering that, yes, I already have FileMon.exe on my USB thumbdrive as part of the “UberUSB” toolpack. I’m prepared without even knowing it! 🙂

    Reply
  • 2. tgaw  |  June 20, 2006 at 10:16 am

    Quick addendum– In your Microsoft Visual Studio 2005 Development Environment, you do have the ability to specify what web server is being used when you interactively run (If you right click on your web site project and select Property Pages and then click on Start Options you’ll see your Server choices).

    On my machine, I was running through IIS and it is looking for the file in the C:\Windows\System32 directory.

    However, other machines may be using the ASP.NET Development Server, which does drastically change the locale of where it is searching for the file. For example, Derek is running off ASP.NET Development Server and it was looking for his qualtrax.config file in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\qualtrax.config

    Fun fun fun– like a mini treasure hunt! 🙂

    Reply
  • 3. Sajjad Akhter  |  July 17, 2006 at 5:25 pm

    Use this.Page.MapPath(“fileName”) to find out file’s path.

    Sajjad

    Reply
  • 4. Danilo  |  September 16, 2012 at 10:46 pm

    Why viewers still use to read news papers when in this technological globe the whole thing is accessible on web?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Flickr Photos

3D Printed Products

Tweets


%d bloggers like this: