Hi
Thanks for your answer. I just want to provide you with some of the code i
have now implemented. There might be better ways but it works for me.
Greetings
/// <summary>
/// Retrieves all shared folders in the filesystem
/// </summary>
/// <returns>list of pathes of shared folders</returns>
public static List<string> GetSharedFolders(){
List<string> sharedFolders = new List<string>();
// Object to query the WMI Win32_Share API for shared files...
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select *
from win32_share");
ManagementBaseObject outParams;
ManagementClass mc = new ManagementClass("Win32_Share"); //for local shares
foreach (ManagementObject share in searcher.Get()){
string type = share["Type"].ToString();
if (type == "0") // 0 = DiskDrive (1 = Print Queue, 2 = Device, 3 = IPH,
2147483648 = Disk Drive Admin, 2147483649 = Print Queue Admin, 2147483650 =
Device Admin, else probably IPH Admin
{
string name = share["Name"].ToString(); //getting share name
string path = share["Path"].ToString(); //getting share path
string caption = share["Caption"].ToString(); //getting share description
sharedFolders.Add(path);
}
}
return sharedFolders;
}
/// <summary>
/// Makes a folder a unshared folder
/// </summary>
/// <param name="pRelativePath">relative path to folder to should not be
shared</param>
/// <returns>true if folder is not shared (also true if folder was not
shared before)</returns>
public static bool RemoveSharingFromAFolder(string pRelativePath)
{
// try to find file name
string pathOfolderToRemoveSharing =
FileHandler.ReturnFullFileName(pRelativePath);
DirectoryInfo dirInfo = new DirectoryInfo(pRelativePath);
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select *
from win32_share");
ManagementBaseObject outParams;
ManagementClass mc = new ManagementClass("Win32_Share"); //for local shares
foreach (ManagementObject share in searcher.Get())
{
string type = share["Type"].ToString();
if (type == "0") // 0 = DiskDrive (1 = Print Queue, 2 = Device, 3 = IPH,
2147483648 = Disk Drive Admin, 2147483649 = Print Queue Admin, 2147483650 =
Device Admin, else probably IPH Admin
{
string path = share["Path"].ToString(); //getting share path
if (path == pathOfolderToRemoveSharing)
{
outParams = share.InvokeMethod("delete", null, null);
if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
{
string sMessage = "Unable to unshare directory. ";
int returnValue =
(((int)(uint)(outParams.Properties["ReturnValue"].Value)));
switch (returnValue)
{
case (2):
sMessage += "Access denied";
break;
case (22):
sMessage += "Duplicate share";
break;
default:
break;
}
throw new ApplicationException(sMessage);
}else{
return true; // successfull
}
}
}
}
return true; // folder was not shared before
}