测试了几种方式,网上大部分都用的WMI,我复制,这个代码不能成功啦,不知道他们是如何成功的。
WMI方式的源码
private static void QshareFolder(string FolderPath, string ShareName, string Description)
        {
            try
            {
                // Create a ManagementClass object
                ManagementClass managementClass = new ManagementClass("Win32_Share");
                // Create ManagementBaseObjects for in and out parameters
                ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
                ManagementBaseObject outParams;
                // Set the input parameters
                inParams["Description"] = Description;
                inParams["Name"] = ShareName;
                inParams["Path"] = FolderPath;
                inParams["Type"] = 0x0; // Disk Drive
                                //Another Type:
                                //        DISK_DRIVE = 0x0
                                //        PRINT_QUEUE = 0x1
                                //        DEVICE = 0x2
                                //        IPC = 0x3
                                //        DISK_DRIVE_ADMIN = 0x80000000
                                //        PRINT_QUEUE_ADMIN = 0x80000001
                                //        DEVICE_ADMIN = 0x80000002
                                //        IPC_ADMIN = 0x8000003
                //inParams["MaximumAllowed"] = int maxConnectionsNum;
                // Invoke the method on the ManagementClass object
                outParams = managementClass.InvokeMethod("Create", inParams, null);
                // Check to see if the method invocation was successful
                if ((uint) (outParams.Properties["ReturnValue"].Value) != 0)
                {
                    throw new Exception("Unable to share directory.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error!");
            }
        }另外一种有点像C++的,我这个也不成工,贴上这个的源码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Security.AccessControl;
using System.IO;
using System.Runtime.InteropServices;
namespace TestShareFile
{
    class Program
    {
        static void Main(string[] args)
        {
            SetSharing();
        }
        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_SPECIAL = 0x80000000,
        }        private enum SHARE_PERMISSION : uint
        {
            ACCESS_READ = 1,
            ACCESS_WRITE = 2,
        }        [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_re;
            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;
        }        [DllImport("Netapi32.dll ")]
        private static extern int NetShareAdd(
        [MarshalAs(UnmanagedType.LPWStr)]   string strServer,
        Int32 dwLevel,
        ref       SHARE_INFO_502 buf,
        out       uint parm_err);        [DllImport("Netapi32.dll ")]
        private static extern int NetShareDel(
        [MarshalAs(UnmanagedType.LPWStr)]   string strServer,
        [MarshalAs(UnmanagedType.LPWStr)]   string netName,
        Int32 reserved);        public static int SetSharing()
        {
             string   shareName   =   "temp "; 
            string   shareDesc   =   "just   test ";
            string path = @"G:\Ushare";       //       do       not       append       comma,       it 'll       fail       
            safetyAccount(path, "");
            SHARE_INFO_502 info = new SHARE_INFO_502();
            info.shi502_netname = shareName;
            info.shi502_type = SHARE_TYPE.STYPE_DISKTREE;
            info.shi502_re = shareDesc;            info.shi502_permissions = 0;     //       ignored       for       user-level       security       
            info.shi502_max_uses = -1;
            info.shi502_current_uses = 0;                   //       ignored       for       set       
            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;
            return NetShareAdd(null, 502, ref       info, out       error);            // int   reserved   =   0;            // return   NetShareDel(null,   "temp ",   reserved); 
        }
        /// <summary>
        /// 添加共享文件的用户组
        /// </summary>
        /// <param name="_filename"></param>
        /// <param name="account"></param>
        public static void safetyAccount(string filename, string account)
        {
            bool ok = false;
            // string filename = @"C:\DownloadsTemp"; //目标目录
            //string account = @"Everyone";//用户名
            if (account == "") account = @"Everyone";            FileSystemRights rights = new FileSystemRights();
            rights = rights | FileSystemRights.Modify;
            InheritanceFlags iFlags = new InheritanceFlags();
            iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;            DirectoryInfo dInfo = new DirectoryInfo(filename);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            FileSystemAccessRule accessRule = new FileSystemAccessRule(account, FileSystemRights.Modify, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
            bool no = dSecurity.ModifyAccessRule(AccessControlModification.Add, accessRule, out ok);
            dInfo.SetAccessControl(dSecurity);
        }
    }
}真心不懂,为什么一样的代码却不能成功,
麻烦大家帮忙看看。最后是CSDN的广告