Shared folders

D

Daniel Knöpfel

Hi

In my application i need to do the following things concerning shared
folders:

- check if a folder is shared
- share a folder
- unshare a folder

It seems to me that .net does not support this by default. Is there a way to
do these things programmatically? I am grateful for any help.

Thanks in advance

daniel
 
L

Latish Sehgal

You need to use WMI classes.
Add a reference to System.management and use the ManagementClass,
ManagementBaseObject classes

//Sample Code :-

using System.Management;

// 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"] = "My Files Share";
inParams["Name"] = "My Files Share";
inParams["Path"] = @"C:\MyTestShare";
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)
{
throw new Exception("Unable to share directory.");
}
 
D

Daniel Knöpfel

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

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top