系统->高级->环境变量->用户变量
SET PATH=C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319;%PATH%
运行 cmd
编译 csc.exe /t:exe speak4.cs
执行 speak4.exe hello 我是广东人
speak4.cs 如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Speak4
{
class Program
{
static void Main(string[] args)
{
try
{
Type spVoice = Type.GetTypeFromProgID("SAPI.SpVoice");
dynamic sp = Activator.CreateInstance(spVoice);
dynamic en = sp.GetVoices("Language = 409; Gender = Female", ""); //English
dynamic cn = sp.GetVoices("Language = 804", ""); // 中文
Regex re = new Regex("^[A-Za-z]+$");
for (int i = 0; i < args.Length; i++)
{
if (re.IsMatch(args[i]))
{
sp.Voice = en.Item(0);
}
else if (cn.Count > 0)
{
sp.Voice = cn.Item(0);
}
sp.Speak(args[i]);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}