ffmpeg.StartInfo.CreateNoWindow = true; // 不显示程序窗口 ffmpeg.Start(); var errorreader = ffmpeg.StandardError; ffmpeg.WaitForExit(); var result = errorreader.ReadToEnd();
using Process ffmpeg = new Process(); ffmpeg.StartInfo.UseShellExecute = false; ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; ffmpeg.StartInfo.RedirectStandardError = true; ffmpeg.StartInfo.FileName = ffmpegpath; ffmpeg.StartInfo.Arguments = "-i " + sourceFile;
ffmpeg.StartInfo.CreateNoWindow = true; // 不显示程序窗口 ffmpeg.Start(); var errorreader = ffmpeg.StandardError; ffmpeg.WaitForExit(); var result = errorreader.ReadToEnd();
object obj = "hello"; if (obj isstring s && s.Length > 0) { Console.WriteLine($"obj是一个非空字符串: {s}"); }
属性模式匹配
1 2 3 4 5 6 7 8 9
publicclassPerson { publicstring Name { get; set; } publicint Age { get; set; } }
Person person = new Person { Name = "John", Age = 30 }; if (person is { Name: "John" }) { Console.WriteLine("这个人的名字是John"); }
类型和属性匹配
1 2 3 4 5 6 7
privatevoidBtnQuestionsClick(object sender, RoutedEventArgs e) { if (!(sender is Button { Tag: TiwenTongjiItem tongjiItem })) { return; } }
集合模式匹配
1 2 3 4
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; if (numbers is { Count: > 0 }) { Console.WriteLine("集合非空"); }
复杂一点
1 2 3 4 5
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; if (numbers is { Count: 5, [2]: 3 }) { Console.WriteLine("集合中有5个元素,且第三个元素是3"); }
合并模式匹配
1 2 3 4
int x = 5; if (x is >= 0and <= 10) { Console.WriteLine("x是介于0和10之间的数"); }
switch模式匹配
示例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14
object obj = 5;
switch (obj) { case int i: Console.WriteLine($"It's an integer: {i}"); break; case string s: Console.WriteLine($"It's a string: {s}"); break; default: Console.WriteLine("Unknown type"); break; }