C#访问共享文件夹问题 5

我想访问一个共享文件夹,于是我就直接写了一条代码System.Diagnostics.Process.Start(@"\\192.168.0.2\文件夹");我想判断是否... 我想访问一个共享文件夹,于是我就直接写了一条代码

System.Diagnostics.Process.Start(@"\\192.168.0.2\文件夹");

我想判断是否成功连接共享文件夹,应该怎么写?
展开
 我来答
定复2d
2009-03-14 · TA获得超过3196个赞
知道大有可为答主
回答量:2052
采纳率:0%
帮助的人:1928万
展开全部
调用windows的API吧

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
#region Windows API enum and struct
public enum ERROR_ID
{
ERROR_SUCCESS = 0, // Success
ERROR_MORE_DATA = 234,
ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
ERROR_INVALID_LEVEL = 124,
ERROR_ACCESS_DENIED = 5,
ERROR_INVALID_PARAMETER = 87,
ERROR_NOT_ENOUGH_MEMORY = 8,
ERROR_NETWORK_BUSY = 54,
ERROR_BAD_NETPATH = 53,
ERROR_NO_NETWORK = 1222,
ERROR_INVALID_HANDLE_STATE = 1609,
ERROR_EXTENDED_ERROR = 1208
}

public enum RESOURCE_SCOPE
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET = 2,
RESOURCE_REMEMBERED = 3,
RESOURCE_RECENT = 4,
RESOURCE_CONTEXT = 5
}

public enum RESOURCE_TYPE
{
RESOURCETYPE_ANY = 0,
RESOURCETYPE_DISK = 1,
RESOURCETYPE_PRINT = 2,
RESOURCETYPE_RESERVED = 8,
}

public enum RESOURCE_USAGE
{
RESOURCEUSAGE_CONNECTABLE = 1,
RESOURCEUSAGE_CONTAINER = 2,
RESOURCEUSAGE_NOLOCALDEVICE = 4,
RESOURCEUSAGE_SIBLING = 8,
RESOURCEUSAGE_ATTACHED = 16,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
}

public enum RESOURCE_DISPLAYTYPE
{
RESOURCEDISPLAYTYPE_GENERIC = 0,
RESOURCEDISPLAYTYPE_DOMAIN = 1,
RESOURCEDISPLAYTYPE_SERVER = 2,
RESOURCEDISPLAYTYPE_SHARE = 3,
RESOURCEDISPLAYTYPE_FILE = 4,
RESOURCEDISPLAYTYPE_GROUP = 5,
RESOURCEDISPLAYTYPE_NETWORK = 6,
RESOURCEDISPLAYTYPE_ROOT = 7,
RESOURCEDISPLAYTYPE_SHAREADMIN = 8,
RESOURCEDISPLAYTYPE_DIRECTORY = 9,
RESOURCEDISPLAYTYPE_TREE = 10,
RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
}

[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCE
{
public RESOURCE_SCOPE dwScope;
public RESOURCE_TYPE dwType;
public RESOURCE_DISPLAYTYPE dwDisplayType;
public RESOURCE_USAGE dwUsage;

[MarshalAs(UnmanagedType.LPStr)]
public string lpLocalName;

[MarshalAs(UnmanagedType.LPStr)]
public string lpRemoteName;

[MarshalAs(UnmanagedType.LPStr)]
public string lpComment;

[MarshalAs(UnmanagedType.LPStr)]
public string lpProvider;
}

#endregion

/**//// <summary>
/// 连接网络上的共享文件夹
/// </summary>
public class NetworkSharedFolder
{

[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A(NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags);

[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A(string sharename, int dwFlags, int fForce);

public static int Connect(string remotePath, string localPath, string username, string password)
{
NETRESOURCE[] share_driver = new NETRESOURCE[1];
share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
share_driver[0].lpLocalName = localPath;
share_driver[0].lpRemoteName = remotePath;

Disconnect(localPath);
int ret = WNetAddConnection2A( share_driver, password, username, 1);

return ret;
}

public static void Disconnect(string localpath)
{
WNetCancelConnection2A(localpath, 1, 1);
}

public static void Test()
{
int ret = NetworkSharedFolder.Connect(@"\\192.168.1.2\log", @"Y:", "user1", "user1");
System.Console.WriteLine("connect ret = " + ret);
}
}
}

参考资料: http://topic.csdn.net/u/20081205/10/bb7598b9-5690-40d4-a00e-b62b77c44b86.html

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
消失的日不落
2009-03-14 · 超过30用户采纳过TA的回答
知道答主
回答量:189
采纳率:0%
帮助的人:0
展开全部
用到网络链接了!
应该不是这么写(个人意见)!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式