any ideas why this does not work?

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.
 
P

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.
 
P

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 ?
 
P

Paul

Seems to work if I remove the Server.MapPath, since a path on an external
machine does not depend on the project location.
 
G

George

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.
 
J

Juan T. Llibre

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.
 
P

Paul

Hi thanks for the additional information. I think the app is running on a
UNC share so most likely impersonating the IIS UNC token.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top