Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a network file share with C#

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.

like image 577
Mike Marks Avatar asked Aug 14 '13 19:08

Mike Marks


People also ask

How do I access the C drive on my computer on my network?

At the computer, open Computer. Right-click the C drive and select Properties. In the Properties box, select the Security tab and verify that the Administrator's group has full privileges. To set up C drive sharing with a specific account, select Sharing and click Advanced Sharing.

How do I access network file share?

Right click on the Computer icon on the desktop. From the drop down list, choose Map Network Drive. Pick a drive letter that you want to use to access the shared folder and then type in the UNC path to the folder. UNC path is just a special format for pointing to a folder on another computer.

What does C$ mean in Windows?

It means it is an "admin" share or "hidden" share.


2 Answers

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.

like image 93
McAden Avatar answered Oct 19 '22 10:10

McAden


It's not that obviously possible.

I had to do something like this:

public class SharedLocationConnector : IDisposable
{
    char driveLetter;
    bool disposed = false;

    public char ConnectToLocation(string path, string userName, string pwd)
    {
        driveLetter = MapShare(path, userName, pwd);
        Thread.Sleep(2000); //It takes that much for connection to happen
        return driveLetter;
    }

    private char MapShare(string path, string username, string pwd)
    {
        char driveLetter = GetAvailableDriveLetter();
        string cmdString = "net use " + driveLetter + ": " + path + ((username != string.Empty) ? " /user:" + username + " " + pwd : "");
        ManagementClass processClass = new ManagementClass("Win32_Process");
        object[] methodArgs = { cmdString, null, null, 0 };
        object result = processClass.InvokeMethod("Create", methodArgs);
        return driveLetter;
    }

    public void  Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (!disposed)
        {
            //Dispose managed objects. Thre are none.

            //Dispose unmanaged objects
            if (!String.IsNullOrWhiteSpace(driveLetter.ToString()))
                FileUtils.DisconnectSambaShare(driveLetter);
            disposed = true;
        }
    }

    ~SharedLocationConnector()
    {
        Dispose(false);
    }

    public void Disconnect()
    {
        if (!String.IsNullOrWhiteSpace(driveLetter.ToString()))
            DisconnectShare(driveLetter);
    }

    private void DisconnectShare(char driveLetter)
    {
        string cmdString = "net use " + driveLetter + ": /DELETE";
        ManagementClass processClass = new ManagementClass("Win32_Process");
        object[] methodArgs = { cmdString, null, null, 0 };
        object result = processClass.InvokeMethod("Create", methodArgs);
    }

}
like image 1
ND72 Avatar answered Oct 19 '22 11:10

ND72