簡體   English   中英

使用c#或cmd與所有權限共享文件夾

[英]Share folder with c# or cmd with all permissions

我需要共享一個具有讀寫權限的文件夾。

在Windows 7中,我可以通過執行以下操作來共享它:

凈共享temp = A:\\ MySharedFolder / GRANT:每個人都已滿

如果我在Windows XP中執行相同的代碼,將無法正常工作。 但是我可以執行:

凈共享溫度= C:\\ MySharedFolder

但不會以完全權限共享該文件夾。


編輯

JotaBe發布了一個鏈接,並從該鏈接構建了一個似乎有效的以下程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {

        [DllImport("Netapi32.dll")]
        private static extern uint NetShareAdd(
            [MarshalAs(UnmanagedType.LPWStr)] string strServer,
            Int32 dwLevel,
            ref SHARE_INFO_502 buf,
            out uint parm_err
        );

        private enum NetError : uint
        {
            NERR_Success = 0,
            NERR_BASE = 2100,
            NERR_UnknownDevDir = (NERR_BASE + 16),
            NERR_DuplicateShare = (NERR_BASE + 18),
            NERR_BufTooSmall = (NERR_BASE + 23),
        }

        private enum SHARE_TYPE : uint
        {
            STYPE_DISKTREE = 0,
            STYPE_PRINTQ = 1,
            STYPE_DEVICE = 2,
            STYPE_IPC = 3,
            STYPE_TEMPORARY = 0x40000000,
            STYPE_SPECIAL = 0x80000000,
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SHARE_INFO_502
        {
            [MarshalAs(UnmanagedType.LPWStr)]
            public string shi502_netname;
            public SHARE_TYPE shi502_type;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string shi502_remark;
            public Int32 shi502_permissions;
            public Int32 shi502_max_uses;
            public Int32 shi502_current_uses;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string shi502_path;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string shi502_passwd;
            public Int32 shi502_reserved;
            public IntPtr shi502_security_descriptor;
        }


        static void Main(string[] args)
        {

            string shareName = "testshare";
            string shareDesc = "This is a test share kekelar2000";
            string path = @"C:\MyShareDirectory"; // do not append comma, it'll fail

            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;   
            info.shi502_max_uses = -1;
            info.shi502_current_uses = 0;    
            info.shi502_path = path;
            info.shi502_passwd = null;       
            info.shi502_reserved = 0;
            info.shi502_security_descriptor = IntPtr.Zero;

            uint error = 0;

            uint result = NetShareAdd("192.168.0.120", 502, ref info, out error);
        }
    }
}

用這個:

netshareadd(netapi32)

您必須以管理員身份運行程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM