在c#中怎么打开已经存在的文件?
解决方案 »
- UDP 通信问题,,开启线程接收后,再切换到一问一答程序就挂了
- .net开发的exe文件加壳
- 请教高手,我想取一个物料编码如 01.001.001 最后一个点号前面的部分 01.001 ,谢谢
- 什麼樣更改dataset的連接
- Unix的compress/uncompress兼容的压缩/解压算法(非SharpZipLib/ZLIB/GZipStream/java.util.zip)
- 我有一段 JavaScript 不知道那位老大能帮我看看,
- C# 在一个事件里面触发另一个事件
- Combobox显示不同字段和图片
- 如何使程序在任务栏上闪烁?
- 急,,高手请如何配置外部网站?·
- sqladapter.insertcommand()问题
- 请问谁能给我一个C#socket编程视频或者有系统,能看懂的文字资料。在此大恩不言谢!
//读取bin目录下的test.txt文件
using (FileStream fs = File.Open("test.txt", FileMode.OpenOrCreate))
{
using (StreamReader sr = new StreamReader(fs))
{
while (sr.Read() > 0)
{
Console.WriteLine(sr.ReadLine());
}
}
}
你可以利用文件流来实现它 如下示例:class FileReadDemo
{
public static void Main()
{
string path;
Console.WriteLine (
"输入要读取的文件名。指定带路径的完整名称:");
path = Console.ReadLine ();
try
{
if (!File.Exists(path))
{
Console.WriteLine("文件不存在");
}
else
{
// 打开流以进行读取。 //创建一个 byte 数组以读取数据
byte[] arr = new byte[100];
UTF8Encoding data = new UTF8Encoding(true);
//继续读文件直到读取文件中的所有数据
while (fs.Read(arr,0,arr.Length) > 0)
{
Console.WriteLine(data.GetString(arr));
}
}
}
catch(Exception ex)
{
Console.WriteLine(“发生错误:" + ex.Message);
}
}
}
System.Diagnostics.Process.Start(path);
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}