Getting File Path

  • Thread starter Thread starter Hugh McLaughlin
  • Start date Start date
H

Hugh McLaughlin

Hello Everyone and thanks for your help in advance. I am
working on an application that uses as helper class to
determine if an image file exists. If the file exists,
the path to the image is returned. Otherwise, a "Picture
not found" path is returned. I have that part workin
well. However, I have developed this on a local machine
and want to push the class to my prosuction server.
However, the paths change from machine to machine. I
want to be able to find the fully qualified path to a
particular file or directory. I tried File.GetFullPath
(myFilename), but that returns C:\WINDOWS\system32
\myFilename which is not where the file resides. I want
to make this as dynamic as possible. Any help would be
greatly appreciated. Thanks.
 
If you have a specific number of files, I'd seriously
consider adding them as embedded resources. To do this,
go to VS.nET and Add Existing ITem and point to the
file. This will add it in comiled form to your project.
Just make sure you set it's build property to Embedded
Resource. If you do this, it will always exist.

I'm not sure about the other part though. Why would it
exist on some machines and not others? Will it/SHould it
always exist locally or are you going to reference a
network share for instance. You can't hit a specific
device name with either FileInfo or Path classes, but I
think the path class may get what you want ..although if
you could show me a in depth example I'm sure we can
figure it out.

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpref/html/frlrfsystemiopathclasstopic.asp

Let me know.

Bill
 
The reason the paths will be different from machine to
machine is test machine (laptop) versus production
webserver. When I use the File.GetFullPath
(myFilename), it returns C:\WINDOWS\system32
\myFilename which is not where the file physically
exists. Why is this?
 
Hi Hugh,

You said:
|| I tried File.GetFullPath (myFilename), but that returns
|| "C:\WINDOWS\system32\myFilename" which is not where the file
resides

There isn't a File.GetFullPath(). But there is Path.GetFullPath() which
creates a path using the current directory. Which is not where the file
resides!

You need to obtain the FileInfo for "myFilename" and use
oFileInfo.FullPath().

Obtaining the FileInfo is left as an exercise.

Regards,
Fergus
 
Hello Everyone and thanks for your help in advance. I am
working on an application that uses as helper class to
determine if an image file exists. If the file exists,
the path to the image is returned. Otherwise, a "Picture
not found" path is returned.

How about this:

public string GetImagePath(string aImageFileName)
{
if(File.Exists(aImageFileName)
{
return Path.GetDirectoryName(aImageFileName)
}
else
{
return "Picture not found"
}
}

The "Path" class is in the System.IO namespace.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
If the file is supposed to be in the same directory as the application, then
you could do something like this.

string path = Assembly.GetExecutingAssembly().Location;
string fullName = Path.Combine(path,imageFile);
if ( File.Exists(fullName) )
{
}
 
Back
Top