appendalltext(s1,s2) 將字串為s1的檔附加到s1檔,檔案不存在,會自動建立
appendtext(s) 將s檔名的檔案附到回傳值 ex:
string s = @"c:/temp.txt"
streamwriter sw = file.appendtext(s);
copy(s1,s2) 將檔名為s1的檔案復制到檔名為s2的檔案,注意:s2不可以是已存在的檔
create(s) 建立 檔名路徑為s的檔案,s不可以是已存在的檔案
delete(s) 刪除檔名為s的檔案,s必須要存在
exists(s) 判斷檔名路徑為s的檔案是否存在,回傳bool值
getcreationtime(s) 取得檔名路徑為s的檔案建立時間
getlastaccesstime(s) 取得檔名路徑為s的檔案的上一次被存取的時間
getlastwritetime(s) 取得檔名路徑為s的檔的上一次被寫入的時間
move(s1,s2) 將路徑為s1的檔轉移至s2,:s2不可以是已存在的檔
open(s,filemode1)
readallbytes(s)
readalllines(s,e) e方式打開s
readalltext(s,e)
replace(s1,s2,s3) 將路徑為s1的檔復制到s2並備份到s3
setCreationtime(s,d) 設定名稱為s的檔創立時間為d datetime
setLastAccessTime(s,d) 設定名稱為s的檔最後存儲時間為d datetime
setlastwriteTime(s,d) 設定名稱為s的檔最後寫入時間為d datetime
writeallbytes(s,byte[])
writealllines(s,s[],e)
writealltext(s1,s2) 將字串s2的內容寫入路徑為s1的檔案
檔案刪除 file.delete(@"c:/a.txt")
檔案改名 file.move(@"c:/a.txt",@"c:/b.txt")
目錄(directory)存取
createdirectory(s) 建立路徑為s的資料夾
delete(s)
exists(s) 判斷路徑為s的資料夾是否存在
getcreationtime(s)
getcurrentdirectory(s) 取得目前工作的資料夾
getdircetory(s) 取的s路徑的所有子文件夾,返回數組
getdirectoryroot(s) 取得根目錄
getfiles(s) 取的資料夾中所有文件
getlastaccesstime(s) 取得資料夾最後存取時間
getlastwritetime(s)
getparent(s) 上一層文件夾
move(s1,s2) 路徑為s1的資料夾移動到s2資料夾,使用方法與file的move()方法類似
setcreationtime(s,d) 設定路徑為s1的資料夾建立時間為d
setcurrentdirectory(s) 目前應用程式工作的資料夾為s
setlastaccesstime(s,d) 最後存取時間
setlastwritetime(s,d) 最後寫入時間
//code ex:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace File_and_Directory
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string now_dev = "C://";
string now_dir = "C://";
private void Form1_Load(object sender, EventArgs e)
{
string [] drives = Directory.GetLogicalDrives();
foreach (string s in drives)
{
comboBox1.Items.Add(s);
}
//MessageBox.Show(Directory.Exists("C://").ToString());
}
//當comboBox1選擇變更時觸發
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1)
{
}
else
{
now_dev = comboBox1.SelectedItem.ToString();
treeView1_initail(now_dev);
}
}
//當comboBox1選擇不同磁碟機時必要的 treeView1初始化方法
private void treeView1_initail(string now_dev)
{
treeView1.Nodes.Clear();
try
{
string[] dirs = Directory.GetDirectories(@now_dev);
for (int i = 0; i < dirs.Length; i++)
{
treeView1.Nodes.Add(dirs[i]);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
string[] dirs = Directory.GetDirectories(@e.Node.FullPath.ToString());
if (dirs.Length != 0 && e.Node.Nodes.Count == 0) //判斷此資料夾是否已經開啟過。
{
for (int i = 0; i < dirs.Length; i++)
{
e.Node.Nodes.Add(MyTool.GetDirectoryNameInPath(dirs[i])); //取得目錄名稱並加入目前被選取的node
}
}
else
{
//MessageBox.Show("已經加入節點");
}
now_dir = e.Node.FullPath.ToString();
listView1_refrensh();
//顯示使用者點選的資料夾
MessageBox.Show(e.Node.FullPath.ToString());
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
private void listView1_refrensh()
{
try
{
listView1.Items.Clear();
string[] dirs = Directory.GetDirectories(@now_dir);
string[] fils = Directory.GetFiles(@now_dir);
for (int i = 0; i < dirs.Length; i++)
{
listView1.Items.Add(MyTool.GetDirectoryNameInPath(dirs[i]), 0);
}
for (int i = 0; i < fils.Length; i++)
{
listView1.Items.Add(MyTool.GetDirectoryNameInPath(fils[i]), 1);
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
private void listView1_ItemActivate(object sender, EventArgs e)
{
if (File.Exists(now_dir + "//" + listView1.SelectedItems[0].Text) == false)
{ //代表選擇不是檔案,是目錄
now_dir = now_dir + "//" + listView1.SelectedItems[0].Text;
//**左邊資料夾與右邊資料夾 [同步]選取
treeView1.SelectedNode = treeView1.SelectedNode.Nodes[listView1.SelectedIndices[0]];
listView1_refrensh();
}
else
{
MessageBox.Show("您選擇了檔案:" + listView1.SelectedItems[0].Text);
}
}
private void btn_dirinfo_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count == 0 && treeView1.SelectedNode != null)
{
GetDirInfo(treeView1.SelectedNode.FullPath.ToString());
}
else if (treeView1.SelectedNode != null)
{
GetDirInfo(now_dir + "//" + listView1.SelectedItems[0].Text);
}
else
{
MessageBox.Show("請選擇目錄");
}
}
private void btn_fileinfo_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count == 0)
{
MessageBox.Show("請選擇檔案");
}
else
{
GetFileInfo(now_dir + "//" + listView1.SelectedItems[0].Text);
}
}
/// <summary>
/// 取得資料夾資訊方法
/// </summary>
/// <param name="dir"></param>
void GetDirInfo(string dir)
{
if (File.Exists(dir) == false)
{ //代表選擇不是檔案,是目錄
bool b = Directory.Exists(dir);
Directory.GetCreationTime(dir).ToString();
string temp = "資料夾建立時間:" + Directory.GetCreationTime(dir).ToString()
+"/n" + "資料夾最後讀取時間:" + Directory.GetLastAccessTime(dir).ToString()
+ "/n" + "資料夾最後寫入時間:" + Directory.GetLastWriteTime(dir).ToString()
+ "/n" + "選取資料夾路徑:" + dir;
MessageBox.Show(temp);
listView1.Focus();
}
else
{
MessageBox.Show("您的選擇是檔案不是目錄/n:" + listView1.SelectedItems[0].Text);
listView1.Focus();
}
}
/// <summary>
/// 取得檔案資訊方法
/// </summary>
/// <param name="file"></param>
void GetFileInfo(string file)
{
if (File.Exists(file) == true)
{ //代表選擇不是目錄 ,是檔案
string temp = "檔案建立時間:" + File.GetCreationTime(file).ToString()
+ "/n" + "檔案最後讀取時間:" + File.GetLastAccessTime(file).ToString()
+ "/n" + "檔案最後寫入時間:" + File.GetLastWriteTime(file).ToString()
+ "/n" + "選取檔案路徑:" + file;
MessageBox.Show(temp);
listView1.Focus();
}
else
{
MessageBox.Show("您選擇了目錄不是檔案/n:" + now_dir + "//" + listView1.SelectedItems[0].Text);
listView1.Focus();
}
}
}
}
本文详细介绍了C#中对文件和目录的各种操作,包括创建、删除、复制、移动、读写等基本操作,并展示了如何使用FileStream、StreamReader、StreamWriter等类进行文件操作。此外,还提供了通过Directory类和File类获取文件和目录属性的方法,以及在Windows Forms应用中展示目录结构的示例代码。
1488

被折叠的 条评论
为什么被折叠?



