【问题标题】:Writing and closing Notepad++ using speech recognition libraries使用语音识别库编写和关闭 Notepad++
【发布时间】: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


【解决方案1】:

您无需打开记事本并写入文件。您可以通过编程方式完成。

// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);

// Open the file to read from.
string readText = File.ReadAllText(path);

【讨论】:

  • 我想通过语音打开文本文件,并通过语音在打开的文本板中写入
【解决方案2】:

您可以使用以下代码关闭记事本:

Process[] processes = Process.GetProcessesByName("notepad");
foreach (var process in processes)
{
    process.Kill();
}

它将关闭所有记事本实例。所以最好确保记事本只是你的(由你的语音识别程序打开)

如果您实际上不想打开记事本并且您的要求只是捕获文本并以编程方式写入文件,请使用以下代码:

// Create a file to write to.
string createText = "Hello World"; // Replace with voice captured text
File.WriteAllText(path, createText);

// Open the file to read from.
string readText = File.ReadAllText(path);

【讨论】:

  • 由于 OP 自己启动流程,因此无需搜索流程实例。如果没有插入文本的部分,这个答案是不完整的(在我看来,这个问题不应该被试图回答,而是应该像广泛的那样封闭)。
  • @Nithish。如果您确定需要打开记事本并输入文本以实际自动模拟人机交互,那么这是一个选项。如果您的要求只是捕获语音文本并写入文件,请参考我的回答以编程方式写入文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
相关资源
最近更新 更多