告诉你ASP.NET(C#)如何建立或删除共享

  • 来源:网络
  • 更新日期:2020-08-04

摘要: 使用前要引用 System.Management /// <summary> /// 建立共享文件夹 /// </summary> /// <param name="shareName">共享名</param> /// <param n

使用前要引用 System.Management

/// <summary>
/// 建立共享文件夹
/// </summary>
/// <param name=shareName>共享名</param>
/// <param name=path>要共享的本地文件夹路径</param>
/// <returns>若返回值为0则成功</returns>
PRivate bool ShareAdd(string shareName, string path)
{
ManagementClass o = new ManagementClass(@\\\\localhost\\root\\cimv2, Win32_Share, null);
ManagementBaSEObject inputArgs = o.GetMethodParameters(Create);
inputArgs[Name] = shareName;
inputArgs[Path] = path;
inputArgs[Type] = 0; //Drive
ManagementBaseObject outParams = o.InvokeMethod(Create, inputArgs, null);
int ret = (int)(outParams.Properties[ReturnValue].Value);
o.Dispose();
return (ret == 0);
}
/// <summary>
/// 删除共享
/// </summary>
/// <param name=shareName>要删除的共享名</param>
private void ShareDel(string shareName)
{
string wsql = Select * From Win32_Share Where Name=\'C\';
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wsql);
if (searcher == null) return;
foreach (ManagementObject o in searcher.Get())
{
o.Delete();
}
}