any ideas why this does not work?

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi for some reason in the code below even when the file exits the
code below never executes int a = 1;
does the path have to contain the entire physical path including the drive?

string validpath = "~/Files/newfile.doc"
if (File.Exists(validpath))
{
int a = 1;
}
Thanks Paul.
 
thanks for the response, yep no file as you stated. Do you know if there is
a way in the code to check if a path is just a path or if it is a file with
out causing a runtime error? I have code that only needs to delete a file if
the string is a file and if it is just a path skip any deletion.
Paul.
 
Thanks for the additional information. I tried
If (File.Exists(Server.MapPath("~/Files/testfile.doc")))
where Files is a folder in my project and this works fine for what is above.

When I try it with a path that does exist to a folder on a different
machine, note without a file like
If (File.Exists(Server.MapPath("\\\\ExternalMachineName\\Folder1\\Folder2")))
I get the run time error
Failed to map the path "ExternalMachineName/Folder1/Folder2"

Any ideas, thanks ?
 
Seems to work if I remove the Server.MapPath, since a path on an external
machine does not depend on the project location.
 
Dude, you do not know what Server.MapPath does.

All t does it converts virtual folder to real folder. Virtual folder is the
one IIS knows about. Real one is the one you see in File Explorer.
File.Exist works with real folder names not virtual.
So "~/MyFolder/MyFile" is a virtual folder (cause you have '~') so in order
to check if file exists you need to use Server.MapPath to convert to real
folder name.
"C:\myfolder\aaa.txt" is a real name. You do not need Server.MapPath.

PS: "\\\\ExternalMachineName\\Folder1\\Folder2" is a real name. So do not
need MapPath.


George.
 
re:
!> I get the run time error
!> Failed to map the path "ExternalMachineName/Folder1/Folder2"
!> Any ideas, thanks ?

The account ASP.NET is running as,
needs access permissions to any shared folder in a different computer.

If the application resides on a UNC share, ASP.NET always impersonates
the IIS UNC token to access that share unless a configured account is used.

If you provide an explicitly configured account, ASP.NET uses that account in preference to the IIS UNC token.

To access a folder in an external machine,
you'll need to run ASP.NET as a network account with access permissions to that folder and its files.

Do that by configuring ASP.NET's identity in web.config :

<identity impersonate="true"
userName="domain\user"
password="password" />

Of course, you'll need to create a Windows account for "domain\user" on your server, and run:

aspnet_regiis -GA domain\user

....on the web server, so that the domain account has the necessary
permissions to all the directories on the server which ASP.NET needs access to.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
Hi thanks for the additional information. I think the app is running on a
UNC share so most likely impersonating the IIS UNC token.
 
Back
Top