十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決

站在用戶的角度思考問題,與客戶深入溝通,找到南靖網(wǎng)站設(shè)計與南靖網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋南靖地區(qū)。
Flile類的常用方法
| 序號 | 方法 | 說明 |
| 1 | Exists(string Path) | 用于檢查指定文件是否存在,該方法返回一個布爾值 |
| 2 | Copy(string SourceFilePath,string DestinationFilePath) | 將指定路徑的源文件中的內(nèi)容復(fù)制到目標文件中,如果目標文件不存在,則在指定路徑中新建一個文件 |
| 3 | Move(string sourceFileName,string destFileName) | 將指定文件移到一個新的路徑 |
| 4 | Delete(string path) | 刪除指定的文件,如果指定的文件不存在,則不引發(fā)異常 |
Directory類的常用方法
| 序號 | 方法 | 說明 |
| 1 | Exists(string path) | 用于堅持指定的文件夾在磁盤上是否存在 |
| 2 | Move(string sourceDirName,string DestDirName) | 用于將文件或目錄及其內(nèi)容移到新位置 |
| 3 | Delete(string,bool) | 刪除指定目錄,如果bool值為true,則刪除子目錄中的所有目錄內(nèi)容 |
例:
代碼:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "全部文件 *.*|*.*";
openFileDialog1.FileName = "全部文件";
openFileDialog1.ShowDialog();
this.textBox1.Text = openFileDialog1.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "全部文件 *.*|*.*";
openFileDialog1.FileName = "全部文件";
openFileDialog1.ShowDialog();
this.textBox2.Text = openFileDialog1.FileName;
}
//復(fù)制文件
private void button3_Click(object sender, EventArgs e)
{
if (!File.Exists(this.textBox1.Text))
{
MessageBox.Show("文件不存在");
}
else
{
File.Copy(this.textBox1.Text, this.textBox2.Text);
MessageBox.Show("拷貝成功");
}
}
//移動文件
private void button4_Click(object sender, EventArgs e)
{
if (!File.Exists(this.textBox1.Text))
{
MessageBox.Show("文件不存在");
}
else
{
File.Move(this.textBox1.Text, this.textBox2.Text);
MessageBox.Show("移動成功");
}
}
//刪除文件
private void button5_Click(object sender, EventArgs e)
{
if (!File.Exists(this.textBox1.Text))
{
MessageBox.Show("文件不存在");
}
else
{
File.Delete(this.textBox1.Text);
MessageBox.Show("刪除成功");
}
}
FileInfo類的屬性和方法
| 屬性 | 說明 |
| Exists | 用于檢查指定文件是否存在,返回一個bool值 |
| Extension | 獲取表示文件擴展命名部分的字符串 |
| Name | 獲取文件名 |
| FullName | 獲取目錄或文件的完整目錄 |
| 方法 | 說明 |
| CopyTo(string) | 將現(xiàn)有文件復(fù)制到新文件,不允許覆蓋現(xiàn)有文件 |
| Delete() | 永久刪除文件 |
| MoveTo(string) | 將指定文件移到新位置(string) |
例:
DirectoryInfo di = new DirectoryInfo("D:\testDir");
//返回當前目錄的子目錄
DirectoryInfo[] subDir = di.GetDirectories();
//返回當前目錄的文件列表
FileInfo[] fi = di.GetFiles();
步驟:
1.引入命名空間:using System.Runtime.Serialization.Formatters.Binary;
2.在SavingInfo、Remind等類的頭部加一個標記[Serializable],例如:
[Serializable]
Public class SavingInfo
{
//..............
}
3.編寫Save()方法和Load()方法,例如:
//序列化方法
public void Save()
{
//定義文件流
FileStream fs = new FileStream(@"files\save.bin", FileMode.Create);
//二進制方式
BinaryFormatter bf = new BinaryFormatter();
//序列化存儲對象
bf.Serialize(this.listArrays);
//關(guān)閉文件流
fs.Close();
}
//反序列化方法
public void Load()
{
//省略判斷文件是否存在
FileStream fs = new FileStream(@"files\save.bin",FileMode.Open);
BinaryFormatter bf=new BinaryFormatter();
//反序列化
this.lisArrays = (SavingInfo)bf.Deserialize(fs);
fs.Close();
}
注:Deserialize()方法將存儲介質(zhì)的數(shù)據(jù)文件流轉(zhuǎn)換為object類型。
不想序列化的屬性在其頭部加上[NonSerialized]標記即可。