views:

179

answers:

2

I am trying to figure out how to accomplish the following task:

  1. Create a network share on a remote computer using .NET
  2. The target computer is on the same network.
  3. The target computer's admin username and password are known.

Your thoughts? How would you go about to accomplish this?

A: 

http://www.google.ca/search?hl=en&q=create+share+net suggests that you'll be using PInvoke to invoke the NetShareAdd API.

I was wondering whether an alternative might be to invoke the NET SHARE command-line command, however looking at NET HELP SHARE I don't see how to use it to create a share on a remote computer.

ChrisW
+2  A: 

The best and easiest method to do Windows Administrative tasks from code is through WMI. Almost anything can be done on a Windows machine, local or remote, through WMI.

In order to administer a Windows shared folder you will need to interact with the Win32_Share WMI class.

Sample on how to use WMI to create a shared folder:

    ManagementPath path = new ManagementPath("Win32_Share");
    path.Server = ".";              // Change this to your server

    object[] parameters = new object[] {
        "C:\\TestShare",            // Path to shared folder
        "Test Share",               // Share name
        0x0,                        // Share type (disk drive)
        5,                          // Maximum amount of concurrent users
        null,                       // Password (optional)
        null                        // Security level (optional)
    };

    ManagementClass share = new ManagementClass(path);
    object result = share.InvokeMethod("Create", parameters);

You will need to add a reference to the System.Management assembly in order to access the WMI classes. You'll also need access to the remote machine of course.

Check out the WMI Reference for more details. Specifically check out the Win32_Share Class reference on what all the parameters and return codes mean.

Y Low