使用window自带共享实现
创建共享文件夹
net share SharedFolder=D:\SharedFolder /GRANT:everyone,full
只读
net share SharedFolder=D:\SharedFolder /GRANT:everyone,read
net share查看共享
指定用户名和密码访问
默认情况下,局域网访问会走 Windows 登录账号。
如果你想指定用户名密码,需要在共享电脑上创建局域网用户:
net user user123 123456 /add
net localgroup "Users" user123 /add
手动映射为本地盘(系统层面)
net use Z: \\192.168.1.10\shared 123456 /user:user123
映射盘符后,读取、写入、删除共享文件夹完全像操作本地磁盘一样,不需要特殊 API。
using System;
using System.IO;
using System.Runtime.InteropServices;
public static class NetworkShareHelper
{
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(ref NETRESOURCE netResource, string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags, bool force);
[StructLayout(LayoutKind.Sequential)]
private struct NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
private static void Connect(string networkFolder, string username, string password)
{
var netResource = new NETRESOURCE
{
dwType = 1, // RESOURCETYPE_DISK
lpRemoteName = networkFolder
};
int result = WNetAddConnection2(ref netResource, password, username, 0);
if (result != 0)
throw new IOException($"无法连接共享文件夹({networkFolder}),错误码:{result}");
}
private static void Disconnect(string networkFolder)
{
WNetCancelConnection2(networkFolder, 0, true);
}
/// <summary>
/// 上传本地文件到共享目录(支持任意资源文件)
/// </summary>
public static void UploadFile(string localFilePath, string networkDestPath, string username = null, string password = null)
{
string folder = Path.GetDirectoryName(networkDestPath);
try
{
if (!string.IsNullOrEmpty(username))
Connect(folder, username, password);
// 创建目标目录(若不存在)
Directory.CreateDirectory(folder);
File.Copy(localFilePath, networkDestPath, true);
Console.WriteLine($"文件上传成功:{Path.GetFileName(localFilePath)} → {networkDestPath}");
}
finally
{
if (!string.IsNullOrEmpty(username))
Disconnect(folder);
}
}
/// <summary>
/// 下载文件到本地
/// </summary>
public static void DownloadFile(string networkPath, string localDestPath, string username = null, string password = null)
{
string folder = Path.GetDirectoryName(networkPath);
try
{
if (!string.IsNullOrEmpty(username))
Connect(folder, username, password);
if (!File.Exists(networkPath))
throw new FileNotFoundException("共享文件不存在:" + networkPath);
File.Copy(networkPath, localDestPath, true);
Console.WriteLine($"下载成功:{networkPath} → {localDestPath}");
}
finally
{
if (!string.IsNullOrEmpty(username))
Disconnect(folder);
}
}
/// <summary>
/// 删除共享文件
/// </summary>
public static void DeleteFile(string networkPath, string username = null, string password = null)
{
string folder = Path.GetDirectoryName(networkPath);
try
{
if (!string.IsNullOrEmpty(username))
Connect(folder, username, password);
if (File.Exists(networkPath))
{
File.Delete(networkPath);
Console.WriteLine("文件已删除:" + networkPath);
}
else
{
Console.WriteLine("文件不存在:" + networkPath);
}
}
finally
{
if (!string.IsNullOrEmpty(username))
Disconnect(folder);
}
}
}
使用
class Program
{
static void Main()
{
string username = "user";
string password = "123456";
// 本地文件
string localFile = @"D:\images\car.jpg";
// 共享路径(目标)
string networkFile = @"\\192.168.1.10\shared\car.jpg";
try
{
// 上传资源文件
NetworkShareHelper.UploadFile(localFile, networkFile, username, password);
// 下载回来验证
NetworkShareHelper.DownloadFile(networkFile, @"D:\downloaded\car_copy.jpg", username, password);
// 删除共享文件
// NetworkShareHelper.DeleteFile(networkFile, username, password);
}
catch (Exception ex)
{
Console.WriteLine("操作失败:" + ex.Message);
}
}
}