Multimobile Development: Building Applications for any Smartphone
Network Shares
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Mike Hunsberger
GOOD ANSWER
Hello,

I have been searching to no avail to find a way to
share a remote directory under C#. I can do it with
scripts but could someone tell me how to share a drive in
C#? Thank you so much. I am at a roadblock.

Mike
Reply to this message...
Vote that this is a GOOD answer... (6 votes from other users already)
 
 
    
Nicholas Paldino [.NET MVP]
GOOD ANSWER
Mike,

If you share it in script, then you can easily take the COM dll that you
use and import it into .NET. If you want, you can also use the Win32 API
functions to add a share.

Which would you prefer?

--
- Nicholas Paldino [.NET MVP]
- Click here to reveal e-mail address

"Mike Hunsberger" <Click here to reveal e-mail address> wrote in message
news:6ba601c1bb28$dc41f2c0$3aef2ecf@TKMSFTNGXA09...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Mike Hunsberger
GOOD ANSWER
Either one. Could you give examples of how to do this?
Thanks a million.

Mike
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Willy Denoyette [MVP] (VIP)
GOOD ANSWER
If you just want to establishes a connection between the local computer and a remote network share, simply use Process.Start to
issue a net use command.

using System.Diagnostics;
....
string device = "X:"; // local device mapping
string uncpath = @\\server\share;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = String.Format("/c net use {0} {1}", device, uncpath);
psi.WindowStyle = ProcessWindowStyle.Minimized;
Process proc = Process.Start(psi);
proc.WaitForExit();
if(proc.ExitCode != 0)
....
.....

Willy.

"Mike Hunsberger" <Click here to reveal e-mail address> wrote in message news:6e3301c1bb3b$2e809d90$36ef2ecf@tkmsftngxa12...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Mike Hunsberger
GOOD ANSWER
Hello. Thanks, but what I need is to create a share. I
create a folder on a remote machine and then want to share
that folder and add share permissions to it.
Mike
[Original message clipped]

Process.Start to
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Willy Denoyette [MVP] (VIP)
GOOD ANSWER
No problem just change the command into "net share ....":

.....
net share pipo=c:\pipo
string sharename= "TEMP"; // Sharename
string uncpath = @"c:\tmp"; // share c:\tmp as TEMP
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = String.Format("/c net share {0}={1}", sharename, uncpath);

Take a look in the help for Net share for extra options.

Another possibility is to use the Management classes (WMI wrappers) or PInvoke.

Willy.

"Mike Hunsberger" <Click here to reveal e-mail address> wrote in message news:767201c1bbac$9c48a4a0$9ee62ecf@tkmsftngxa05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Mike H
GOOD ANSWER
Thank you very much. I will try this.

Mike H
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Willy Denoyette [MVP] (VIP)
GOOD ANSWER
Here's how to achieve the same using Management namespace classes.

using System;
using System.Management;

public class Wmis {
public static void Main() {
string sharename = "TEMP";
UInt32 ret = Create("Test", 10, sharename, "passwordhere", @"c:\tmp");
Console.WriteLine("Creation returned: " + ret);
ManagementObject share = new ManagementObject("Win32_Share=" + "'" + sharename +"'");
string s = (string)share.Properties["Description"].Value; // read property "description"
Console.ReadLine(); // press enter to delete share
ret = Delete(share);
Console.WriteLine("Deletion returned: " + ret);
}
public static UInt32 Create(string Description, UInt32 MaximumAllowed, string Name, string Password, string Path)
{
ManagementBaseObject inParams = null;
ManagementClass classObj = new System.Management.ManagementClass(null, "Win32_Share", null);
inParams = classObj.GetMethodParameters("Create");
inParams["Access"] = null; // default Security descriptor used
inParams["Description"] = Description;
inParams["MaximumAllowed"] = MaximumAllowed;
inParams["Name"] = Name;
inParams["Password"] = Password;
inParams["Path"] = Path;
inParams["Type"] = 0; // Disk drive 0, print queue 1
ManagementBaseObject outParams = classObj.InvokeMethod("Create", inParams, null);
return Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
}

public static UInt32 Delete(ManagementObject share)
{
ManagementBaseObject inParams = null;
ManagementBaseObject outParams = share.InvokeMethod("Delete", inParams, null);
return System.Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
}
}

Willy.

"Mike H" <Click here to reveal e-mail address> wrote in message news:76ed01c1bbb1$38cae0f0$19ef2ecf@tkmsftngxa01...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Mike H
GOOD ANSWER
Thank you Willy! This is exactly what I was looking for.
With my limited programming knowledge it still took me
some time to get working, but it now does what I need.

Thanks.......
[Original message clipped]

null);
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (4 votes from other users already)
 
 
    
David
GOOD ANSWER
Hi Willy,
how can I share an existing folder on a remote machine, for example my server?
I tried: UInt32 ret = Create("Test", 10, sharename, "passwordhere", @"\\\\SERVER\\C$\\tmp");

--------------------------------
From: David
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Ranjeet Sharma
GOOD ANSWER
Hi All,

How can i get the permissions of a folder inside a shared folder on the remote system using system.management.

Any Ideas!!!!
Please help.

Thanks in advance,

Ranjeet Sharma

--------------------------------
From: Ranjeet Sharma
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Steven M. Cohn
GOOD ANSWER
I have an easier way to accomplish the same thing by referencing the Windows
Scripting Host Network object (WshNetwork)... while it's technically not
pure C# since you're importing a COM object, it's cheaper than creating a
new process to run a command - and it's only three lines of code :-)

- First, right-click on the References folder in your project.
- Click the COM tab.
- Find the item named "Windows Script Host Object Model"
- Click Select
- Click OK.
- A new reference named IWshRuntimeLibrary should appear in the
References folder.

Now you have a reference to the WSH dll in your project and can create code
such as:

using IWshRuntimeLibrary;
...
WshNetwork net = new WshNetwork();
net.MapNetworkDrive("X:", @"\\machine\share", false, "username",
"password");
That's it!

\Steven

"Mike Hunsberger" <Click here to reveal e-mail address> wrote in message
news:6ba601c1bb28$dc41f2c0$3aef2ecf@TKMSFTNGXA09...

[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Mike Hunsberger
GOOD ANSWER
Wow, That is cool. I will look into that too. Thanks.
Mike H
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
David
GOOD ANSWER
Did anybody try the command UInt32 ret = Create("Test", 10, sharename, "passwordhere", @"c:\tmp") for a remote machine? I'm trying to share a folder on my server but it's not working:
UInt32 ret = Create("Test", 10, sharename, "passwordhere", @"\\SERVER\tmp")

--------------------------------
From: David
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
David Dussart
GOOD ANSWER
Hi,
could anybody help me to share a folder on a remote machine (server).
UInt32 ret = Create("Test", 10, sharename, "passwordhere", @"\\SERVER\C$\tmp") is not working.

--------------------------------
From: David
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Console
System.Convert
System.Diagnostics.Process
System.Diagnostics.ProcessStartInfo
System.Diagnostics.ProcessWindowStyle
System.Management.ManagementBaseObject
System.Management.ManagementClass
System.Management.ManagementObject
System.String
System.UInt32




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734