简单的办法,用StreamReader打开文件,读出内容,末尾加上要追加的字符窜,再用StreamWriter写入文件。
解决方案 »
- 连接数据库的时候出现错误 ConnectionString 中未指定 OLE DB 提供程序
- 我自定义一个类,在哪里声明类对象,可以在窗体各个事件里使用该对象?谢谢
- win7 x64下flash的843端口telnet 不通
- C#写的应用程序不能以共享方式运行?一直奇怪也没有解决
- 新手求教
- 初学问题:如何去理解C#中的委托和事件呢??
- 关于报表数据源的问题,请高手指明一条出路,希望后天上班的时候解决掉
- 图片绘到窗体问题。
- 请教C#中的System.Drawing.Icon类对象怎么存文件名?
- C# Xpath路徑法 datagridview
- 这段javascript怎么写,急
- 求一正则表达式,"输入密码只能是 0-9 或 a-z 不能是符号" ,谢谢!!
=new System.IO.FileInfo("c:\\1.txt");
System.IO.FileStream mStream
= mFile.Open(System.IO.FileMode.Append,
System.IO.FileAccess.ReadWrite);
.......
{
//初始化FileStream方法一
//FileStream fs=new FileStream("",FileMode.Create,FileAccess.Write,FileShare.None);
//初始化FileStream方法二
FileInfo theFile=new FileInfo(filePath);
FileStream fs=theFile.OpenWrite();
//从流中写入一个字节
// int nextbyte=100;
// fs.WriteByte((byte)nextbyte);
//从流中写入特定数量的字节数。
string strTemp="hello,china";
byte[] byteArray=System.Text.Encoding.ASCII.GetBytes(strTemp);
fs.Write(byteArray,0,byteArray.Length);
fs.Close();
}
public void WriteFileByStreamReader(string filePath)
{
//初始化streamreader
StreamWriter sw=new StreamWriter(filePath);
//写入一行
sw.WriteLine("nihao哈暗黑!");
//写入一个字符
sw.Write('a');
sw.Close();
}
Imports System.IOPublic Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter ' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
sw = File.CreateText(path) sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
End If ' This text is always added, making the file longer over time
' if it is not deleted.
sw = File.AppendText(path)
sw.WriteLine("This")
sw.WriteLine("is Extra")
sw.WriteLine("Text")
sw.Flush()
sw.Close() ' Open the file to read from.
Dim sr As StreamReader = File.OpenText(path)
Dim s As String
Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLine(s)
Loop
sr.Close()
End Sub
End Class
System.IO.FileStream s= f.Open(System.IO.FileMode.Append, System.IO.FileAccess.ReadWrite);
主要是上面的append
主要是个append
不过好像有个类直接有appendtext方法
查查msdn
sw.Write(t.Text);
sw.Close();这样就行了.
那第二个参数为 append 如果文件存在,而且 append 为真时,则向文件中追加.
如果 append 为假,则改写文件.