473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 9629
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scherbina Vladimir | last post by:
is there any function to get the names of shared folders ?
0
by: Dennis | last post by:
I have tried to use the IWshRuntimeLibrary and ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk") to get all Logical Drives, Mapped Network Drives, Shared Folders and Shared Drives with...
1
by: nuggies | last post by:
I'm developing an ASP.NET 2.0 (C#) application in which I need to display the available shared folders on the server. The shared folder paths need to be in UNC format (i.e., \\servername\share)...
3
by: sva | last post by:
Using C# for an application in which I am working on, I need to display the available shared folders on the computer that's running the application. The shared folder paths need to be in UNC format...
1
by: lecnac | last post by:
Here's some details: Server and workstation both in the same workgroup Logged into server as local Administrator Logged into workstation as a local user that is only in the Users group The...
1
by: lecnac | last post by:
Sorry for the repost. I must have done something wrong when I tried to post my reply (I can't seem to find it). Anyway, I'd really appreciate any help that anyone could provide. My issue is...
1
by: Marcpp | last post by:
Any idea to work with windows shared folders? Which library is needed to execute console comands? Thankyou.
1
by: Gianni | last post by:
Hi, I would like to know how can I recover the shared folders of a remote computer with vb dotnet
1
by: =?Utf-8?B?UHJhZGVlcCBFYXJsYQ==?= | last post by:
Hi I am having one windows application on my local machine and I want to display the folders(shared folders) of remote machine in tree view control of my win application. I haved added me as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.