Accessing a network file share with C#

Mike Marks picture Mike Marks · Aug 14, 2013 · Viewed 51.6k times · Source

I've never done this before, and all of the research I've done indicates needing user names/passwords. Here's the situation: I am developing an app for my company, and the app needs to access a file share on the network. Let's call that file share \\server\TPK. My app needs to get files from this folder on this share. Is working with file shares on a company network the same as working with File I/O (System.IO)? Can anyone give me any guidance on how to do this? I know this probably is an elementary question, and I apologize for that.

Answer

McAden picture McAden · Aug 14, 2013

Generally speaking, yes. It's the same. Just use the UNC path as you've stated. You may have security issues depending on how your application is running but a quick test should be something like:

FileInfo myFile = new FileInfo(@"\\server\TPK\some-file-that-exists.pdf");
bool exists = myFile.Exists;

Just point it to a file that you know exists and see if it finds it. You may have to deal with Credentials or Identity depending on the configuration of your application. If this is the case you should get an Exception stating "Access Denied" or something along those lines.