C#设置和取消文件夹共享

/// <summary> /// 设置文件夹共享 /// </summary> /// <param name="FolderPath">文件夹路径</param> /// <param name="ShareName">共享名</param> /// <param name="Description">共享注释</param> /// <returns></returns> public int ShareNetFolder(string FolderPath, string ShareName, string Description) { try { ManagementClass managementClass = new ManagementClass("Win32_Share"); // Create ManagementBaseObjects for in and out parameters ManagementBaseObject inParams = managementClass.GetMethodParameters("Create"); ManagementBaseObject outParams; // Set the input parameters inParams["Description"] = Description; inParams["Name"] = ShareName; inParams["Path"] = FolderPath; inParams["Type"] = 0x0; // Disk Drive outParams = managementClass.InvokeMethod("Create", inParams, null); // Check to see if the method invocation was successful if ((uint)(outParams.Properties["ReturnValue"].Value) != 0) { throw new Exception("Unable to share directory."); } } catch { return -1; } return 0; } /// <summary> /// 取消文件夹共享 /// </summary> /// <param name="ShareName">文件夹的共享名</param> /// <returns></returns> public int CancelShareNetFolder(string ShareName) { try { SelectQuery selectQuery = new SelectQuery("Select * from Win32_Share Where Name = '" + ShareName + "'"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery); foreach (ManagementObject mo in searcher.Get()) { mo.InvokeMethod("Delete", null, null); } } catch { return -1; } return 0; } 

你可能感兴趣的:(C#设置和取消文件夹共享)