5

In my code i do

    if (!File.Exists(getSomePath()))
    {
        MessageBox.Show("... existing" + " " + getSomePath());
        this.Close();
    }

I can see getSomePath() is correct but when i open the app on the network it says it doesnt exist. When i copy the folder to my local drive it says it does exist.

Whats going on?

Rob
  • 26,989
  • 16
  • 82
  • 98
  • Just a notice from my experience: in case that your code run as a Windows service, besides configuring Security & Sharing, you also need to make a LogOn account to your service (in services.msc). This LogOn account should exist in both your local computer and remote computer, and have the same password. If you have a domain user, then things would be simpler: just give this user the right to access both systems – Hoàng Long Nov 26 '15 at 14:19

5 Answers5

3

Likely SMB caching is causing File.Exists to see a delayed result.

http://technet.microsoft.com/en-us/library/ff686200(v=WS.10).aspx

jte17
  • 31
  • 1
3

If you're running it from a network share, then you will need to have the users add the compiled assembly as trusted in the .net. The way to do this is by "strong naming" your assembly, and having that strong name trusted on each user's computer.

Edit: The reason for this is for security, so a unwary user doesn't get a virus that runs from a remote (network share, etc) location. This only occurs when the user is running the app that lives in the remote location. He can neither access file shares or even his own local system from that remote app.

DMCS
  • 31,720
  • 14
  • 71
  • 104
  • but it seems to b random... i've got a service that for 90% of the files in the same directory File.exists is good, and I can browse to them in explorer from my machine.... but sometimes certain extensions fail. – FlavorScape Mar 19 '13 at 21:02
2

If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.

roman m
  • 26,012
  • 31
  • 101
  • 133
  • From my own testing, this doesn't seem to be the case. – Turnor Apr 11 '09 at 01:20
  • what "doesn't seem to be the case"? – roman m Apr 11 '09 at 01:23
  • When you run a .NET application from a network share rather than a local drive, it runs in the security context of that share, but it doesn't mean that C:\ refers to that network share. C:\ still refers to the local drive. – Turnor Apr 11 '09 at 01:26
1

You mention that it works ok locally, but not when you run it from a network share. Does your application have the appropriate security permissions to access the filesystem? By default, applications run from a network share have reduced security permissions.

Turnor
  • 1,836
  • 12
  • 14
1

File.Exists most anywhere is suspect. But when you do go to open the file, this sounds like a network permissions issue.

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794