使用SMB协议创建文件、获取文件和删除文件



创建文件,创建文件夹,获取文件,删除文件,列出文件夹和重命名文件。这些都是我需要使用SMB协议实现的方法。

我已经用FTP协议实现了相同的功能。例如:

private void FtpCreateFolder(string ftpAddress, string ftpUName, string ftpPWord)
    {
            WebRequest ftpRequest = WebRequest.Create("ftp://" + ftpAddress + "/AUTO_TEST_FOLDER");
            ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
            ftpRequest.Credentials = new NetworkCredential(ftpUName, ftpPWord);
    }

我应该使用什么库来实现与SMB协议相同的方法?

我张贴解决方案。你可以看到更多的细节

首先创建网络连接:

    [StructLayout(LayoutKind.Sequential)]
    public class NetResource
    {
        public ResourceScope Scope;
        public ResourceType ResourceType;
        public ResourceDisplaytype DisplayType;
        public int Usage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
    }
    public class NetworkConnection : IDisposable
    {
        private string _networkName;
        private NetworkCredential _credentials;
        public NetworkConnection(string networkName, NetworkCredential credentials)
        {
            _networkName = networkName;
            _credentials = credentials;
        }
        public int Connect()
        {
            var netResource = new NetResource()
            {
                Scope = ResourceScope.GlobalNetwork,
                ResourceType = ResourceType.Disk,
                DisplayType = ResourceDisplaytype.Share,
                RemoteName = _networkName
            };
            var userName = string.IsNullOrEmpty(_credentials.Domain)
                ? _credentials.UserName
                : string.Format(@"{0}{1}", _credentials.Domain, _credentials.UserName);
            var result = WNetAddConnection2(
                netResource,
                _credentials.Password,
                userName,
                0);
            return result;
        }
        ~NetworkConnection()
        {
            Dispose(false);
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            WNetCancelConnection2(_networkName, 0, true);
        }
        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2(NetResource netResource,
            string password, string username, int flags);
        [DllImport("mpr.dll")]
        private static extern int WNetCancelConnection2(string name, int flags,
            bool force);
    }
}
public enum ResourceScope : int
{
    Connected = 1,
    GlobalNetwork,
    Remembered,
    Recent,
    Context
};
public enum ResourceType : int
{
    Any = 0,
    Disk = 1,
    Print = 2,
    Reserved = 8,
};
public enum ResourceDisplaytype : int
{
    Generic = 0x0,
    Domain = 0x01,
    Server = 0x02,
    Share = 0x03,
    File = 0x04,
    Group = 0x05,
    Network = 0x06,
    Root = 0x07,
    Shareadmin = 0x08,
    Directory = 0x09,
    Tree = 0x0a,
    Ndscontainer = 0x0b
};

如何使用网络连接

 public class SmbFileContainer
    {
        private readonly NetworkCredential networkCredential;
        // Path to shared folder:
        private readonly string networkPath;
        public SmbFileContainer()
        {
            this.networkPath = @"\MY_PCSharedFolder";
            var userName = "Bob";
            var password = "123";
            var domain = "";
            networkCredential = new NetworkCredential(userName, password, domain);
        }
        public bool IsValidConnection()
        {
            using (var network = new NetworkConnection($"{networkPath}", networkCredential))
            {
                var result = network.Connect();
                return result == 0;
            }
        }
        public void CreateFile(string targetFile, string content)
        {
            using (var network = new NetworkConnection(networkPath, networkCredential))
            {
                network.Connect();
                var file = Path.Combine(this.networkPath, targetFile);
                if (!File.Exists(file))
                {
                    using (File.Create(file)) { };
                    using (StreamWriter sw = File.CreateText(file))
                    {
                        sw.WriteLine(content);
                    }
                }
            }
        }
    }
测试:

static void Main(string[] args)
        {
            var smb = new SmbFileContainer();
            if (smb.IsValidConnection())
            {
                smb.CreateFile("testFile.txt", "Hello World");
            }
        }

最新更新