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();
                    }
                }
            }
  • 相关阅读:
    linux环境下安装redis扩展
    LINUX环境下SVN安装与配置(利用钩子同步开发环境与测试环境)
    Linux环境下网卡配置
    MAC 下虚拟主机的配置
    从json_encode过来的的字符串被返回到html页面时的解析
    for循环绑定事件,闭包思想!
    js8月-4号,,思想
    三种添加事件的方式
    smarty第一天
    5秒钟后自动跳转!!!!
  • 原文地址:https://www.cnblogs.com/qianj/p/12808477.html
Copyright © 2011-2022 走看看