博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ManagementClass("Win32_Share")之共享目录
阅读量:4314 次
发布时间:2019-06-06

本文共 3707 字,大约阅读时间需要 12 分钟。

1   public class ShareFolder 2     { 3         private static readonly Dictionary
ReturnDetails = new Dictionary
(); 4 5 ///
6 /// Success (0) 7 /// Access denied (2) 8 /// Unknown failure (8) 9 /// Invalid name (9)10 /// Invalid level (10)11 /// Invalid parameter (21)12 /// Duplicate share (22)13 /// Redirected path (23)14 /// Unknown device or directory (24)15 /// Net name not found (25)16 /// Other (26–4294967295)17 /// 18 static ShareFolder()19 {20 ReturnDetails.Add(0, "Success");21 ReturnDetails.Add(2, "Access denied");22 ReturnDetails.Add(8, "Unknown failure");23 ReturnDetails.Add(9, "Invalid name");24 ReturnDetails.Add(10, "Invalid level");25 ReturnDetails.Add(21, "Invalid parameter");26 ReturnDetails.Add(22, "Duplicate share");27 ReturnDetails.Add(23, "Redirected path");28 ReturnDetails.Add(24, "Unknown device or directory");29 ReturnDetails.Add(25, "Net name not found");30 }31 32 ///
33 /// 设置文件夹共享 34 /// 35 ///
文件夹路径 36 ///
共享名 37 ///
共享注释38 ///
39 public static void Share(string folderPath, string shareName, string description)40 {41 var directoryInfo = new DirectoryInfo(folderPath);42 DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();43 directorySecurity.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.FullControl,44 InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));45 directoryInfo.SetAccessControl(directorySecurity);46 47 var managementClass = new ManagementClass("Win32_Share");48 // Create ManagementBaseObjects for in and out parameters 49 ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");50 ManagementBaseObject outParams;51 // Set the input parameters 52 inParams["Description"] = description;53 inParams["Name"] = shareName;54 inParams["Path"] = folderPath;55 inParams["Type"] = 0; // Disk Drive 56 inParams["MaximumAllowed"] = null;57 inParams["Password"] = null;58 inParams["Access"] = null; // Make Everyone has full control access.59 outParams = managementClass.InvokeMethod("Create", inParams, null);60 // Check to see if the method invocation was successful 61 uint result = (uint)(outParams.Properties["ReturnValue"].Value);62 63 if (result != 0)64 {65 if (ReturnDetails.ContainsKey(result))66 {67 throw new Exception(ReturnDetails[result]);68 }69 else70 {71 throw new Exception("failed to create share folder");72 }73 }74 }75 }

//测试

1   class Program 2     { 3         static void Main(string[] args) 4         { 5             string folderPath = @"C:\SML\ShareFolder7"; 6  7             //创建文件夹 8             if (!Directory.Exists(folderPath)) 9             {10                 Directory.CreateDirectory(folderPath);11             }12 13             ShareFolder.Share(folderPath, "共享文件7", "共享注释");14 15 16         }17     }

 

转载于:https://www.cnblogs.com/JustYong/p/5726084.html

你可能感兴趣的文章
循环缓冲实现(ring buffer/circular buffer)
查看>>
我说过的那些话
查看>>
matplotlib函数理解
查看>>
java 学习日记---------简易学生信息管理系统
查看>>
jquery 中cache为true与false 的区别
查看>>
Jquery学习之DOM操作
查看>>
linux下ubuntu系统安装及开发环境配置
查看>>
使用混合云,要避开哪些坑?
查看>>
C#文件操作
查看>>
Spring SimpleJdbcTemplate查询示例
查看>>
java设计模式之外观模式(门面模式)
查看>>
经典排序算法
查看>>
给自己的网站加上robots.txt
查看>>
cobbler全自动批量安装部署linux
查看>>
MySQL中Index Merge简介
查看>>
Chrome浏览器扩展开发系列之四:Browser Action类型的Chrome浏览器扩展
查看>>
docker-ce安装
查看>>
前端实现文件的断点续传
查看>>
转:spring4.0之二:@Configuration的使用
查看>>
【Android开发】交互界面布局详解
查看>>