Sharing folders

  • Thread starter Thread starter Mystique
  • Start date Start date
Hi,

You can use WMI to create a share, the following example should get you
started.

using(System.Management.ManagementClass Win32Share = new
ManagementClass("Win32_Share"))
using(System.Management.ManagementBaseObject parameters =
Win32Share.GetMethodParameters("Create"))
{
parameters["Name"] = "ShareName";
parameters["Path"] = "c:\\Temp";
parameters["Type"] = 0;
parameters["Access"] = null;
parameters["Description"] ="Shared programatically from WMI";
parameters["MaximumAllowed"] = 5;

using(System.Management.ManagementBaseObject result =
Win32Share.InvokeMethod("Create", parameters, null))
{
if (((uint)result["ReturnValue"]) == 0)
{
MessageBox.Show("Folder shared");
}
else
{
MessageBox.Show("Folder share failed");
}
}
}

Hope this helps
 
thanks Chris
it worked :-)

do you know how i can get a list of all the folders that i are shared on my
PC?

Regards,
Mystique

Chris Taylor said:
Hi,

You can use WMI to create a share, the following example should get you
started.

using(System.Management.ManagementClass Win32Share = new
ManagementClass("Win32_Share"))
using(System.Management.ManagementBaseObject parameters =
Win32Share.GetMethodParameters("Create"))
{
parameters["Name"] = "ShareName";
parameters["Path"] = "c:\\Temp";
parameters["Type"] = 0;
parameters["Access"] = null;
parameters["Description"] ="Shared programatically from WMI";
parameters["MaximumAllowed"] = 5;

using(System.Management.ManagementBaseObject result =
Win32Share.InvokeMethod("Create", parameters, null))
{
if (((uint)result["ReturnValue"]) == 0)
{
MessageBox.Show("Folder shared");
}
else
{
MessageBox.Show("Folder share failed");
}
}
}

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


Mystique said:
Hi guys

How i make a folder to be shared with C# ?

Regards,
Mystique
 
You can also use the Win32_Share class to list all shares on your computer:

ManagementScope scope = new ManagementScope("\\root\\cimv2");
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Options.EnablePrivileges = true;

ManagementPath path = new ManagementPath("Win32_Share");
ManagementClass osClass = new ManagementClass(scope, path, new
ObjectGetOptions(null));

foreach (ManagementObject os in osClass.GetInstances())
Console.WriteLine(os.Properties["Path"].Value.ToString());

HTH, Jakob.
--
http://www.dotninjas.dk
http://www.powerbytes.dk


Mystique said:
thanks Chris
it worked :-)

do you know how i can get a list of all the folders that i are shared on my
PC?

Regards,
Mystique

Chris Taylor said:
Hi,

You can use WMI to create a share, the following example should get you
started.

using(System.Management.ManagementClass Win32Share = new
ManagementClass("Win32_Share"))
using(System.Management.ManagementBaseObject parameters =
Win32Share.GetMethodParameters("Create"))
{
parameters["Name"] = "ShareName";
parameters["Path"] = "c:\\Temp";
parameters["Type"] = 0;
parameters["Access"] = null;
parameters["Description"] ="Shared programatically from WMI";
parameters["MaximumAllowed"] = 5;

using(System.Management.ManagementBaseObject result =
Win32Share.InvokeMethod("Create", parameters, null))
{
if (((uint)result["ReturnValue"]) == 0)
{
MessageBox.Show("Folder shared");
}
else
{
MessageBox.Show("Folder share failed");
}
}
}

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


Mystique said:
Hi guys

How i make a folder to be shared with C# ?

Regards,
Mystique
 
Back
Top