/// <summary> /// Get the drive where a share is hosted /// </summary> /// <param name="ServerName">Server hosting share</param> /// <param name="ShareName">Name of share</param> /// <returns>drive letter (empty string if unsuccessful)</returns> internal static string GetLocalDrive(string ServerName, string ShareName) { // // Ensure UNC path formatting at start // if (!ServerName.StartsWith(@"\\")) { if (!ServerName.StartsWith(@"\")) { ServerName = @"\\" + ServerName; } else { ServerName = @"\" + ServerName; } } string ShareDrive = String.Empty; string SharePath = String.Empty; IntPtr OutputBuffer = IntPtr.Zero; // // Attempt to get the share information. Report error if unsuccessful // Int32 ErrorCode = NetShareGetInfo(ServerName, ShareName, _InfoLevel, ref OutputBuffer); if (ErrorCode == (Int32)NetShareError.NERR_Success) { _SHARE_INFO_2 ShareInfo = (_SHARE_INFO_2)Marshal.PtrToStructure(OutputBuffer, typeof(_SHARE_INFO_2)); SharePath = ShareInfo.shi2_path; NetApiBufferFree(OutputBuffer); } else { PrintError(ErrorCode); return(ShareDrive); } if (!String.IsNullOrEmpty(SharePath)) { // // If a share path was returned, attempt to parse out the drive letter // int Index = SharePath.IndexOf(':'); if (Index > 0) { ShareDrive = SharePath.Substring(0, Index + 1); } } return(ShareDrive); }
/// <summary> /// Create a net share /// </summary> /// <param name="ServerName">Server hosting share</param> /// <param name="ShareName">name of share</param> /// <param name="SharePath">path to share</param> /// <param name="ShareDescription">description of share</param> /// <returns>success/failure</returns> internal static int CreateShare(string ServerName, string ShareName, string SharePath, string ShareDescription) { // // Ensure UNC path formatting at start // if (!ServerName.StartsWith(@"\\")) { if (!ServerName.StartsWith(@"\")) { ServerName = @"\\" + ServerName; } else { ServerName = @"\" + ServerName; } } // // Build share information // _SHARE_INFO_2 ShareInfo = new _SHARE_INFO_2(); ShareInfo.shi2_netname = ShareName; ShareInfo.shi2_type = _STYPE_DISKTREE; ShareInfo.shi2_remark = ShareDescription; ShareInfo.shi2_permissions = _PERM_FILE_READ | _PERM_FILE_WRITE | _PERM_FILE_CREATE; ShareInfo.shi2_max_uses = UInt32.MaxValue; ShareInfo.shi2_current_uses = 0; ShareInfo.shi2_path = SharePath; ShareInfo.shi2_passwd = String.Empty; IntPtr OutputBuffer = IntPtr.Zero; // // Create the share and report success or failure // Int32 ErrorCode = NetShareAdd(ServerName, _InfoLevel, ref ShareInfo, ref OutputBuffer); if (ErrorCode != (Int32)NetShareError.NERR_Success) { PrintError(ErrorCode); } return(ErrorCode); }
/// <summary> /// Returns the local path to a share on the specified server /// </summary> /// <param name="ServerName">Server the path resides on</param> /// <param name="ShareName">Share to find path to</param> /// <returns>Local path to share or empty string if failure</returns> internal static int GetLocalPath(string ServerName, string ShareName, out string SharePath) { // // Ensure UNC path formatting at start // if (!String.IsNullOrEmpty(ServerName) && !ServerName.StartsWith(@"\\")) { if (!ServerName.StartsWith(@"\")) { ServerName = @"\\" + ServerName; } else { ServerName = @"\" + ServerName; } } IntPtr OutputBuffer = IntPtr.Zero; SharePath = String.Empty; // // Get share info structure // Int32 ErrorCode = NetShareGetInfo(ServerName, ShareName, _InfoLevel, ref OutputBuffer); if (ErrorCode == (Int32)NetShareError.NERR_Success) { // // If successful, get the local path to the resource and free the buffer // _SHARE_INFO_2 ShareInfo = (_SHARE_INFO_2)Marshal.PtrToStructure(OutputBuffer, typeof(_SHARE_INFO_2)); SharePath = ShareInfo.shi2_path; NetApiBufferFree(OutputBuffer); } return(ErrorCode); }
/// <summary> /// Create a net share /// </summary> /// <param name="ServerName">Server hosting share</param> /// <param name="ShareName">name of share</param> /// <param name="SharePath">path to share</param> /// <param name="ShareDescription">description of share</param> /// <returns>success/failure</returns> internal static int CreateShare(string ServerName, string ShareName, string SharePath, string ShareDescription) { // // Ensure UNC path formatting at start // if (!ServerName.StartsWith(@"\\")) { if (!ServerName.StartsWith(@"\")) { ServerName = @"\\" + ServerName; } else { ServerName = @"\" + ServerName; } } // // Build share information // _SHARE_INFO_2 ShareInfo = new _SHARE_INFO_2(); ShareInfo.shi2_netname = ShareName; ShareInfo.shi2_type = _STYPE_DISKTREE; ShareInfo.shi2_remark = ShareDescription; ShareInfo.shi2_permissions = _PERM_FILE_READ | _PERM_FILE_WRITE | _PERM_FILE_CREATE; ShareInfo.shi2_max_uses = UInt32.MaxValue; ShareInfo.shi2_current_uses = 0; ShareInfo.shi2_path = SharePath; ShareInfo.shi2_passwd = String.Empty; IntPtr OutputBuffer = IntPtr.Zero; // // Create the share and report success or failure // Int32 ErrorCode = NetShareAdd(ServerName, _InfoLevel, ref ShareInfo, ref OutputBuffer); if (ErrorCode != (Int32)NetShareError.NERR_Success) { PrintError(ErrorCode); } return ErrorCode; }
private static extern Int32 NetShareAdd( [MarshalAs(UnmanagedType.LPWStr)] string ServerName, [MarshalAs(UnmanagedType.U4)] UInt32 InfoLevel, [MarshalAs(UnmanagedType.Struct)] ref _SHARE_INFO_2 ShareInfo, ref IntPtr OutputBuffer);