最近在学arduino,对一些关键字,基本的结构属性用法语句掌握不是很清楚。手头没有实物书本查找,用word翻看介于页码颇多,没有很强的针对性。想着能够通过章节索引的方式查询相关语句。灵感一到,如下产生。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
namespace Arduino_guidancebook
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox2.Text =@"C:\Users\Mr.wang\Desktop\c#学习\"+e.Node.FullPath;//AfterSelect事件获取选定节点
//textbox2显示物理路径;;;PS:我用node.fullpath属性得到的路径不是绝对路径,大家可以用别的法子比如System.IO.path.GetFileName();理论上能实现
}
private void Form1_Load(object sender, EventArgs e)
{
PaintTreeView(treeView1, @"C:\Users\Mr.wang\Desktop\c#学习");
}
//定义PaintTreeView函数得到程序所在路径下的所有文件夹和文件夹的子文件
private void PaintTreeView(TreeView treeview,string fullpath)
{
try
{
treeview.Nodes.Clear();
DirectoryInfo dirs=new DirectoryInfo(fullpath);//获得程序所在路径的目录对象
DirectoryInfo[]dir=dirs.GetDirectories();//获得目录下属的文件夹
int dicount =dir.Count();//记录文件夹的个数
//for循环获得每个文件夹和文件夹里的所有文件
for(int i=0;i
{
TreeNode node=new TreeNode();//treeview的节点对象
node.Text=dir[i].Name;
treeview.Nodes.Add(node);
FileInfo[]file=dir[i].GetFiles();
int filecount=file.Count();
for(int j=0;j
{
TreeNode node1 =new TreeNode();
node1.Text=file[j].Name;
node.Nodes.Add(node1);
}
//string pathNade=fullpath+"\\"+dir[i].Name;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message+"\r\n出错的位置为:Form1.PaintTreeView()函数");
}
}
//
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text != null)
{
string conn = string.Format(//创建连接字符串
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=text",
textBox2.Text.Substring(0, textBox2.Text.LastIndexOf(@"\"))
);
OleDbConnection openconnect=new OleDbConnection(conn);
try
{
openconnect.Open();
OleDbCommand cmd = new OleDbCommand(
string.Format("select*from{0}",
textBox2.Text.Substring(textBox2.Text.LastIndexOf(@"\"),
textBox2.Text.Length - textBox2.Text.LastIndexOf(@"\"))
), openconnect);
OleDbDataReader oda = cmd.ExecuteReader();
while (oda.Read())
{
listBox1.Text += oda[0].ToString();//得到文本文件中的字符串
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);//弹出消息对话框
}
}
}
}
}