Monday, October 27, 2003 11:45 PM bart

Creating shares with "backslash-sensitive strings" :(

I like the .NET Framework very much but there are still a lot of things which are not in there today (but which will be covered in Whidbey). Just think of setting permissions on files and folders. Fortunately there are some great libraries to help people with this ugly task, e.g. http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=e6098575-dda0-48b8-9abf-e0705af065d9. Today I had to fight with my C# code to get a share to work from within managed code. Not that easy but a pain to debug as well... For those interested this is the tricky Win32 call:

[DllImport("Netapi32.dll")]

private static extern int NetShareAdd(
[MarshalAs(UnmanagedType.LPWStr)]
string server, Int32 dwLevel, IntPtr buf, IntPtr parm_err);

It did the trick, however there were some issues to get it right... Never use a backslash at the end of a path, for example c:\temp\. This call doesn't like that at all and returns a great error with number 123 :) The Platform SDK was a must to sort it out while debugging. But finally, everything is working great. So, this problem did not affect my great love: .NET :)

Can't wait to get it to work in "Whidbey"...

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

# re: Creating shares with "backslash-sensitive strings" :(

Friday, October 31, 2003 10:38 AM by bart

Bart,
Managing file permissions is possible in v1.1 too without resorting to PInvoke.
Whitbey will add this to the BCL, but as of today you can use the System.Management namespace classes that wraps WMI functionality to do that, on XP and higher it's also possible to use the DirectoryServices namespace classes to manage ACL's on Filesystem objects and AD objects.
If you need a sample of both methods just drop me a mail.

# re: Creating shares with "backslash-sensitive strings" :(

Friday, October 31, 2003 11:45 AM by bart

Bart,
Another point I forgot in my previous feedback is that the same namespaces can be used to create Fileshares Printershares etc.

Here is a sample using the DirectoryServices with some COM interop with the activeds.dll.

using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using activedsnet;
class Tester {
public static void Main() {
// DirectoryEntry container = new DirectoryEntry("WinNT://yourcomputernamehere, computer", null, null, AuthenticationTypes.ServerBind);
using (DirectoryEntry container = new DirectoryEntry("WinNT://scenic/LanmanServer", "administrator", "kevin", AuthenticationTypes.ServerBind))
{
IADsContainer cont = container.NativeObject as IADsContainer;
if (cont != null) {
// Share the path as PublicDocs
IADsFileShare FileShare = cont.Create("FileShare", "PublicDocs") as IADsFileShare;
FileShare.Path = @"c:\pipo";
FileShare.Description = "MyPipo";
FileShare.MaxUserCount = 2;
FileShare.SetInfo();
}
}

}
}

// K:\NetV1.1\Management>csc /r:activedsnet.dll fileshares.cs

To run the sample you need to create a Interop Assembly by running tlbimp on the activeds.tlb (found in the system32 dir)
fi .
tlbimp c:\windows\system32\activeds.tlb out:activedsnet.dll

will create a IA called activedsnet.dll containing a namespace activedsnet

Adapt the path to your own needs, compile the program and run

Success.
Willy.

# re: Creating shares with "backslash-sensitive strings" :(

Tuesday, January 20, 2004 11:25 PM by bart

It seems to be fine... But i think there a problem.
I had to work with shares in vb.net and i get an error the path in my shares is higher than 260 characters...
In fact, i had to browse all the folders in my shares and for each files of my folders, i had to output to a file the ACE.
Did you get such errors with the length of your path ?

thx,

troll

# re: Creating shares with "backslash-sensitive strings" :(

Thursday, September 02, 2004 10:14 PM by bart

Hi. I'm just learning but, when I run this line:

tlbimp c:\windows\system32\activeds.tlb out:activedsnet.dll

it gives me activeds.dll not activedsnet.dll as you have indicated here. What am I doing wrong>

Thanks.

HardwareGuy

# re: Creating shares with "backslash-sensitive strings" :(

Monday, April 25, 2005 9:46 AM by bart

i need c# .net code for giving folder permissions to a perticular windows user.