Programmatically Share a Folder?

BadgerByte

Well-known member
Joined
Aug 5, 2005
Messages
194
Location
Ipswich, UK
Programming Experience
5-10
Hey,

I've done a lot of googling on this one and come up pretty blank.

Does anyone know how to Share a folder using VB.NET.

Thanks in advance,

Andy
 
Well, i don't see a reason why you Suspect Win API. It is designed by C/C++ programs and is the most direct way to interact with a Windows system for apps.
However, as i mention above i will try to find an adequate API func for sharing folder/s and will let you know about the way how it has been done as i'm also interested ... :D
I think i have worked on something very close to this but at the moment i cannot recall where and why :eek:

Regards ;)
 
Hi Badger,
Well, i'm not sure that this is the right thing ... the one that you are looking for but however you can take a look of ... namely i find out this code that creates shared folder/directory (credit is not mine) :D

VB.NET:
[size=2][color=#0000ff]Function[/color][/size][size=2] CreateShare([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Desc [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sharename [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] path [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] server [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String

[/color][/size][size=2][/size][size=2][color=#008000]'Creates the specified share 

[/color][/size][size=2][/size][size=2][color=#008000]'Return Values 

[/color][/size][size=2][/size][size=2][color=#008000]'0 = Success 

[/color][/size][size=2][/size][size=2][color=#008000]'anything else = Failed 

[/color][/size][size=2][/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] scope [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = ("\\" & server & "\root\cimv2")

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] classObj [/size][size=2][color=#0000ff]As[/color][/size][size=2] ManagementClass = [/size][size=2][color=#0000ff]New[/color][/size][size=2] ManagementClass(scope, "Win32_Share", [/size][size=2][color=#0000ff]Nothing[/color][/size][size=2])

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] mbo [/size][size=2][color=#0000ff]As[/color][/size][size=2] ManagementBaseObject

mbo = classObj.GetMethodParameters("Create")

mbo("Access") = [/size][size=2][color=#0000ff]Nothing

[/color][/size][size=2]mbo("Description") = Desc

mbo("Name") = sharename

mbo("Path") = path

mbo("Type") = 0

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] outParams [/size][size=2][color=#0000ff]As[/color][/size][size=2] ManagementBaseObject = classObj.InvokeMethod("Create", mbo, [/size][size=2][color=#0000ff]Nothing[/color][/size][size=2])

[/size][size=2][color=#0000ff]Return[/color][/size][size=2] outParams.Properties("ReturnValue").Value.ToString

[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception

MessageBox.Show(ex.ToString)

[/size][size=2][color=#0000ff]Return[/color][/size][size=2] 1

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Function[/color][/size]
don't foget to to add a reference to System.Management for your project and also put:
Imports System.Management ... at the top of your class

HTH,
Regards ;)
 
Kulrom, Kulrom, Kulrom,

My Dearest Kulrom,
Kulrom the great,
Kulrom for King!!!

Thank you so much for digging this up, I care not where the credit lies, I haven't been able to find a shred.

Many thanks again Sir.
 
You are wellcome ... btw, you know the meaning of Sir ???
Well i suppose it means: Stupid Idiot Rascle :D
tuca.gif
 
Ok I'm miserable again now!

This is very weird, I've tried your example, and numerous other variations I've found (It's much easier to find code once you know which classes etc you need!). The folder does get shared, but I can't open the share from any other machine on the network, I just get "access is denied". This is all a bit beyond me. I realise this isn't an easy one but if you've managed to get this to work I'd be forever greatful.

Many Thanks
 
It certainly is rather difficult. I don't pretend to be a programming genius (by any stretch of the imagination!) but I can usually figure things out from an example... In this case however the examples I have found for setting security just leave me wanting to bite my own feet off.

For instance the example here:

http://www.dotnet247.com/247reference/msgs/51/259195.aspx

I've read through it several hundred times, and I'm already chewing my big toe!
If there's one thing I can't bare, it's trying to use code that I don't understand.

Once again, thank you for your help here Kulrom.

*If only I worked on the top floor of a tall office block, I could simply jump*
 
Hey Kulrom,

Thanks for your persistence! I've had a go with that one, but it has the same problems.
It seems that it's near impossible to add a share using WMI if the user has "simple file sharing" enabled. XP home doesn't let you turn this off, and it's on by default in professional. So for now, I've abandoned the idea of sharing the folder programatically. Why's nothing ever simple!!!!!!

Thanks again, you're still King Kulrom!!
 
Back
Top