SmbFileContainer.cs
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore
{
/// <summary>
/// SMB共享文件夹
/// </summary>
public class SmbFileContainer
{
private readonly NetworkCredential networkCredential;
// Path to shared folder:
private readonly string networkPath;
/// <summary>
/// SMB共享文件夹
/// </summary>
/// <param name="connectionInfo">连接信息</param>
public SmbFileContainer(SmbFileConnectionInfo connectionInfo)
{
networkPath = connectionInfo.NetworkPath;
networkCredential = new NetworkCredential(connectionInfo.UserName, connectionInfo.Password, connectionInfo.Domain);
}
/// <summary>
/// 检查是否是有效连接
/// </summary>
/// <returns></returns>
public bool IsValidConnection()
{
bool cRet = false;
using (var network = new NetworkConnection($"{networkPath}", networkCredential))
{
var result = network.Connect();
cRet = result == 0;
}
if (!cRet)
{
if (Directory.Exists(networkPath))
{
return true;
}
}
return cRet;
}
/// <summary>
/// 创建文件
/// </summary>
/// <param name="targetFile"></param>
/// <param name="content"></param>
public void CreateFile(string targetFile, string content)
{
using (var network = new NetworkConnection(networkPath, networkCredential))
{
network.Connect();
var file = System.IO.Path.Combine(networkPath, targetFile);
System.IO.FileStream fs = System.IO.File.Create(file);
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs))
{
sw.Write(content);
}
}
}
}
/// <summary>
/// SMB共享文件夹连接信息
/// </summary>
public class SmbFileConnectionInfo
{
/// <summary>
/// 网络路径
/// 例:@"\\192.168.101.125\Monitor\ScanResult"
/// </summary>
public string NetworkPath { get; set; }
/// <summary>
/// 用户名
/// 例:administrator
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// 例:123
/// </summary>
public string Password { get; set; }
/// <summary>
/// 域名
/// </summary>
public string Domain { get; set; } = "";
}
[StructLayout(LayoutKind.Sequential)]
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;
}
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);
}
enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
};
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
};
}