C#访问网络共享文件夹的方法有以下两种:
- 使用.NET框架提供的System.IO命名空间和File类
- 使用WNetAddConnection2函数和WNetCancelConnection2函数
方法一:使用System.IO命名空间和File类
在C#中,我们可以使用System.IO命名空间中的File类来访问网络共享文件夹。具体步骤如下:
- 在代码中引入System.IO命名空间.
using System.IO;
- 使用File的静态方法来打开文件
FileStream fileStream = File.Open(@"\\servername\sharedfolder\file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
需要注意的是,在文件路径中要指定网络共享文件夹的地址,格式为\servername\sharedfolder\filename。
代码示例:
using System.IO;
namespace AccessNetworkSharedFolder
{
class Program
{
static void Main(string[] args)
{
try
{
FileStream fileStream = File.Open(@"\\servername\sharedfolder\file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader(fileStream);
Console.WriteLine(reader.ReadLine());
reader.Close();
fileStream.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception : " + ex.Message);
}
}
}
}
方法二:使用WNetAddConnection2和WNetCancelConnection2函数
另外一种访问网络共享文件夹的方法是使用WNetAddConnection2函数和WNetCancelConnection2函数。WNetAddConnection2函数可以将远程共享资源连接到本地驱动器号(例如,将远程共享文件夹映射到本地的一个驱动器号,如 Z:)。WNetCancelConnection2函数用于取消远程共享资源的连接。
具体步骤如下:
- 在代码中引入System.Runtime.InteropServices命名空间。
using System.Runtime.InteropServices;
- 声明WNetAddConnection2函数和WNetCancelConnection2方法。
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(ref NETRESOURCE lpNetResource, string lpPassword, string lpUsername, int dwFlags);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A(string lpName, int dwFlags, bool fForce);
- 创建NETRESOURCE对象。
[StructLayout(LayoutKind.Sequential)]
public 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;
}
- 调用WNetAddConnection2函数来连接共享资源并获取本地驱动器号。
NETRESOURCE nr = new NETRESOURCE();
nr.dwType = RESOURCETYPE_DISK;
nr.lpRemoteName = @"\\servername\sharedfolder";
int result = WNetAddConnection2A(ref nr, "password", "username", 0);
if (result == 0)
{
Console.WriteLine("Connection established");
string sharedFolder = @"Z:\folder\file.txt";
StreamReader reader = new StreamReader(sharedFolder);
Console.WriteLine(reader.ReadLine());
reader.Close();
}
else
{
Console.WriteLine("Connection failed");
}
- 使用完毕后,使用WNetCancelConnection2函数来取消连接。
int result = WNetCancelConnection2A(@"Z:", 0, true);
if (result == 0)
{
Console.WriteLine("Connection terminated successfully");
}
else
{
Console.WriteLine("Connection termination failed");
}
完整代码示例:
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace AccessNetworkSharedFolder
{
class Program
{
[StructLayout(LayoutKind.Sequential)]
public 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;
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(ref NETRESOURCE lpNetResource, string lpPassword, string lpUsername, int dwFlags);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A(string lpName, int dwFlags, bool fForce);
private const int RESOURCETYPE_DISK = 1;
static void Main(string[] args)
{
NETRESOURCE nr = new NETRESOURCE();
nr.dwType = RESOURCETYPE_DISK;
nr.lpRemoteName = @"\\servername\sharedfolder";
int result = WNetAddConnection2A(ref nr, "password", "username", 0);
if (result == 0)
{
Console.WriteLine("Connection established");
string sharedFolder = @"Z:\folder\file.txt";
StreamReader reader = new StreamReader(sharedFolder);
Console.WriteLine(reader.ReadLine());
reader.Close();
}
else
{
Console.WriteLine("Connection failed");
}
int disconnectResult = WNetCancelConnection2A(@"Z:", 0, true);
if (disconnectResult == 0)
{
Console.WriteLine("Connection terminated successfully");
}
else
{
Console.WriteLine("Connection termination failed");
}
}
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#访问网络共享文件夹的方法 - Python技术站