using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Text; using System.Windows.Forms; using System.IO;namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.openFileDialog1.ShowDialog(); } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { StreamReader hc = new StreamReader(this.openFileDialog1.FileName.ToString()); this.richTextBox1.Text = hc.ReadToEnd(); } } } 其他就看你要具体什么样的功能了!
刚才忘说了 StreamReader hc = new StreamReader(this.openFileDialog1.FileName.ToString(),System.Text.Encoding.GetEncoding("GB2312"));//指定下编码,不然会有乱码的!
MOTO-TXT没用过,用手机版office不就行了.
用RichTextBox来实现确实是最简单的方法,不过我自己就不喜欢这样,我是使用Textbox然后使用StreamReader来读取的,贴出我写的读文件方法以及保存文件的方法给楼主参考下吧:先在using那里添加这个: using System.Collections;private void ReadFile(string filepath) { try { FileStream aFile = new FileStream(filepath, FileMode.Open); StreamReader sr = new StreamReader(aFile, Encoding.Default);
没有具体做过,自己去找一下。之后当点击比如打开菜单时,可以弹出一个地址选择器。之后,把选中的路径取给文件读取对像。打开。具本细节自己找。大体思路无非是这样。
做的好的话可以类似于Windows的记事本哦。可以参考用文件流读写。方便好用!
你要实现点怎么样的功能呢?
倒是听说过有一个叫exebook的小东西,可以把好多txt的文档,汇集到一块,相当于一个电子图书的东西!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
StreamReader hc = new StreamReader(this.openFileDialog1.FileName.ToString());
this.richTextBox1.Text = hc.ReadToEnd();
}
}
}
其他就看你要具体什么样的功能了!
StreamReader hc = new StreamReader(this.openFileDialog1.FileName.ToString(),System.Text.Encoding.GetEncoding("GB2312"));//指定下编码,不然会有乱码的!
MOTO-TXT没用过,用手机版office不就行了.
using System.Collections;private void ReadFile(string filepath)
{
try
{
FileStream aFile = new FileStream(filepath, FileMode.Open);
StreamReader sr = new StreamReader(aFile, Encoding.Default);
ArrayList arrText = new ArrayList(); /*while (sr.Peek() != -1)
{
string strLine = sr.ReadLine();
arrText.Add(strLine);
}*/
arrText.Add(sr.ReadToEnd()); //作用跟上面的注释语句基本等效,不过我觉得这个更好。 string[] arrString = new string[arrText.Count];
arrText.CopyTo(arrString, 0);
txtTextArea.Lines = arrString;
sr.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "打开文件失败!");
}
} private void SaveFile(string filepath)
{
FileStream aFile = new FileStream(filepath, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile, Encoding.Default); try
{
foreach (string oneLine in txtTextArea.Lines)
sw.WriteLine(oneLine); //这段注释代码跟上面的foreach语句作用等效
/*int i;
for (i = 0; i < txtTextArea.Lines.Length - 1; i++)
{
sw.WriteLine(txtTextArea.Lines[i]);
}*/ sw.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "保存文件出错!");
}
}
using System.Collections;private void ReadFile(string filepath)
{
try
{
FileStream aFile = new FileStream(filepath, FileMode.Open);
StreamReader sr = new StreamReader(aFile, Encoding.Default);
ArrayList arrText = new ArrayList(); /*while (sr.Peek() != -1)
{
string strLine = sr.ReadLine();
arrText.Add(strLine);
}*/
arrText.Add(sr.ReadToEnd()); //作用跟上面的注释语句基本等效,不过我觉得这个更好。 string[] arrString = new string[arrText.Count];
arrText.CopyTo(arrString, 0);
txtTextArea.Lines = arrString;
sr.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "打开文件失败!");
}
} private void SaveFile(string filepath)
{
FileStream aFile = new FileStream(filepath, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile, Encoding.Default); try
{
foreach (string oneLine in txtTextArea.Lines)
sw.WriteLine(oneLine); //这段注释代码跟上面的foreach语句作用等效
/*int i;
for (i = 0; i < txtTextArea.Lines.Length - 1; i++)
{
sw.WriteLine(txtTextArea.Lines[i]);
}*/ sw.Close();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "保存文件出错!");
}
}
119654848#qq.com #替换成@
嘛,算了,发发牢骚而已。可以当我没说过,“分”这些就算了,只是让我觉得楼主你有点“是非不分”而已。
我用过易和C#,大体的流程是:添加可显示文本控件->可用流根据用户选择路径打开文本文件->可添加读取书签->字体颜色和文本控件的背景色设置。
RichTextBox1,Button1需要引用命名空间:
using System.IO思路:
OpenFileDiailog ofd = new OpenFileDiailog(); //实例化OpenFileDiailog
ofd.ShowDailog(); //向用户弹出选择文件对话框
if(ofd.fileName!=""){ //如果用户选择了文本文件路径RichTextBox1.Text=
File.ReadAllText(ofd.fileName,Encoding.Default); //通过用户选择的文本文件路径,用默认编码读取文本数据,并用RichTextBox1接受数据
}
如果楼主还有疑问,请mail:[email protected] 或加我QQ,乐意为你解答疑问。
C#这门语言还是有前途和钱途的,希望你能继续努力。