如题,需要注意脚本的编码应该是ascii(阿里的centos8上是这样)。代码:using System;using System.Diagnostics;using System.IO;using System.Text;namespace mylinux{class Program{static void Main(string...

如题,需要注意脚本的编码应该是ascii(阿里的centos8上是这样)。
代码:
using System; using System.Diagnostics; using System.IO; using System.Text; namespace mylinux { class Program { static void Main(string[] args) { setCommand(); doSh(); } static void setCommand() { StreamWriter sw = new StreamWriter("test.sh", false, Encoding.ASCII); string t = ""; sw.WriteLine("#!/bin/bash"); Console.WriteLine("请输入Linux脚本命令(exit退出):"); t = Console.ReadLine(); while(t.ToLower()!="exit") { sw.WriteLine(t); Console.WriteLine("请输入Linux脚本命令(exit退出):"); t = Console.ReadLine(); } sw.Close(); } static void doSh() { Process process = new Process(); process.StartInfo.FileName = "sh"; process.StartInfo.Arguments = "test.sh"; process.Start(); process.WaitForExit(); process.Close(); } } }
运行结果:
织梦狗教程
本文标题为:c#利用脚本,本地执行linux命令


基础教程推荐
猜你喜欢
- .netcore 3.1中使用swagger显示注释 2023-09-27
- Unity使用物理引擎实现多旋翼无人机的模拟飞行 2023-04-14
- C#中WPF颜色对话框控件的实现 2023-05-25
- Unity实现打砖块游戏 2023-06-07
- C#字体池技术实现代码详解 2023-02-03
- c# – 数据库模式已更改 2023-11-09
- C#字节数组(byte[])和字符串相互转换方式 2023-07-18
- 如何使用C#轻松运行Shell命令? 2023-11-12
- 如何在WXP(和更新的MSWindows)上使用C#终止所有[grand]子进程 2023-09-20
- WPF实现多运算符表达式计算器 2023-03-14