Link to home
Start Free TrialLog in
Avatar of barnetjeb
barnetjeb

asked on

File.Exists always returns false

I have a web app on a server.  I want to get a file from another machine.  I'm trying to do something like...

If file.exists("\\machine2\import\myfile.txt") then
   response.write("yes")
Else
   response.write("no")
End if

It always returns no.  Am I able to do this?

Thanks
Avatar of nomoreself
nomoreself

What language are you trying to write this in?  Javascript?  PHP?  C#?
Can you access the file if you use the same string in the explorer
It may be a permissions issue, if you are running this from asp make sure the impersonated account has permissions. Most likely it will be the local ASP service account (which can't have permissions to network resources) so you would either have to change the account or use another authentication scheme (good for Intranet apps)
erm.. try to change the code into these
>>
If file.exists("\\\\machine2\\import\\myfile.txt") then
   response.write("yes")
Else
   response.write("no")
End if

probably the problem is with the backslash in the code.

hope it help

That's only for Server Side JScript. The If syntax and improper casing suggests VBScript
The file has to be accessible from the WEB SERVER, not your machine. Chances are it's not.

And even then, it's difficult to make a network connection work. The IUSR_machine account rarely has network access.
Hi barnetjeb,

Maybe this will help:

   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FileExists("\\machine2\import\myfile.txt")) then
       response.write("yes")
   Else
       response.write("no")
   End if
   Set fso = Nothing

If that doesn't work, try this:

   Dim fso, theFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   theFile = Server.MapPath("\\machine2\import\myfile.txt")
   If (fso.FileExists(theFile)) then
       response.write("yes")
   Else
       response.write("no")
   End if
   Set fso = Nothing
I doubt server.mapPath will work.

It will return something like this....
C:\inetpub\\\machine2\import\myfile.txt

Not a valid path.
Avatar of barnetjeb

ASKER

OK here is what I tried...

Server.MapPath only works when you are using a relative path.  You cannot do \\machinename, and you cannot map a drive and do something like g:\.

When I run the application from the computer, it works fine.  ie. If I'm on my development machine and I run it sees the path and returns yes.  If I go to another machine and bring up the application on my machine it doesn't work..it returns no.

I can see the path fine in Windows Explorer.

The directory currently has full network access for everyone.

My web.config file has <identity impersonate="true" />.  Is there somewhere else I need to change?

Here is what I'm wondering.  Is this actually running as a computer user as opposed to a domain user (ie. ASPNET, and since that computer is giving network access to everyone (which I presume means everyone in the domain) it doesn't recognize it?  I'm sure that is what it is, something to do which permissions, but I don't know where or what to change to make this work.

Thanks for all the suggestions.  I look forward to your replies.
I'm Using VB.NET
ASKER CERTIFIED SOLUTION
Avatar of GoSu
GoSu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also note: the Code you gave us will not compile in VB.NET. You HAVE to be using VBScript in ASP Classic.
No I'm using VB.NET.  What doesn't compile about it.  It compiles fine for me.
I'm not using classic ASP.  Will the solution you posted still work for me?
barnetjeb, I aplogize.

I had made an incorrect assuption and then confused TheKenman's post (Which is ASP Classic) with your own.

Yes the steps I gave you will work if you keep your web.config as it is
regarding
<identity impersonate="true" />

Thanks GoSu,
I will give this a try and tell you how I make out within a day or so.  I thought my problem had something to do with that, but I just didn't know where to change these settings.  Thanks again.