• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#中WNetAddConnection2A的使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
 
namespace NetConnect{
    public partial class NetWareFileCopy : Form
    {
        
        public NetWareFileCopy()
        {
            InitializeComponent();
        }

        private void btNetCopy_Click(object sender, EventArgs e)
        {
            NetWorkConnection();
        }

        #region Define NetWare Connect Class

        public enum ERROR_ID
        {
            ERROR_SUCCESS = 0,  // Success
            ERROR_BUSY = 170,
            ERROR_MORE_DATA = 234,
            ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
            ERROR_INVALID_LEVEL = 124,
            ERROR_ACCESS_DENIED = 5,
            ERROR_INVALID_PASSWORD = 86,
            ERROR_INVALID_PARAMETER = 87,
            ERROR_BAD_DEV_TYPE = 66,
            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,
            ERROR_DEVICE_ALREADY_REMEMBERED = 1202,
            ERROR_NO_NET_OR_BAD_PATH = 1203,
            ERROR_SESSION_CREDENTIAL_CONFLICT = 1219
        }

        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;
        }

        public class NetworkConnection
        {

            [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].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET;
                share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
                share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;
                share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;
                share_driver[0].lpLocalName = localPath;
                share_driver[0].lpRemoteName = remotePath;

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

                return ret;
            }

            public static int Disconnect(string localpath)
            {
                return WNetCancelConnection2A(localpath, 11);
            }

        }

        #endregion

        //调用:
        public static void NetWorkConnection()
        {
                string localpath = "Z:";
                string serverPath = @"\\10.0.0.1\FileShared";
                string loginUser = "userid";
                string loginPassword = "password";

                int status = NetworkConnection.Connect(serverPath, localpath, loginUser, loginPassword);
                if (status == (int)ERROR_ID.ERROR_SUCCESS)
                {
                    // 连接成功,在服务器端创建一文件,并写入一字串 , 
                    FileStream fs = new FileStream(localpath + @"//ConnectSuccess.txt", FileMode.OpenOrCreate);
                    using (StreamWriter stream = new StreamWriter(fs))
                    {
                        stream.WriteLine("网络连接成功");
                        stream.Flush();
                        stream.Close();
                    }
                    fs.Close();
                }
                else
                {
                    // 连接失败
                    Console.WriteLine(status);
                }
                // 断开连接
                NetworkConnection.Disconnect(localpath);
        }
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#在listview控件中显示数据库数据发布时间:2022-07-13
下一篇:
C#调用java包里的方法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap