2016-04-25 238 views
0

如何共享文件夹槽C#。 enter image description here共享文件夹槽C#

如果我检查复选框(见图片)比我有我所需要的。 但我想用C#做到这一点,只需在本地文件夹路径。

感谢您的帮助。

+0

您需要添加参考'System.Management' DLL和相应的代码。 – Bikee

+0

你有没有检查过https://msdn.microsoft.com/en-us/library/bb425864.aspx文章并尝试设置'WHSSharePermissions'枚举?或者也许有一种方法可以在WMI中做到这一点.. –

+2

总体而言,我们不会为你编码这样的东西 - 在你的许多googlings中你没有找到任何代码尝试? – BugFinder

回答

1

此代码共享文件夹这里

private static void QshareFolder(string FolderPath, string ShareName, string Description) 
{ 
try{ 
    // Create a ManagementClass object 

    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 

    //Another Type: 

    // DISK_DRIVE = 0x0 

    // PRINT_QUEUE = 0x1 

    // DEVICE = 0x2 

    // IPC = 0x3 

    // DISK_DRIVE_ADMIN = 0x80000000 

    // PRINT_QUEUE_ADMIN = 0x80000001 

    // DEVICE_ADMIN = 0x80000002 

    // IPC_ADMIN = 0x8000003 

    //inParams["MaximumAllowed"] = int maxConnectionsNum; 

    // Invoke the method on the ManagementClass object 

    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 (Exception ex) 
{ 
//MessageBox.Show(ex.Message, "error!"); 
} 
} 

更多细节http://www.codeproject.com/Articles/18624/How-to-Share-Windows-Folders-Using-C

+0

非常好。谢谢。 – Raskolnikov

+0

不客气 – Mostafiz