467,084 Members | 1,223 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,084 developers. It's quick & easy.

File Exist

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

*** Sent via Developersdex http://www.developersdex.com ***
Apr 6 '07 #1
  • viewed: 4390
Share:
5 Replies
if your code does not have read access, then exists will return false

-- bruce (sqlwork.com)

Eugene Anthony wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Apr 6 '07 #2
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" <no****@nospam.comwrote in message
news:ui**************@TK2MSFTNGP03.phx.gbl...
if your code does not have read access, then exists will return false

-- bruce (sqlwork.com)

Eugene Anthony wrote:
>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

*** Sent via Developersdex http://www.developersdex.com ***

Apr 6 '07 #3
I'd be inclined to break up my statement so that evaluation of the parameter
isn't done inside the method call, e.g.

string path = ds1.Tables[0].Rows[0].ItemArray.GetValue(0).ToString();
// you can put a breakpoint on the above line to satisfy that the path is
legit.
if (File.Exists( path) {
......
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Eugene Anthony" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Apr 6 '07 #4
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" <so***********@yahoo.comwrote in message
news:uL**************@TK2MSFTNGP05.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Apr 6 '07 #5
I would hazard a guess that if your path does in fact contain
"images/5/Video1/qbert.flv" that its not a complete and understandable file
path. A file path would probably be something c:\images\5\Video1\qbert.flv

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"Eugene Anthony" <so***********@yahoo.comwrote in message
news:uL**************@TK2MSFTNGP05.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Apr 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by batels@gmail.com | last post: by
7 posts views Thread by andylcx@hotmail.com | last post: by
3 posts views Thread by Dave | last post: by
52 posts views Thread by paytam@gmail.com | last post: by
2 posts views Thread by tino | last post: by
2 posts views Thread by Jeff | last post: by
8 posts views Thread by Anthony Papillion | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.