Click here to Skip to main content
15,914,409 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
The next code displays received midi content in a console application in C#.
How do I change it that it writes the midi content instantly to a textfile (append to it)?

C#
using System;
using Midi;
using System.Threading;
using System.Collections.Generic;

namespace MidiExamples
{    
    public class Example05 : ExampleBase
    {
        public Example05()
            : base("Example05.cs", "Prints notes and chords as they are played.")
        { }

        public class Summarizer
        {
            public Summarizer(InputDevice inputDevice)
            {
                this.inputDevice = inputDevice;
                pitchesPressed = new Dictionary<Pitch, bool>();
                inputDevice.NoteOn += new InputDevice.NoteOnHandler(this.NoteOn);
                inputDevice.NoteOff += new InputDevice.NoteOffHandler(this.NoteOff);
                PrintStatus();
            }

            private void PrintStatus()
            {
                Console.Clear();
                Console.WriteLine("Play notes and chords on the MIDI input device, and watch");
                Console.WriteLine("their names printed here.  Press any QUERTY key to quit.");
                Console.WriteLine();

                // Print the currently pressed notes.
                List<Pitch> pitches = new List<Pitch>(pitchesPressed.Keys);
                pitches.Sort();
                Console.Write("Notes: ");
                for (int i = 0; i < pitches.Count; ++i)
                {
                    Pitch pitch = pitches[i];
                    if (i > 0)
                    {
                        Console.Write(", ");
                    }
                    Console.Write("{0}", pitch.NotePreferringSharps());
                    if (pitch.NotePreferringSharps() != pitch.NotePreferringFlats())
                    {
                        Console.Write(" or {0}", pitch.NotePreferringFlats());
                    }
                }
                Console.WriteLine();
                // Print the currently held down chord.
                List<Chord> chords = Chord.FindMatchingChords(pitches);
                Console.Write("Chords: ");
                for (int i = 0; i < chords.Count; ++i)
                {
                    Chord chord = chords[i];
                    if (i > 0)
                    {
                        Console.Write(", ");
                    }
                    Console.Write("{0}", chord);
                }
                Console.WriteLine(); 
            }

            public void NoteOn(NoteOnMessage msg)
            {
                lock (this)
                {
                    pitchesPressed[msg.Pitch] = true;
                    PrintStatus();
                }
            }

            public void NoteOff(NoteOffMessage msg)
            {
                lock (this)
                {
                    pitchesPressed.Remove(msg.Pitch);
                    PrintStatus();
                }
            }

            private InputDevice inputDevice;
            private Dictionary<Pitch, bool> pitchesPressed;
        }

        public override void Run()
        {
            // Prompt user to choose an input device (or if there is only one, use that one).
            InputDevice inputDevice = ExampleUtil.ChooseInputDeviceFromConsole();
            if (inputDevice == null)
            {
                Console.WriteLine("No input devices, so can't run this example.");
                ExampleUtil.PressAnyKeyToContinue();
                return;
            }
            inputDevice.Open();
            inputDevice.StartReceiving(null);

            Summarizer summarizer = new Summarizer(inputDevice);
            ExampleUtil.PressAnyKeyToContinue();
            inputDevice.StopReceiving();
            inputDevice.Close();
            inputDevice.RemoveAllEventHandlers();
        }
    }
}
Posted
Updated 29-Oct-12 1:20am
v2

Try,

C#
File.AppendAllText(strPath, strFileContent);
 
Share this answer
 
v2
Use Stream writer from System.IO namespace to write to text file.
for more details refer following link.
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900