侧边栏壁纸
博主头像
怪客のBlog 博主等级

行动起来,活在当下

  • 累计撰写 35 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录
C#

C# 执行Linux命令及文件授权

怪客
2023-01-18 / 0 评论 / 0 点赞 / 248 阅读 / 0 字

执行命令

      #region Linux下执行命令
        /// <summary>
        /// Linux下执行命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static string RunLinuxCmd(string cmd)
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo("/bin/bash", "")
            };
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            process.StandardInput.WriteLine(cmd);
            process.StandardInput.Close();
            var result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Dispose();
            return result;
          
        }
        #endregion

文件授权

        #region Linux文件授权
        /// <summary>
        /// Linux文件授权
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="chodType">权限类型</param>
        public static void chmod(string file, chodType chodType)
        {
            switch (chodType)
            {
                case chodType._0777:
                    const int _0777 =
          S_IRUSR | S_IXUSR | S_IWUSR
          | S_IRGRP | S_IXGRP | S_IWGRP
          | S_IROTH | S_IXOTH | S_IWOTH;
                    chmod(Path.GetFullPath(file), _0777);
                    break;
                case chodType._0755:
                    const int _0755 =
          S_IRUSR | S_IXUSR | S_IWUSR
          | S_IRGRP | S_IXGRP
          | S_IROTH | S_IXOTH;
                    chmod(Path.GetFullPath(file), _0755);
                    break;
                case chodType._0644:
                    const int _0644 =
       S_IRUSR | S_IWUSR
       | S_IRGRP
       | S_IROTH;
                    chmod(Path.GetFullPath(file), _0644);
                    break;
                case chodType._0600:
                    const int _0600 = S_IRUSR | S_IWUSR;
                    chmod(Path.GetFullPath(file), _0600);
                    break;
                default:
                    break;
            }
        }
        #endregion
        
            #region Linux文件权限枚举
            /// <summary>
            /// Linux文件权限枚举
            /// </summary>
            public enum chodType
            {
                /// <summary>
                /// 777
                /// </summary>
                _0777,
                /// <summary>
                /// 755
                /// </summary>
                _0755,
                /// <summary>
                /// 644
                /// </summary>
                _0644,
                /// <summary>
                /// 600
                /// </summary>
                _0600

            }
            #endregion
        ```
0

评论区