File Exist

E

Eugene Anthony

ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString() contains the
string path: images/5/Video1/qbert.flv

if (File.Exists(ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString()))
{
//code to execute
}

Despite the path exist along with the file name, the code never gets
executed.


How do I solve the problem?


Eugene Anthony
 
B

bruce barker

if your code does not have read access, then exists will return false

-- bruce (sqlwork.com)
 
D

David

You might want to ensure the path actually exists... images directory is
from the location the code is executing, so if you are in a subfolder but
your path is expected from root, then your path will not exist.

--
Best regards,
Dave Colliver.
http://www.DerbyFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

bruce barker said:
if your code does not have read access, then exists will return false

-- bruce (sqlwork.com)

Eugene said:
ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString() contains the
string path: images/5/Video1/qbert.flv

if (File.Exists(ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString()))
{
//code to execute
}

Despite the path exist along with the file name, the code never gets
executed.


How do I solve the problem?


Eugene Anthony
 
K

Kevin Spencer

The System.IO.File class, and other System.IO classes are designed to work
with file and folder paths. What you have stored in your database is a
virtual path. In fact, it is a relative virtual path.

To explain a bit:

There are several different kinds of paths for different things. Among these
are file and folder paths, UNC paths (network paths), and virtual paths (web
paths). Here are some examples of each:

File Path examples:

foo.txt (relative file path to file in same folder as executing assembly)
C:\somefolder\foo.txt (absolute file path to file)
..\parentfolder\foo.txt (relative path to file in parent folder of executing
assembly)

Virtual Path examples:

foo.txt (virtual path to file in same virtual directory as web application)
/somefolder/foo.txt (root-relative virtual path to file)
.../siblingfolder/foo.txt (relative virtual path to sibling virtual folder
location of file)

UNC Path example:
\\machine\C\foo.txt (UNC path to file in share C in machine "machine"

Now, you're working in an ASP.Net application, which means that you're
workiing in the context of a web application. The web application has no
knowledge of the file system on the machine, only the virtual directories of
the application. To get at the file system location on the machine, you have
to get the path to it from the web server, or HttpServerUtility class. The
HttpServerUtility class has a method for doing this translation:
MapPath(virtual path). The HttpServerUtility is available as the Server
property of a Page.

However, you're storing relative paths in your database, which means that
the path is relative to the location of the virtual folder containing the
executing page in the web application. Of course, that will be different in
each folder of your web application. So, first you have to determine what
the beginning of the path is relative TO, and then pass a meaningful path
which is relative to the currently-executing page to the Server.MapPath()
method.

As an example, let's suppose that these are all relative to the root folder
of your web application. For example, your web application is at
http://localhost. The path "images/5/Video1/qbert.flv" - relative to the
root folder of your app would be http://localhost/images/5/Video1/qbert.flv.
Now, let's pretend that the executing page is in the folder
http://localhost/example. The path "images/5/Video1/qbert.flv" - relative to
THAT folder would translate to
http://localhost/example/images/5/Video1/qbert.flv. You don't want that. So,
you need to determine the root-relative path first.

I hope you're still with me, because it gets a bit dodgy from here. Your web
application may not be in the root folder of a web site. It may, for
example, be in http://localhost/web1. Now, using a root-relative path means
that the application root folder is going to be C:\inetpub\wwwroot\web1, not
C:\inetpub\wwwroot. If the image is in C:\inetpub\wwwroot\web1\images, the
root-relative path to it will be \web1\images, rather than \images. But you
may be developing on a local web server with 1 web site and many sub-web
applications, for deployment to a root web site. How do you keep things
synchronized without changing code?

Well, here's where it pays off if your stored paths are relative to the
application root. You can get the absolute path to the web application root
fairly easily, using the Request object. The Request.ApplicationPath
property yields the root-relative web application path. In the example of
http://localhost/web1, this would yield "/web1". You can now use this to
build a root-relative path to the file. Of course, it looks like your stored
path doesn't begin with a slash, so you will need to append it in, as in:

string rootRelative = Request.ApplicationPath + "/" +
ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString();

Now, you pass that to the Server.MapPath method to get the absolute path to
the file:

string filePath = Server.MapPath(rootRelative)

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Eugene Anthony said:
ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString() contains the
string path: images/5/Video1/qbert.flv

if (File.Exists(ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString()))
{
//code to execute
}

Despite the path exist along with the file name, the code never gets
executed.


How do I solve the problem?


Eugene Anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top