C#写入文本文件(在我的项目中实现代码) [英] C# write to textfile (implement the code in my project)
本文介绍了C#写入文本文件(在我的项目中实现代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下一个代码在C#的控制台应用程序中显示接收到的midi内容.
如何更改它以将midi内容立即写入文本文件(附加到文本文件)?
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)?
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();
}
}
}
推荐答案
尝试,
Try,
File.AppendAllText(strPath, strFileContent);
使用System.IO名称空间中的Stream writer写入文本文件.
有关更多详细信息,请参见以下链接.
http://msdn.microsoft.com /en-us/library/system.io.streamwriter.aspx [ ^ ]
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[^]
这篇关于C#写入文本文件(在我的项目中实现代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文