开发者

Execute a line in a text file

I have a program that reads text files filled with code designed to be executed line by line by the program, like a batch file. The problem is that I don't no how to do the line executing part. Here is my code, I thought using the \r would fool the console. But it just shows me a list of lines in the file.

if (tok[0] == "read" && length == 2)
{
  try
  {
    StreamReader tr = new StreamReader(@"C:\Users\Public\"+tok[1]+".txt");
    while (!tr.EndOfStream)
    {
      Console.WriteLine(tr.ReadLine());
    }
  }
  catch
  {
    Console.WriteLine("No such text file.\n");
  }
  Prompt();
}

If I knew what to search for to fix my开发者_StackOverflow problem in Google, I would have. But I've got no idea.

Thanks

EDIT - My program is a crude synthesizer. It takes inputs in the form of 440 5, or 415 2. The first number is frequency, the second duration. What I'm wanting to do is read text files, which my code does, and execute the sound info line by line, which it doesn't, hence my question. It works perfectly fine from standard input.


Audio synthesis is not straightforward, there used to be

Console.Beep(frequency,duration);

but that's using the PC speaker most systems don't have anymore - here's an example though using DirectSound to achieve something close to what you want.

To read the frequency and duration from your text file you can use something like this (splitting on space):

StreamReader tr = new StreamReader(@"test.txt");
while(!tr.EndOfStream)
{
    string[] parts = tr.ReadLine().Split(new[]{' '});
    int frequency = Convert.ToInt32(parts[0]);
    int duration = Convert.ToInt32(parts[1]);
}


You should load your code and compile it in the runtime.

Check out following examples:

  • http://www.csharpfriends.com/articles/getarticle.aspx?articleid=118
  • http://www.codeproject.com/KB/dotnet/evaluator.aspx

EDIT:

You should use Process.Start(cmd); to execute commands in the shell. Here I've found few nice examples: http://dotnetperls.com/process-start


If your program works fine using Standard Input just pass the text file to it like this:

yourprogram.exe < textFile.txt

Then the contents of the text file will be passed to your program on Standard Input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜