469,608 Members | 2,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,608 developers. It's quick & easy.

Shared folders

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
Mar 28 '07 #1
2 8949
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.");
}

Mar 29 '07 #2
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<stringGetSharedFolders(){

List<stringsharedFolders = 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

}





Apr 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Scherbina Vladimir | last post: by
1 post views Thread by nuggies | last post: by
1 post views Thread by Marcpp | last post: by
1 post views Thread by Gianni | last post: by
1 post views Thread by =?Utf-8?B?UHJhZGVlcCBFYXJsYQ==?= | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.