110,536
社区成员
发帖
与我相关
我的任务
分享
string path = "C:\\360safebox";
string[] fileInfo = Directory.GetFiles( path, "*.dat", SearchOption.AllDirectories );
for( int i = 0 ; i < fileInfo.Length-1 ; i++ ) {
textBox1.AppendText( fileInfo[ i ] + Environment.NewLine );
}
string filename = "c:\\a.txt";
using( FileStream fs = new FileStream( filename, FileMode.Open ) ) {
byte[] con = new byte[ fs.Length ];
fs.Read( con, 0, (int)fs.Length );
textBox1.AppendText( Encoding.Unicode.GetString( con ) );
}
//这个是个窗体类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace TextReader
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
string _fileFolder="";
public string fileFolder
{
get
{
return _fileFolder;
}
}
bool ishavePage = false;
string _fileName = "";
public string fileName
{
get
{
return _fileName;
}
}
string _fileFullName = "";
public string fileFullName
{
get
{
return _fileFullName;
}
}
List<string> fileFullNameList = new List<string>();
private void btnAddNext_Click(object sender, EventArgs e)
{
bool added = false;
if (fileFullNameList==null||fileFullNameList.Count==0)
{
DirectoryInfo di = new DirectoryInfo(_fileFolder);
if (!di.Exists)
{
return;
}
FileInfo[] fi = di.GetFiles();
foreach (FileInfo file in fi)
{
string t= file.FullName;
string[] tlist= t.Split('.');
if (tlist[tlist.Length-1].ToLower()=="txt")
{
fileFullNameList.Add(file.FullName);
}
}
}
for (int i = 0; i < fileFullNameList.Count; i++)
{
if (added)
{
return;
}
string[] temp = fileFullNameList[i].Split('\\');
string tempFileName = temp[temp.Length - 1];
bool isfileOpened = false;
for (int j = 0; j < tabControl1.TabPages.Count; j++)
{
if (tabControl1.TabPages[j].Text==tempFileName)
{
isfileOpened = true;
break;
}
}
if (!isfileOpened)
{
textBox t = new textBox();
t.setText(readTxtFile(fileFullNameList[i], ""));
tabControl1.TabPages.Add(tempFileName);
if (tabControl1.TabPages.Count != 0)
{
for (int k = 0; k < tabControl1.TabPages.Count; k++)
{
if (tabControl1.TabPages[k].Text == tempFileName)
{
// tabControl1.TabPages[k].Focus();
tabControl1.SelectTab(tabControl1.TabPages[k]);
tabControl1.TabPages[k].Controls.Add(t);
tabControl1.TabPages[k].Controls[0].Dock = DockStyle.Fill;
added = true;
}
}
}
}
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
_fileFullName = openFileDialog1.FileName;
string[] temp = fileFullName.Split('\\');
_fileName = temp[temp.Length - 1];
string temstr=_fileFullName;
_fileFolder = temstr.Remove(_fileFullName.Length - _fileName.Length-1);
}
textBox t = new textBox();
t.setText(readTxtFile(_fileFullName, ""));
if (!ishavePage)
{
TabPage page = new TabPage();
page.Text = fileName;
tabControl1.TabPages.Add(page);
if (tabControl1.TabPages.Count!=0)
{
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
if (tabControl1.TabPages[i].Text==fileName)
{
tabControl1.TabPages[i].Controls.Add(t);
tabControl1.TabPages[i].Controls[0].Dock = DockStyle.Fill;
}
}
}
ishavePage = true;
}
else
{
tabControl1.TabPages.Add(fileName);
if (tabControl1.TabPages.Count != 0)
{
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
if (tabControl1.TabPages[i].Text == fileName)
{
tabControl1.TabPages[i].Controls.Add(t);
tabControl1.TabPages[i].Controls[0].Dock = DockStyle.Fill;
}
}
}
}
}
public ArrayList readTxtFile(string path, string fgf)
{
if (!File.Exists(path))
{
Console.WriteLine("文件不存在!");
Console.WriteLine("按回车键退出!");
Console.ReadLine();
return null;
}
try
{
StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));
string l;
ArrayList content = new ArrayList();
while ((l = sr.ReadLine()) != null)
{
content.Add(l);
}
sr.Close();
content.TrimToSize();
return content;
}
catch (IOException ex)
{
Console.WriteLine("读文件出错!请检查文件是否正确。");
Console.WriteLine(ex.ToString());
return null;
}
}
}
}
//这个是才窗体中控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace TextReader
{
public partial class textBox : UserControl
{
public textBox()
{
InitializeComponent();
}
public string fileName="";
public string fileFolder = "";
private void toolStripButton1_Click(object sender, EventArgs e)
{
}
public void setText(ArrayList s)
{
for (int i = 0; i < s.Count; i++)
{
if (this.boxReport.Text.Length==0)
{
boxReport.Text += s[i].ToString();
}
else
{
boxReport.Text += Environment.NewLine + s[i].ToString();
}
}
}
private void textBox_DragDrop(object sender, DragEventArgs e)
{
}
}
}