最近在學(xué)arduino,對(duì)一些關(guān)鍵字,基本的結(jié)構(gòu)屬性用法語句掌握不是很清楚。手頭沒有實(shí)物書本查找,用word翻看介于頁碼頗多,沒有很強(qiáng)的針對(duì)性。想著能夠通過章節(jié)索引的方式查詢相關(guān)語句。靈感一到,如下產(chǎn)生。
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#學(xué)習(xí)\"+e.Node.FullPath;//AfterSelect事件獲取選定節(jié)點(diǎn)
//textbox2顯示物理路徑;;;PS:我用node.fullpath屬性得到的路徑不是絕對(duì)路徑,大家可以用別的法子比如System.IO.path.GetFileName();理論上能實(shí)現(xiàn)
}
private void Form1_Load(object sender, EventArgs e)
{
PaintTreeView(treeView1, @"C:\Users\Mr.wang\Desktop\c#學(xué)習(xí)");
}
//定義PaintTreeView函數(shù)得到程序所在路徑下的所有文件夾和文件夾的子文件
private void PaintTreeView(TreeView treeview,string fullpath)
{
try
{
treeview.Nodes.Clear();
DirectoryInfo dirs=new DirectoryInfo(fullpath);//獲得程序所在路徑的目錄對(duì)象
DirectoryInfo[]dir=dirs.GetDirectories();//獲得目錄下屬的文件夾
int dicount =dir.Count();//記錄文件夾的個(gè)數(shù)
//for循環(huán)獲得每個(gè)文件夾和文件夾里的所有文件
for(int i=0;i
{
TreeNode node=new TreeNode();//treeview的節(jié)點(diǎn)對(duì)象
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出錯(cuò)的位置為:Form1.PaintTreeView()函數(shù)");
}
}
//
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text != null)
{
string conn = string.Format(//創(chuàng)建連接字符串
@"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);//彈出消息對(duì)話框
}
}
}
}
}