使用 C# 访问网络文件共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18240909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:38:14  来源:igfitidea点击:

Accessing a network file share with C#

c#httpfile-io.netfileshare

提问by Mike Marks

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.

我以前从未这样做过,而且我所做的所有研究都表明需要用户名/密码。情况是这样的:我正在为我的公司开发一个应用程序,该应用程序需要访问网络上的文件共享。我们称之为文件共享\\server\TPK。我的应用程序需要从此共享上的此文件夹中获取文件。在公司网络上使用文件共享与使用文件 I/O ( System.IO) 是否相同?谁能给我任何关于如何做到这一点的指导?我知道这可能是一个基本问题,对此我深表歉意。

采纳答案by McAden

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:

一般来说,是的。一样的。只需使用您所说的 UNC 路径。根据应用程序的运行方式,您可能会遇到安全问题,但快速测试应该类似于:

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.

只需将它指向一个您知道存在的文件,然后查看它是否找到它。根据应用程序的配置,您可能需要处理凭据或身份。如果是这种情况,您应该得到一个异常,说明“访问被拒绝”或类似的内容。

回答by ND72

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);
    }

}