【发布时间】:2017-11-13 10:13:34
【问题描述】:
我正在尝试编写将打开 Notepad++、写入文件并随后关闭它的代码。我的代码包含在下面。我对 C# 完全陌生。有什么图书馆或方法可以做到这一点吗?
// Button Reference
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text.Equals("Enable Voice Control"))
{
button1.Text = "Stop Voice Control";
recEngine.RecognizeAsync(RecognizeMode.Multiple);
}
else
{
button1.Text = "Enable Voice Control";
recEngine.RecognizeAsyncStop();
}
}
public void Form1_Load(object sender, EventArgs e)
{
Choices commands = new Choices();
commands.Add(myCommands);
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Process cmd = new Process();
cmd.StartInfo.FileName = @"notepad++.exe" ;
//cmd.StartInfo.Arguments =@"\Write.txt";
cmd.Start();
cmd.CloseMainWindow();
cmd.WaitForExit();
cmd.Refresh();
//if (cmd.StandardError != null)
//Console.WriteLine(cmd.StandardError.ReadToEnd());
var result = e.Result;
var i = 0;
foreach (var command in myCommands)
{
if (command.StartsWith("close"))
{
this.Close();
//cmd.StartInfo.FileName = @"notepad++";
cmd.Kill();
}
if (command.StartsWith("--") || command == string.Empty) continue; // Skip commentBlocks and skipEmptylines
var parts = command.Split(new char[] { '|' }); // Split the lines
i++;
if (command.Equals(result.Text))
{
Console.WriteLine("Command is {0}: {1}", i, command);
break;
}
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
【问题讨论】:
-
如果你想写
.txt文件就用File Api写 -
尝试自动化另一个流程是一个非常受欢迎的新手项目。这不是一个好代码,这种代码从来都不是简单的,也不会学到什么。如果您了解如何使用 System.Windows.Automation 命名空间,您将做对。但是考虑继续前进,您首先要编写和控制自己的程序。该项目可以等待。
-
@Hans passnt 感谢您的输入
标签: c# speech-recognition voice-recognition