对文件夹添加共享属性!

看到一段代码,谁写的忘了。请作者多多包含。

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,
  }

  [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;
  }

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

  public uint SetDirectoryShare(string path, string shareName, string shareDesc)
  {
   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;
   return NetShareAdd(null, 502, ref info, out error);
  }

第二种方法
using System.IO;
using System.Management;

private void CreateDirtory()
  {
   try
   {
    // create a directory
    Directory.CreateDirectory(@"C:\Inetpub\wwwroot\Temp");
    // 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"] = "My Files Share";
    inParams["Name"] = "My Files Share";
    inParams["Path"] = @"C:\Inetpub\wwwroot\Temp";
    inParams["Type"] = 0x0; // Disk Drive
    // 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 e)
   {
    MessageBox.Show(e.Message.ToString());
   }
  }

转载于:https://www.cnblogs.com/mullr2005/archive/2005/10/18/257223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值