標籤:
一、實驗目的:
1. 熟悉.NET架構下路徑及檔案資訊的擷取方法。
2. 熟悉文字檔、二進位流式檔案的讀取和儲存方法。
二、實驗內容及要求:
1、實現選擇一個路徑,並顯示該路徑下的所有目錄及檔案。
2、編程實現顯示選中檔案的屬性資訊(大小、最後訪問時間、最後修改時間等資訊)
3、建立應用程式,實現在表單關閉後,下次開啟時能自動儲存表單的大小和位置(用文字檔儲存)。
4、繼續完善資料庫操作類,實現:將資料庫連接資訊(伺服器IP地址、資料庫名)用文字檔配置,在資料庫操作類中提供方法從設定檔中讀入伺服器IP地址、資料庫名,實現連接字串的動態產生。
5、編程實現,將教師資訊:教師號(Int16),教師名(string),性別(bool),基本工資(double),寫入二進位檔案儲存。同時實現從二進位檔案中讀出寫入內容。
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;namespace CJ11103060203{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); string curDir = ""; string subDir = ""; string curFile = ""; try { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { curDir = fbd.SelectedPath; string[] surDirs = Directory.GetDirectories(curDir); string[] Files = Directory.GetFiles(curDir); for (int i = 0; i < subDir.Length; i++) { subDir = surDirs[i]; subDir = subDir.Substring(subDir.LastIndexOf("\\") + 1); listBox1.Items.Add(subDir); } for (int k = 0; k < Files.Length; k++) { curFile = Files[k]; curFile = curFile.Substring(curFile.LastIndexOf("\\") + 1); listBox2.Items.Add(curFile); } } } finally { } } private void button2_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { FileInfo file = new FileInfo(ofd.FileName); MessageBox.Show("檔案名稱: " + file.Name + "\n" + "大小: " + file.Length.ToString() + "\n" + "最後的訪問時間: " + file.LastAccessTime.ToString() + "\n" + "最後的修改時間: " + file.LastWriteTime.ToString() + "\n" + "所在目錄: " + file.DirectoryName); } } }}後台對檔案的操作
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;namespace _2CJ11103060203{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { int x, y, w, h; x = this.Left; y = this.Top; w = this.Width; h = this.Height; StreamWriter sw = File.CreateText(Application.StartupPath + "\\config.txt"); sw.WriteLine("x=" + x.ToString()); sw.WriteLine("y=" + y.ToString()); sw.WriteLine("w=" + w.ToString()); sw.WriteLine("h=" + h.ToString()); sw.Flush(); sw.Close(); sw.Dispose(); } private void Form1_Load(object sender, EventArgs e) { int x, y, w, h; string ss = ""; StreamReader sr = File.OpenText(Application.StartupPath + "\\config.txt"); ss = sr.ReadLine(); x = Convert.ToInt16(ss.Substring(2)); ss = sr.ReadLine(); y = Convert.ToInt16(ss.Substring(2)); ss = sr.ReadLine(); w = Convert.ToInt16(ss.Substring(2)); ss = sr.ReadLine(); h = Convert.ToInt16(ss.Substring(2)); ss = sr.ReadLine(); this.Left = x; this.Top = y; this.Width = w; this.Height = h; sr.Close(); sr.Dispose(); } }}
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;namespace _3CJ11103060203{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } struct teacher { public Int16 jsh; public string jsm; public bool xb; public double gz; } private void button1_Click(object sender, EventArgs e) { teacher t; t.jsh = Convert.ToInt16(textBox1.Text); t.jsm = textBox2.Text; if (radioButton1.Checked) t.xb = true; else t.xb = false; t.gz = Convert.ToDouble(textBox3.Text); SaveFileDialog sf = new SaveFileDialog(); if (sf.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(sf.FileName, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write((Int16)t.jsh); bw.Write((string)t.jsm); bw.Write((bool)t.xb); bw.Write((double)t.gz); bw.Flush(); fs.Close(); } } private void button2_Click(object sender, EventArgs e) { teacher t; OpenFileDialog od = new OpenFileDialog(); if (od.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(od.FileName, FileMode.Open); BinaryReader br = new BinaryReader(fs); t.jsh = br.ReadInt16(); t.jsm = br.ReadString(); t.xb = br.ReadBoolean(); t.gz = br.ReadDouble(); textBox1.Text = t.jsh.ToString(); textBox2.Text = t.jsm; if (t.xb) radioButton1.Checked = true; else radioButton2.Checked = true; textBox3.Text = t.gz.ToString(); fs.Close(); } } }}View Code
C#對檔案的操作