Beispiel #1
0
        /// <summary>
        /// 共享某个文件夹
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>生成的共享名(目前实现是取目录名,会有重名问题)</returns>
        public string ShareFolder(
            string path)
        {
            if (!Directory.Exists(path))
            {
                throw new Exception("目录不存在");
            }
            if (IsShared(path))
            {
                throw new Exception("路径已被共享");
            }

            SHARE_INFO_502 info = new SHARE_INFO_502();

            info.shi502_netname             = Path.GetFileNameWithoutExtension(path);
            info.shi502_type                = (uint)SHARE_TYPE.STYPE_DISKTREE;
            info.shi502_remark              = "";
            info.shi502_permissions         = (int)SHARE_PERMISSIONS.ACCESS_ALL;
            info.shi502_max_uses            = -1;
            info.shi502_current_uses        = 0;
            info.shi502_path                = path;
            info.shi502_passwd              = IntPtr.Zero;
            info.shi502_reserved            = 0;
            info.shi502_security_descriptor = IntPtr.Zero;
            uint err;
            var  result = netapi32.NetShareAdd(null, 502, ref info, out err);

            if (result != 0)
            {
                string errMsg = null;
                if (Enum.IsDefined(typeof(NetError), result))
                {
                    errMsg = "。描述:" + ((NetError)result).ToString();
                }
                throw new Exception(String.Format("共享失败,错误码:{0},{1}{2}", result, err, errMsg));
            }
            SharedFoldersChanged(this, EventArgs.Empty);
            return(info.shi502_netname);
        }
Beispiel #2
0
 public static extern uint NetShareAdd(
     [MarshalAs(UnmanagedType.LPWStr)] string strServer,
     Int32 dwLevel,
     ref SHARE_INFO_502 buf,
     out uint parm_err
     );