最近在學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);//彈出消息對話框
}
}
}
}
}