zoukankan      html  css  js  c++  java
  • 设置文件夹共享及所有访问权限

    直接贴代码:

       public class FileHelper
        {
            /// <summary>
            /// 设置文件夹共享
            /// </summary>
            /// <param name="FolderPath">文件夹路径</param>
            /// <param name="ShareName">共享名</param>
            /// <param name="Description">共享注释</param>
            /// <returns></returns>
            public static int ShareNetFolder(string folderPath, string shareName, string description)
            {
                try
                {
                    ManagementClass managementClass = new ManagementClass("Win32_Share");
                    ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
                    ManagementBaseObject outParams;
                    inParams["Description"] = description;
                    inParams["Name"] = shareName;
                    inParams["Path"] = folderPath;
                    inParams["Type"] = 0x0; // Disk Drive
                    outParams = managementClass.InvokeMethod("Create", inParams, null);
                    if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
                    {
                        Common.WriteLog("Unable to share directory");
                    }
                    //设置文件夹所有访问权限
                    SetFolderAllRight(folderPath, "Everyone", "FullControl");
                }
                catch (Exception ex)
                {
                    Common.WriteLog(ex.Message + ":" + ex.StackTrace);
                    return -1;
                }
                return 0;
            }
            /// <summary>
            /// 取消文件夹共享
            /// </summary>
            /// <param name="ShareName">文件夹的共享名</param>
            /// <returns></returns>
            public static int CancelShareNetFolder(string ShareName)
            {
                try
                {
                    SelectQuery selectQuery = new SelectQuery("Select * from Win32_Share Where Name = '" + ShareName + "'");
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
                    foreach (ManagementObject mo in searcher.Get())
                    {
                        mo.InvokeMethod("Delete", null, null);
                    }
                }
                catch
                {
                    return -1;
                }
                return 0;
            }
            /// <summary>
            /// 设置文件夹所有访问权限
            /// </summary>
            /// <param name="path">文件夹路径</param>
            /// <param name="rightName">权限名称</param>
            /// <param name="power">权限等级</param>
            public static void SetFolderAllRight(string path, string rightName, string power)
            {
                try
                {
                    // 获取文件夹信息
                    DirectoryInfo dirinfo = new DirectoryInfo(path);
                    if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
                    {
                        dirinfo.Attributes = FileAttributes.Normal;
                    }
                    //获得该文件夹的所有访问权限
                    DirectorySecurity dirsecurity = dirinfo.GetAccessControl(AccessControlSections.All);
                    //设定文件ACL继承
                    InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
                    //添加访问权限规则 完全控制权限
                    FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule(rightName, FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
                    bool isModified = false;
                    dirsecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);
                    //设置访问权限
                    dirinfo.SetAccessControl(dirsecurity);
                }
                catch (Exception ex)
                {
                    Common.WriteLog(ex.Message + ":" + ex.StackTrace);
                }
            }

     写日志方法:

            /// <summary>
            /// 写日志
            /// </summary>
            /// <param name="msg"></param>
            public static void WriteLog(string msg)
            {
                string filePath = GetRootDirectory() + "Log";
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string logPath = filePath + "\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                try
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("消息:" + msg);
                        sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sw.WriteLine("**************************************************");
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
                catch (IOException e)
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("异常:" + e.Message);
                        sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
                        sw.WriteLine("**************************************************");
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }
  • 相关阅读:
    解决VS控制台窗口自动关闭问题
    ZOJ1003:Crashing Balloon(dfs)
    POJ2607:Fire Station(SPFA+枚举)
    C语言在屏幕上输出玫瑰花图片
    HRBUST
    UVA10182: Bee Maja (模拟)
    洛谷P1144: 最短路计数(bfs)
    (转载)MySQL LIKE 用法:搜索匹配字段中的指定内容
    (转载)[MySQL技巧]INSERT INTO… ON DUPLICATE KEY UPDATE
    (转载)INSERT INTO .. ON DUPLICATE KEY 语法与实例教程
  • 原文地址:https://www.cnblogs.com/qianj/p/12808477.html
Copyright © 2011-2022 走看看