示例#1
0
        /// <summary>
        /// Accesses the UNC path with shared network resource using <see cref="Credential"/>.
        /// </summary>
        /// <param name="credential">Username and password required for access to the shared network resource.</param>
        public void Connect(Credential credential)
        {
            if (credential == null)
            {
                _disposed = true;
                return;
            }
            string[] path_split = _path.Split(new char[] { '\\' });
            string   domain     = path_split.Length > 2 ? path_split[2] : null;
            UseInfo2 useinfo    = new UseInfo2 {
                ui2_remote     = _path,
                ui2_domainname = domain,
                ui2_username   = credential.Username,
                ui2_password   = credential.Password,
                ui2_asg_type   = 0, // USE_WILDCARD
                ui2_usecount   = 1
            };

            NetUseAdd(null, 2, ref useinfo, out uint parmErr); // 2 = useinfo is USE_INFO_2
        }
        private bool NetUseWithCredentials()
        {
            try
            {
                var useinfo = new UseInfo2
                {
                    ui2_remote = _sUncPath,
                    ui2_username = _sUser,
                    ui2_domainname = _sDomain,
                    ui2_password = _sPassword,
                    ui2_asg_type = 0,
                    ui2_usecount = 1
                };

                uint paramErrorIndex;
                var returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                LastError = (int)returncode;
                return returncode == 0;
            }
            catch
            {
                LastError = Marshal.GetLastWin32Error();
                return false;
            }
        }
 internal static extern UInt32 NetUseAdd(
     String uncServerName,
     UInt32 level,
     ref UseInfo2 buf,
     out UInt32 parmError);
        private bool NetUseWithCredentials()
        {
            uint returncode;
            try
            {
                UseInfo2 useinfo = new UseInfo2();

                useinfo.ui2_remote = this.sUNCPath;
                useinfo.ui2_username = this.sUser;
                useinfo.ui2_domainname = this.sDomain;
                useinfo.ui2_password = this.sPassword;
                useinfo.ui2_asg_type = 0;
                useinfo.ui2_usecount = 1;
                uint paramErrorIndex;
                returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                this.iLastError = (int)returncode;
                return returncode == 0;
            }
            catch
            {
                this.iLastError = Marshal.GetLastWin32Error();
                return false;
            }
        }
 internal static extern UInt32 NetUseAdd(
     String UncServerName,
     UInt32 Level,
     ref UseInfo2 Buf,
     out UInt32 ParmError);
示例#6
0
 private static extern uint NetUseAdd(string uncServerName, uint levelFlags, ref UseInfo2 buf, out uint parmErr);
 internal static extern uint NetUseAdd(
     string uncServerName,
     uint level,
     ref UseInfo2 buf,
     out uint parmError);