首页域名资讯 正文

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

2025-02-07 2 0条评论

使用前要引用 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();
    }
}




纵横数据面向全国提供域名注册虚拟主机、云服务器、服务器托管与租用,如需了解,请联系QQ: 171356849 微信:zh18159893430 咨询,谢谢!

文章版权及转载声明

本文作者:亿网 网址:https://edns.com/ask/post/145933.html 发布于 2025-02-07
文章转载或复制请以超链接形式并注明出处。