if (!Directory.Exists(@"D:\sharedfile"))
{
Directory.CreateDirectory(@"D:\sharedfile");
}
int result = ShareNetFolder(@"D:\sharedfile", "sharedfile", "共享注释");
if (result >= 0)
MessageBox.Show("共享成功!");
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;
}