/// <summary>Creates a <see cref="ShareInfo"/> instance.</summary> /// <param name="host">A host to retrieve shares from.</param> /// <param name="shareLevel">One of the <see cref="ShareInfoLevel"/> options.</param> /// <param name="shareInfo">A <see cref="NativeMethods.ShareInfo2"/> or <see cref="NativeMethods.ShareInfo503"/> instance.</param> internal ShareInfo(string host, ShareInfoLevel shareLevel, object shareInfo) { switch (shareLevel) { case ShareInfoLevel.Info1005: NativeMethods.ShareInfo1005 s1005 = (NativeMethods.ShareInfo1005)shareInfo; ServerName = host ?? Environment.MachineName; ResourceType = s1005.ShareResourceType; break; case ShareInfoLevel.Info503: NativeMethods.ShareInfo503 s503 = (NativeMethods.ShareInfo503)shareInfo; CurrentUses = s503.CurrentUses; MaxUses = s503.MaxUses; NetName = s503.NetName; Password = s503.Password; Path = Utils.IsNullOrWhiteSpace(s503.Path) ? null : s503.Path; Permissions = s503.Permissions; Remark = s503.Remark; ServerName = s503.ServerName == "*" ? host ?? Environment.MachineName : s503.ServerName; ShareType = s503.ShareType; SecurityDescriptor = s503.SecurityDescriptor; break; case ShareInfoLevel.Info2: NativeMethods.ShareInfo2 s2 = (NativeMethods.ShareInfo2)shareInfo; CurrentUses = s2.CurrentUses; MaxUses = s2.MaxUses; NetName = s2.NetName; Password = s2.Password; Path = Utils.IsNullOrWhiteSpace(s2.Path) ? null : s2.Path; Permissions = s2.Permissions; Remark = s2.Remark; ServerName = host ?? Environment.MachineName; ShareType = s2.ShareType; break; case ShareInfoLevel.Info1: NativeMethods.ShareInfo1 s1 = (NativeMethods.ShareInfo1)shareInfo; CurrentUses = 0; MaxUses = 0; NetName = s1.NetName; Password = null; Path = null; Permissions = AccessPermissions.None; Remark = s1.Remark; ServerName = host ?? Environment.MachineName; ShareType = s1.ShareType; break; } NetFullPath = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}{3}", Filesystem.Path.UncPrefix, ServerName, Filesystem.Path.DirectorySeparatorChar, NetName); ShareLevel = shareLevel; }
/// <summary>Creates a <see cref="ShareInfo"/> instance.</summary> /// <param name="host">A host to retrieve shares from.</param> /// <param name="shareLevel">One of the <see cref="ShareInfoLevel"/> options.</param> /// <param name="shareInfo">A <see cref="NativeMethods.ShareInfo2"/> or <see cref="NativeMethods.ShareInfo503"/> instance.</param> internal ShareInfo(string host, ShareInfoLevel shareLevel, object shareInfo) { switch (shareLevel) { case ShareInfoLevel.Info1005: NativeMethods.ShareInfo1005 s1005 = (NativeMethods.ShareInfo1005) shareInfo; ServerName = host ?? Environment.MachineName; ResourceType = s1005.ShareResourceType; break; case ShareInfoLevel.Info503: NativeMethods.ShareInfo503 s503 = (NativeMethods.ShareInfo503) shareInfo; CurrentUses = s503.CurrentUses; MaxUses = s503.MaxUses; NetName = s503.NetName; Password = s503.Password; Path = Utils.IsNullOrWhiteSpace(s503.Path) ? null : s503.Path; Permissions = s503.Permissions; Remark = s503.Remark; ServerName = s503.ServerName == "*" ? host ?? Environment.MachineName : s503.ServerName; ShareType = s503.ShareType; SecurityDescriptor = s503.SecurityDescriptor; break; case ShareInfoLevel.Info2: NativeMethods.ShareInfo2 s2 = (NativeMethods.ShareInfo2)shareInfo; CurrentUses = s2.CurrentUses; MaxUses = s2.MaxUses; NetName = s2.NetName; Password = s2.Password; Path = Utils.IsNullOrWhiteSpace(s2.Path) ? null : s2.Path; Permissions = s2.Permissions; Remark = s2.Remark; ServerName = host ?? Environment.MachineName; ShareType = s2.ShareType; break; case ShareInfoLevel.Info1: NativeMethods.ShareInfo1 s1 = (NativeMethods.ShareInfo1)shareInfo; CurrentUses = 0; MaxUses = 0; NetName = s1.NetName; Password = null; Path = null; Permissions = AccessPermissions.None; Remark = s1.Remark; ServerName = host ?? Environment.MachineName; ShareType = s1.ShareType; break; } NetFullPath = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}{3}", Filesystem.Path.UncPrefix, ServerName, Filesystem.Path.DirectorySeparatorChar, NetName); ShareLevel = shareLevel; }
/// <summary>Creates a <see cref="ShareInfo"/> instance.</summary> /// <param name="host">A host to retrieve shares from.</param> /// <param name="shareLevel">One of the <see cref="ShareInfoLevel"/> options.</param> /// <param name="shareInfo">A <see cref="NativeMethods.SHARE_INFO_2"/> or <see cref="NativeMethods.SHARE_INFO_503"/> instance.</param> internal ShareInfo(string host, ShareInfoLevel shareLevel, object shareInfo) { switch (shareLevel) { case ShareInfoLevel.Info1005: NativeMethods.SHARE_INFO_1005 s1005 = (NativeMethods.SHARE_INFO_1005)shareInfo; ServerName = host ?? Environment.MachineName; ResourceType = s1005.shi1005_flags; break; case ShareInfoLevel.Info503: NativeMethods.SHARE_INFO_503 s503 = (NativeMethods.SHARE_INFO_503)shareInfo; CurrentUses = s503.shi503_current_uses; MaxUses = s503.shi503_max_uses; NetName = s503.shi503_netname; Password = s503.shi503_passwd; Path = Utils.IsNullOrWhiteSpace(s503.shi503_path) ? null : s503.shi503_path; Permissions = s503.shi503_permissions; Remark = s503.shi503_remark; ServerName = s503.shi503_servername == "*" ? host ?? Environment.MachineName : s503.shi503_servername; ShareType = s503.shi503_type; SecurityDescriptor = s503.shi503_security_descriptor; break; case ShareInfoLevel.Info2: NativeMethods.SHARE_INFO_2 s2 = (NativeMethods.SHARE_INFO_2)shareInfo; CurrentUses = s2.shi2_current_uses; MaxUses = s2.shi2_max_uses; NetName = s2.shi2_netname; Password = s2.shi2_passwd; Path = Utils.IsNullOrWhiteSpace(s2.shi2_path) ? null : s2.shi2_path; Permissions = s2.shi2_permissions; Remark = s2.shi2_remark; ServerName = host ?? Environment.MachineName; ShareType = s2.shi2_type; break; case ShareInfoLevel.Info1: NativeMethods.SHARE_INFO_1 s1 = (NativeMethods.SHARE_INFO_1)shareInfo; CurrentUses = 0; MaxUses = 0; NetName = s1.shi1_netname; Password = null; Path = null; Permissions = AccessPermissions.None; Remark = s1.shi1_remark; ServerName = host ?? Environment.MachineName; ShareType = s1.shi1_type; break; } NetFullPath = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}{3}", Filesystem.Path.UncPrefix, ServerName, Filesystem.Path.DirectorySeparatorChar, NetName); ShareLevel = shareLevel; }
internal static ShareInfo GetShareInfoCore(ShareInfoLevel shareLevel, string host, string share, bool continueOnException) { if (Utils.IsNullOrWhiteSpace(share)) { return(null); } // When host == null, the local computer is used. // However, the resulting OpenResourceInfo.Host property will be empty. // So, explicitly state Environment.MachineName to prevent this. // Furthermore, the UNC prefix: \\ is not required and always removed. string stripUnc = Utils.IsNullOrWhiteSpace(host) ? Environment.MachineName : Path.GetRegularPathCore(host, GetFullPathOptions.CheckInvalidPathChars, false).Replace(Path.UncPrefix, string.Empty); bool fallback = false; startNetShareGetInfo: SafeGlobalMemoryBufferHandle safeBuffer; uint structureLevel = Convert.ToUInt16(shareLevel, CultureInfo.InvariantCulture); uint lastError = NativeMethods.NetShareGetInfo(stripUnc, share, structureLevel, out safeBuffer); using (safeBuffer) { switch (lastError) { case Win32Errors.NERR_Success: switch (shareLevel) { case ShareInfoLevel.Info1005: return(new ShareInfo(stripUnc, shareLevel, safeBuffer.PtrToStructure <NativeMethods.SHARE_INFO_1005>(0)) { NetFullPath = Path.CombineCore(false, Path.UncPrefix + stripUnc, share) }); case ShareInfoLevel.Info503: return(new ShareInfo(stripUnc, shareLevel, safeBuffer.PtrToStructure <NativeMethods.SHARE_INFO_503>(0))); case ShareInfoLevel.Info2: return(new ShareInfo(stripUnc, shareLevel, safeBuffer.PtrToStructure <NativeMethods.SHARE_INFO_2>(0))); case ShareInfoLevel.Info1: return(new ShareInfo(stripUnc, shareLevel, safeBuffer.PtrToStructure <NativeMethods.SHARE_INFO_1>(0))); } break; // Observed when SHARE_INFO_503 is requested, but not supported/possible. // Fall back on SHARE_INFO_2 structure and try again. case Win32Errors.RPC_X_BAD_STUB_DATA: case Win32Errors.ERROR_ACCESS_DENIED: if (!fallback && shareLevel != ShareInfoLevel.Info2) { shareLevel = ShareInfoLevel.Info2; fallback = true; goto startNetShareGetInfo; } break; default: if (!continueOnException) { throw new NetworkInformationException((int)lastError); } break; } return(null); } }
public static ShareInfo GetShareInfo(ShareInfoLevel shareLevel, string host, string share, bool continueOnException) { return(GetShareInfoCore(shareLevel, host, share, continueOnException)); }
public static ShareInfo GetShareInfo(ShareInfoLevel shareLevel, string uncPath, bool continueOnException) { string[] unc = GetHostShareFromPath(uncPath); return(GetShareInfoCore(shareLevel, unc[0], unc[1], continueOnException)); }
internal static ShareInfo GetShareInfoInternal(ShareInfoLevel shareLevel, string host, string share, bool continueOnException) { if (Utils.IsNullOrWhiteSpace(share)) return null; // When host == null, the local computer is used. // However, the resulting OpenResourceInfo.Host property will be empty. // So, explicitly state Environment.MachineName to prevent this. // Furthermore, the UNC prefix: \\ is not required and always removed. string stripUnc = Utils.IsNullOrWhiteSpace(host) ? Environment.MachineName : Path.GetRegularPathInternal(host, GetFullPathOptions.CheckInvalidPathChars).Replace(Path.UncPrefix, string.Empty); bool fallback = false; startNetShareGetInfo: var buffer = IntPtr.Zero; try { uint structureLevel = Convert.ToUInt16(shareLevel, CultureInfo.InvariantCulture); uint lastError = NativeMethods.NetShareGetInfo(stripUnc, share, structureLevel, out buffer); switch (lastError) { case Win32Errors.NERR_Success: switch (shareLevel) { case ShareInfoLevel.Info1005: return new ShareInfo(stripUnc, shareLevel, Utils.MarshalPtrToStructure<NativeMethods.ShareInfo1005>(0, buffer)) { NetFullPath = Path.CombineInternal(false, Path.UncPrefix + stripUnc, share) }; case ShareInfoLevel.Info503: return new ShareInfo(stripUnc, shareLevel, Utils.MarshalPtrToStructure<NativeMethods.ShareInfo503>(0, buffer)); case ShareInfoLevel.Info2: return new ShareInfo(stripUnc, shareLevel, Utils.MarshalPtrToStructure<NativeMethods.ShareInfo2>(0, buffer)); case ShareInfoLevel.Info1: return new ShareInfo(stripUnc, shareLevel, Utils.MarshalPtrToStructure<NativeMethods.ShareInfo1>(0, buffer)); } break; // Observed when ShareInfo503 is requested, but not supported/possible. // Fall back on ShareInfo2 structure and try again. case Win32Errors.RPC_X_BAD_STUB_DATA: case Win32Errors.ERROR_ACCESS_DENIED: if (!fallback && shareLevel != ShareInfoLevel.Info2) { NativeMethods.NetApiBufferFree(buffer); shareLevel = ShareInfoLevel.Info2; fallback = true; goto startNetShareGetInfo; } break; default: if (!continueOnException) throw new NetworkInformationException((int)lastError); break; } return null; } finally { if (buffer != IntPtr.Zero) NativeMethods.NetApiBufferFree(buffer); } }
public static ShareInfo GetShareInfo(ShareInfoLevel shareLevel, string host, string share, bool continueOnException) { return GetShareInfoInternal(shareLevel, host, share, continueOnException); }
public static ShareInfo GetShareInfo(ShareInfoLevel shareLevel, string uncPath, bool continueOnException) { string[] unc = GetHostShareFromPath(uncPath); return GetShareInfoInternal(shareLevel, unc[0], unc[1], continueOnException); }