Jeevika
Jeevika

Reputation: 165

How to share and unshare windows folder using c#

I have to share a folder to a user and unshare it programmatically using c#. I am able to share a folder using InvokeMethod of Win32_Share class.

            int IsShared = 0;
            ManagementClass mc = new ManagementClass("Win32_Share");

            object[] methodargs = { folderPath, shareName, "0" };

            object result = mc.InvokeMethod("Create", methodargs);


            if ((uint)result != 0)
            {
                IsShared = 1;
                return IsShared;
            }
            else
                return IsShared;

But how to do it for a particular user?

Also please let me know how to unshare it? Win32_Share class has delete() method.But I am unable to unshare using it.

Upvotes: 0

Views: 1674

Answers (2)

Maybe not the best approach, but I ended up calling a command line silently and it worked for me:

To share:

        var folderName = "your_shared_folder_name";
        var targetDir = "your_folders_target_path";
        var process = new Process();

        process.StartInfo = new ProcessStartInfo()
        {
            UseShellExecute = false,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            ErrorDialog = false,
            WindowStyle = ProcessWindowStyle.Hidden,
            FileName = "cmd.exe",
            Arguments = $"/C net share {folderName}=\"{targetDir}\" /Grant:Everyone,READ"
        };

        process.Start();
        process.WaitForExit();

Notice the /Grant:Everyone,READ. This is what I wanted, but you might wanna fiddle with this part a little bit.

To delete:

    var folderName = "your_shared_folder_name";
    var process = new Process();
    process.StartInfo = new ProcessStartInfo()
    {
        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        ErrorDialog = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = $"/C net share \"{folderName}\" /delete"
    };

    process.Start();
    process.WaitForExit();

Upvotes: 1

Alessio Calicetti
Alessio Calicetti

Reputation: 1

Check the following

 private void shareDir(string p)
    {
        string shareName = "testshare";
        string shareDesc = "This is a test share kekelar2000";
        string path = p;

        SHARE_INFO_502 info = new SHARE_INFO_502();
        info.shi502_netname = shareName;
        info.shi502_type = SHARE_TYPE.STYPE_DISKTREE;
        info.shi502_remark = shareDesc;
        info.shi502_permissions = 0;    // ignored for user-level security
        info.shi502_max_uses = 1;
        info.shi502_current_uses = 1;
        info.shi502_path = path;
        info.shi502_passwd = null;        // ignored for user-level security
        info.shi502_reserved = 0;
        info.shi502_security_descriptor = IntPtr.Zero;

        uint error = 0;
        uint result = NetShareAdd(Dns.GetHostName(), 502, ref info, out error);
    }

Upvotes: 0

Related Questions