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
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
| |
| Mike Hunsberger |
| GOOD ANSWER |
Either one. Could you give examples of how to do this? Thanks a million.
Mike [Original message clipped]
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
| |
| Mike H |
| GOOD ANSWER |
Thank you very much. I will try this.
Mike H [Original message clipped]
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
| |
| 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]
|
|
|
| |
|
|
| |
|
| |
| 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
|
|
|
| |
|
|
| |
|
|
| |
| 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
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
| |
| 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]
|
|
|
| |
|
|
| |
| |
| Mike Hunsberger |
| GOOD ANSWER |
Wow, That is cool. I will look into that too. Thanks. Mike H [Original message clipped]
|
|
|
| |
|
|
| |
| |
| 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
|
|
|
| |
|
|
| |
|
| |
| 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
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|