解决方案 »
- DataGridView的数据源是XML文档,更新其内容时如何更新XML文档???急!!!
- 关于类Environment使用方法,
- 查询数据的条数
- 在使用DataGridView控件时,大家遇到过这样的情况吗(见帖图)
- 在Vista x64系统中用C#调用C++编写的win32 DLL出现"找不到指定的模块"的错误
- 一个人忙不过来,C#程序找人兼职帮完成一点东西
- c#中如何写有传回值的sqlcommand
- Access数据库如何批量操作呀 ?
- 利用installshiled生成的setup,xp机器为什么运行不了?
- 我安装了framework sdk,但是没有见到什么 命令提示副,那怎么弄?
- Web控件如何屏蔽网页弹窗
- 如何使用C#根据数据库连续产生EXCEL?
private void button1_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
StreamReader SReader = new StreamReader(textBox1.Text, Encoding.Default);
textBox2.Text = SReader.ReadToEnd();
}
catch { MessageBox.Show("请选择文件"); }
}
How to: Write Text to a File
http://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.110).aspxusing System;
using System.IO;class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx
using System;
using System.IO;class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}How to: Write Text to a File
http://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.110).aspx
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;class Program
{
static void Main(string[] args)
{
// Set a variable to the My Documents path.
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
StringBuilder sb = new StringBuilder(); // Enumerate the files in the My Documents path, filtering for text files only.
foreach (string txtName in Directory.EnumerateFiles(mydocpath,"*.txt"))
{
// Open a stream reader and write the name of the file, a visual separator,
// and the contents of the file to the stream.
using (StreamReader sr = new StreamReader(txtName))
{
sb.AppendLine(txtName.ToString());
sb.AppendLine("= = = = = =");
sb.Append(sr.ReadToEnd());
sb.AppendLine();
sb.AppendLine();
}
}
// Write the stream contents to a new file named "AllTxtFiles.txt".
using (StreamWriter outfile = new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}
}
}