How to make a folder shared with C#

  • Thread starter Thread starter ad
  • Start date Start date
I try some codes like below:
But it failed when compile time with message:
fs dosen't have create method

Can some help me?



private void CreateShare(string sGroup, string sUser)
{
string sRealUsrHomePath="d:\\UserHome";
string sDcComputerName="MyPC";
int fsflag = 0;
string sFS="WinNT://MyPC/LanmanServer,FileService";
DirectoryEntry fs = new DirectoryEntry(sFS);
DirectoryEntry shareforder;
try
{
shareforder = fs.create("Fileshare", sUser + "$");
} catch
{
fsflag = 1;
}
if (fsflag == 0)
{
shareforder.path = sRealUsrHomePath + "\\" + sGroup + "\\" + sUser;
shareforder.maxusercount = -1;
shareforder.setinfo();
}


}
 
You need to add a reference to the activeds.tlb
(%windir%\system32\activeds.tlb), and include a using directive to import
this activeds namespace.

using System;
using System.DirectoryServices;
using ....
using(DirectoryEntry lanmanserver = new
DirectoryEntry("WinNT://yourmachinename/Lanmanserver"))
{
IADsContainer fserv = lanmanserver.NativeObject as IADsContainer;
IADsFileShare sharenew = fserv.Create("fileshare", "NewShare") as
IADsFileShare;
sharenew.Path="C:\\";
sharenew.SetInfo();
}



Another way to achieve the same is by using System.Management classes,
here's a sample:

using System.Management;
....

using (ManagementClass o = new ManagementClass("\\\\.\\root\\cimv2",
"Win32_Share", null))
{
string method = "create";
ManagementBaseObject inputArgs = o.GetMethodParameters(method);
inputArgs["Name"] = "ImASharedfolder";
inputArgs["Path"] = "c:\\temp";
inputArgs["Description"] = "this is temp shared";
inputArgs["Type"] = 0; // Disk share type
ManagementBaseObject outParams = o.InvokeMethod(method, inputArgs, null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret != 0)
Console.WriteLine("Failed {0}", ret);
}


Willy.
 
Thank,
I like the DirectoryServices!

Willy Denoyette said:
You need to add a reference to the activeds.tlb
(%windir%\system32\activeds.tlb), and include a using directive to import
this activeds namespace.

using System;
using System.DirectoryServices;
using ....
using(DirectoryEntry lanmanserver = new
DirectoryEntry("WinNT://yourmachinename/Lanmanserver"))
{
IADsContainer fserv = lanmanserver.NativeObject as IADsContainer;
IADsFileShare sharenew = fserv.Create("fileshare", "NewShare") as
IADsFileShare;
sharenew.Path="C:\\";
sharenew.SetInfo();
}



Another way to achieve the same is by using System.Management classes,
here's a sample:

using System.Management;
...

using (ManagementClass o = new ManagementClass("\\\\.\\root\\cimv2",
"Win32_Share", null))
{
string method = "create";
ManagementBaseObject inputArgs = o.GetMethodParameters(method);
inputArgs["Name"] = "ImASharedfolder";
inputArgs["Path"] = "c:\\temp";
inputArgs["Description"] = "this is temp shared";
inputArgs["Type"] = 0; // Disk share type
ManagementBaseObject outParams = o.InvokeMethod(method, inputArgs, null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret != 0)
Console.WriteLine("Failed {0}", ret);
}


Willy.

ad said:
I have find a VB version at
http://support.microsoft.com/kb/169398/en-us?ln=en-us&sd=gn&fr=0
How can I rewrite it in C#
 
Back
Top