473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
5 4615
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lin Ma | last post by:
Greetings, Is it possbile to check a file exist without using Server.CreateObject("Scripting.FileSystemObject") in asp page?? The reason is our hosting company turn that function off for...
23
by: batels | last post by:
Hey All, I'm a bit new at this,and i tryed seraching google about it but i didn't quite got a solution. I'm writing an HTML page that needs to check if another html file exist (in the same...
7
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
3
by: Dave | last post by:
How can i check to see if a link (file) exist while a web page is loading. If the page doesn't exist - i want the hyperlink to go an error page telling the user the link doesn't exist otherwise...
52
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
2
by: tino | last post by:
I use File.Exist to check whether a certain image exists. Upon successfull check I want to use the file further but I always get a "The process cannot access the file because it is being used by...
2
by: Jeff | last post by:
Hey ASP.NET 2.0 Below is the code I have trouble with. I've placed some pictures in the ~/Network/Images/Fullsize/ folder in my project. The problem is that File Exist always return false (it...
1
by: siwoodworker | last post by:
Hey all- I am trying to move files when they become avaiable, but am having no luck. Everything I have read talks about using IO.File.Exist(myfile), but this still does not work. I thought about...
8
by: Anthony Papillion | last post by:
Hello Everyone, I'm writing some code that needs to check if a file exists or not. I'm using the System.IO's File.Exist() method and it's either misreporting or I am not fully understanding how...
1
by: Adya | last post by:
Hi all, I have a list of files starting with a 4 digit code as below, 1) MI01_xxxx_xxxxx.pdf 2) MI01_xxx.pdf 3) MI01_xxxxx.pdf 4) MI03_xxxxx_xxxxx_xxxx.pdf 5) MI04_xxxxxxx_xxxx.pdf 6)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.