-1

I am trying to access a network disk, with C#, I've tried doing this:

if (!Directory.Exists(path)) //With \\Server\path

OR

if (!(new FileInfo(path).Exist)) //With \\Server\path

Both tell me it does not exist.

I must say, I am running an ASP.NET site and I am trying to reach it from there, I don't know if that matters?

The thing I am trying to do is my DLL needs a path to the root directory of it's contents so I need to specify a path, the path is located in disk S but disk S is not a logical disk, it's a network disk.

We start the IIS website as an administrator of the whole domain to prevent not having privileges.

runefist
  • 543
  • 1
  • 8
  • 19
  • 1
    Have you tried @"\\server\path\..."? – tym32167 Jul 11 '16 at 13:44
  • 1
    It's probably a permission issue. Your ASP app will be running as a specific local user that won't have access. – DavidG Jul 11 '16 at 13:44
  • 4
    It's also a bad idea to give your IIS user network permissions. Basically, don't do this. This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) (*DLL needs a path to the root directory of it's contents*) – Liam Jul 11 '16 at 13:46
  • Possible duplicate of [Access to the path is denied](http://stackoverflow.com/questions/4877741/access-to-the-path-is-denied) – Liam Jul 11 '16 at 13:48
  • I had this problem once and the easiest way to solve it was to map the network path in Windows to a unit like this http://www.howtogeek.com/school/windows-network-sharing/lesson8/all/ – derloopkat Jul 11 '16 at 13:54
  • @derloopkat This is very difficult and complicated to do for an ASP.Net site, remember that mapped drives are specific to the user, not the machine... I wouldn't recommend doing it this way. – DavidG Jul 11 '16 at 13:58
  • The final version of my application mapped and unmaped a unit programatically in C#. Can be done in few lines. But this is not what I meant in my previous message. I suggested to map the unit manually. Easiest way to access a file is creating admin account with network rights on a shared folder, map the unit to that folder and then run your IIS application pool using this account. That can be done in 15 minutes. After that point you'll access the file in C# using a path like e.g. "M:\Archive\profiles.xml" – derloopkat Jul 11 '16 at 14:50

3 Answers3

0
Server\\path\path\path

doesn't look like a correct UNC pathname to me.

\\Server\path\path\path

should be correct.

Secondly, the user account under which your IIS process runs needs to have appropriate permissions to the network location in question. The identity used is usually determined by the application pool it's in, or if your app uses impersonation it could be identifying as the client user. Either way you should be careful to only grant the permissions absolutely definitely required by the app, and no more.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • N.B. the UNC info was based on the info the original question, which has now been edited. I suggest the permissions may still be an issue. Just because your IIS is an administrator of the server (which is a VERY BAD idea btw) doens't mean it can access a network location. – ADyson Jul 11 '16 at 13:54
  • Ye I've eddited my question, to provide more info for you guys, but I am using network administrator so I have no clue why I should not have permissions to it? – runefist Jul 11 '16 at 13:59
  • You've edited saying it tells you the path doesn't exist. Have you checked that the path is valid outside the application? e.g. log on to server as the user under which the app runs and check that the path (as used by the application) definitely exists? – ADyson Jul 11 '16 at 14:23
0

After setting for both the pool and the website the administrator as account who starts the process, AND adding '@' and the '\' we've managed to access the network folder (Thanks everyone!), current status: DLL says it can't find the path to it...

So ye the code we have written right now has access but the DLL says it cannot find the path, so we contacted the owner who made the DLL.

runefist
  • 543
  • 1
  • 8
  • 19
-3

For Check file exist you have to use this:

if(!File.Exists("path"))

For check Folder exist you have to use this:

if (!Directory.Exists("Path"))
Amirhossein Yari
  • 2,054
  • 3
  • 26
  • 38
  • 1
    Cool story bro... ;) He's not asking this. The issue is IIS does not be default have network permissions – Liam Jul 11 '16 at 13:52