Windows 服务启动共享目录
private void FormMain_Load(object sender, EventArgs e) { ShareDirec(); } public void ShareDirec() { ManagementClass management =new ManagementClass("Win32_share"); ManagementBaseObject inParams = management.GetMethodParameters("Create"); ManagementBaseObject outParams; string strPath = @"Z:\"; string strName = "网络磁盘Z"; inParams["Name"] = strName; inParams["Path"] = strPath; inParams["Type"] = 0x0; outParams = management.InvokeMethod("Create", inParams, null); if ((uint)(outParams.Properties["ReturnValue"].Value) != 0) { txtLog.AppendText(string.Format("【{0}】共享失败", strPath)); return; } else { string strLog = string.Format("已将路径【{0}】设置为共享目录,共享名称为【{1}", strPath, strName); txtLog.AppendText(strLog); } }