 |
|
 |
Hi there,
This excellent snippet is something I use over and over in .NET development.
Do you mind if I include this (with some additions) to a .NET library I am hoping to "Open Source" (MIT license?)
Of course, your name and this article will be embedded in the MIT license notice.
Cheers,
Shermerhorn
|
|
|
|
 |
|
 |
This took me a long time to figure out so I would like to share it with the community. This will add 'ReadAndExecute' permissions to the everyone account on the newly created share. This has been tested across both WinXP and Win7. I imagine it will work in Vista as well. My additions are in bold.
DirectorySecurity dirSec = new DirectorySecurity( );
System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier( System.Security.Principal.WellKnownSidType.WorldSid, null );
System.Security.Principal.NTAccount acct = sid.Translate( typeof( System.Security.Principal.NTAccount ) ) as System.Security.Principal.NTAccount;
string strEveryoneAccount = acct.ToString( );
dirSec.AddAccessRule( new FileSystemAccessRule( strEveryoneAccount, FileSystemRights.ReadAndExecute, AccessControlType.Allow ) );
byte[] managedSD = dirSec.GetSecurityDescriptorBinaryForm( );
int sdSize = Marshal.SizeOf( managedSD[0] ) * managedSD.Length;
shInfo.shi502_security_descriptor = Marshal.AllocHGlobal( sdSize );
Marshal.Copy( dirSec.GetSecurityDescriptorBinaryForm( ), 0, shInfo.shi502_security_descriptor, dirSec.GetSecurityDescriptorBinaryForm( ).Length );
string strTargetServer = strServer;
if ( strServer.Length != 0 )
{
strTargetServer = strServer;
if ( strServer[0] != '\\' )
{
strTargetServer = "\\\\" + strServer;
}
}
int nRetValue = 0;
// Call Net API to add the share..
int nStSize = Marshal.SizeOf( shInfo );
IntPtr buffer = Marshal.AllocCoTaskMem( nStSize );
Marshal.StructureToPtr( shInfo, buffer, false );
nRetValue = NetApi32.NetShareAdd( strTargetServer, 502,
buffer, IntPtr.Zero );
Marshal.FreeHGlobal( shInfo.shi502_security_descriptor );
Marshal.FreeCoTaskMem( buffer );
return ( NetApi32.NetError )nRetValue;
modified on Tuesday, June 8, 2010 12:23 PM
|
|
|
|
 |
|
 |
This solved my permission problem. GREAT example, thank you!
Regards, Jazper
|
|
|
|
 |
|
 |
I noticed you have not gone into how to set the share permissions so just thought I would post a link to my blog post that has an example of how to do this from a a VB.NET app:
http://cjwdev.wordpress.com/2010/05/27/shared-a-folder-and-setting-share-permissions-from-vb-net/
Hope someone finds it useful!
Chris
|
|
|
|
 |
|
 |
Hi,
"NetShareDel" is not able to remove the sharing of a folder on Desktop of a vista Machine.
It is working fine for other OS like Xp , for other folders on Vista
but not for a folder on Desktop
Do anybody have any idea ??
Do help me plz
DDeepali
|
|
|
|
 |
|
 |
Hi,
I wrote a corresponding function for NetShareDel. It is failing with error code 2310.
Could You please suggest me what would be reason for that !
Thanks!
Sushil
|
|
|
|
 |
|
 |
It seems that the API is not able to find the correct share name. even though it exists.
Any suggestions ?
|
|
|
|
 |
|
 |
Hi Sushil
If you doesn't have the Share with the name which you are trying to delete then it will return error code: 2310.
You can delete an existing share using following declaration.
[DllImport("Netapi32.dll")]
public static extern bool NetShareDel([MarshalAs(UnmanagedType.LPWStr)] string strServer, [MarshalAs(UnmanagedType.LPWStr)] string strShareName, int reserved);
|
|
|
|
 |
|
|
 |
|
 |
This is a great example, thanks a million. Once thing I would like to know is if this code can be tweaked to add Windows and Macintosh share, and set permissions for both.
Thanks if you can shed some light on this.
Best Regards,
Ervin.
|
|
|
|
 |
|
 |
.NET currently doesn't work on macs. Sorry.
|
|
|
|
 |
|
 |
Hi I don't mean to create a share on a Macintosh machine, but rather to create a Macintosh share on a Windows server. Is this possible with .NET
|
|
|
|
 |
|
 |
Use the macfile program.
It is available on your file server after you install Service for Macintosh (SFM)
|
|
|
|
 |
|
 |
Does anyone know of a similar way to remove a share?
|
|
|
|
 |
|
 |
You should be able to follow the code provided for NetShareAdd and reverse it a bit in order to do a NetShareDel
|
|
|
|
 |
|
 |
This works for me (2000 server/xp):
[DllImport("Netapi32.dll")]
public static extern bool NetShareDel([MarshalAs(UnmanagedType.LPWStr)] string strServer, MarshalAs(UnmanagedType.LPWStr)] string strShareName, int reserved);
It seems reserved needs to be zero alway's
|
|
|
|
 |
|
 |
Any chance of an example that shows how to set the permissions of the new share?
|
|
|
|
 |
|
 |
Working on it
---
Softomatix
http://www.pardesifashions.com/Softomatix/default.aspx
|
|
|
|
 |
|
 |
I had a look to see if it was something I could write but it looked a little complicated
|
|
|
|
 |
|
 |
Have you got anywhere on the permissions side of things? I've tried this example on XP and found that other computers on my network could not access the shares that this code creates. Please help!
Matt
|
|
|
|
 |
|
 |
I am having the same problem. I can get the function NetShareAdd to run succesfully and create a shared folder. However, I can not open the folder on other computers on my network. An error message appears that says I do not have the right permission to access the folder. I am also using windows XP on all the computers. Did you find out how to solve this problem. Any help will be greatly appreciated. Thanks
|
|
|
|
 |
|
 |
see http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=E6098575-DDA0-48B8-9ABF-E0705AF065D9[^]
sample code:
result1 = AD_ShareUtil.CreateShare(null,path,faxShare,"DESCRIPTION",false);
if ( (result1 != NetApi32.NetError.NERR_Success) && (result1 != NetApi32.NetError.NERR_NetNameNotFound))
MessageBox.Show(String.Format("Cannot create network share fxsSrvCp$: {0}",result1),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
SecurityDescriptor desc = SecurityDescriptor.GetNamedSecurityInfo(faxShare,SE_OBJECT_TYPE.SE_LMSHARE,SECURITY_INFORMATION.DACL_SECURITY_INFORMATION);
Dacl dacl ;
if (desc == null)
{
desc = new SecurityDescriptor();
desc.AllocateAndInitializeSecurityDescriptor();
dacl = new Dacl();
}
else
{
dacl = desc.Dacl;
}
dacl.SetEmpty();
dacl.AddAce (new AceAccessAllowed (new Sid ("BUILTIN\\Administrators"), AccessType.GENERIC_ALL));
dacl.AddAce (new AceAccessAllowed (new Sid ("Everyone"), AccessType.GENERIC_READ | AccessType.GENERIC_EXECUTE | AccessType.READ_CONTROL |
AccessType.STANDARD_RIGHTS_READ));
desc.SetDacl(dacl);
desc.SetNamedSecurityInfo(faxShare,SE_OBJECT_TYPE.SE_LMSHARE ,SECURITY_INFORMATION.DACL_SECURITY_INFORMATION);
note also my message about threee bugs.
Sincerely,
Marat Bakirov mbakirov@mail.ru
-- modified at 2:18 Friday 23rd September, 2005
|
|
|
|
 |
|
 |
If we are not setting any permissions on the share then isnt using SHARE_INFO_2 a lot easier?
|
|
|
|
 |
|
 |
a) use Management namespace classes -> "Win32_Share"
b) use Scripting Host Network object (WshNetwork)
ManagementPath path = new ManagementPath();
path.Server = "serverName"; // remote server NetBios name or host name
path.NamespacePath = @"root\CIMV2";
path.RelativePath = @"Win32_Share";
ConnectionOptions co = new ConnectionOptions();
// account with WMI access privileges on remote server (Remote enabled, execute methods ....)
co.Username = "userAccount";
co.Password = "hisPassword";
// downlevel domain name or server name if local account
co.Authority = "NTLMDOMAIN:domainName";
ManagementScope scope = new ManagementScope( path, co);
ManagementClass mc = new System.Management.ManagementClass( scope, path, null);
System.Management.ManagementBaseObject inParams = mc.GetMethodParameters("Create");
inParams["Name"] = "my_share_name";
inParams["Path"] = @"c:\directory_to_be_shared";
inParams["Type"] = 0;
inParams["Access"] = null;
inParams["Description"] ="my share description";
inParams["MaximumAllowed"] = 5;
System.Management.ManagementBaseObject mbo = mc.InvokeMethod("Create", inParams, null);
uint result = (uint) mbo.Properties["ReturnValue"].Value;
// 0 = success
|
|
|
|
 |
|
 |
This way was much easier for me.
--Mark
|
|
|
|
 |