Thursday, May 26, 2011

Method to create Share folder in web server C#

public void CreateSharedFolder(string PhysicalPath, bool bFolderCreated)
{
try
{
int index = PhysicalPath.LastIndexOf("\\");
string AliasFolder = PhysicalPath.Substring(index + 1);
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["Name"] = AliasFolder;
inParams["Path"] = PhysicalPath;
inParams["Type"] = 0x0; // Disk Drive
// 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)
{
if (bFolderCreated)
{
Directory.Delete(PhysicalPath, true);
}
throw new Exception("Unable to share directory as it is already in use.");
}
}
catch (Exception ex)
{
throw new Exception("Error :", ex);
}
}

No comments: