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 NsFileOperate
{
public partial class FileOperate : Form
{
public FileOperate()
{
InitializeComponent();
}
/// <summary>
/// 方法名稱:btnOpenFile_Click
/// 方法作用:打開文件方法一
/// 作者:心語
/// 完成時間:2010年5月25日19:49:29
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenFile_Click(object sender, EventArgs e)
{
//設置標題
this.openFileDialog1.Title = "打開文件";
//設置默認路徑
this.openFileDialog1.InitialDirectory = "d:\\";
//設置文件類型
this.openFileDialog1.Filter = "心語文件*.dxl|*.dxl|文本文件*.txt|*.txt|所有文件|*.*";
//設置顯示文件類型
this.openFileDialog1.FilterIndex = 1;
//關閉對話框時是否還原當前目錄
this.openFileDialog1.RestoreDirectory = true;
//獲取或設置默認文件擴展名
this.openFileDialog1.DefaultExt = ".txt";
//判斷是否選擇了打開按鈕
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
//獲得文件路徑
string path = this.openFileDialog1.FileName;//注意位置
//創建文件流
FileStream file = new FileStream(path, FileMode.Open);
//創建讀取器
StreamReader reader = new StreamReader(file);
//顯示讀取內容
this.txtFileName.Text = reader.ReadToEnd();
}
}
/// <summary>
/// 方法名稱:btnOpenFile2_Click
/// 方法作用:打開文件方法二
/// 作者:心語
/// 完成時間:2010年5月25日19:55:31
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenFile2_Click(object sender, EventArgs e)
{
//創建打開對話框對象
OpenFileDialog open = new OpenFileDialog();
//默認路徑
open.InitialDirectory = "d:\\";
//文本格式篩選
open.Filter = "心語文件*.dxl|*.dxl|文本文件*.txt|*.txt|所有文件|*.*";
//設置顯示文件類型
open.FilterIndex = 1;
//關閉對話框時是否還原當前目錄
open.RestoreDirectory = true;
//調用打開對話框方法
if (open.ShowDialog() == DialogResult.OK)
{
//取得文件路徑
string path = open.FileName;
//創建文件流
FileStream filestream = new FileStream(path, FileMode.Open);
//創建byte數組
byte[] bt = new byte[filestream.Length];
//調用read讀取方法
filestream.Read(bt, 0, bt.Length);
//以Unicode編碼方式顯示文本
this.txtFileName.Text = Encoding.Unicode.GetString(bt);
}
}
/// <summary>
/// 方法名稱:btnSaveFile_Click
/// 方法作用:保存文件
/// 作者:心語
/// 完成時間:2010年5月25日20:01:48
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSaveFile_Click(object sender, EventArgs e)
{
//創建保存對話框對象
SaveFileDialog save = new SaveFileDialog();
//默認路徑
save.InitialDirectory = "d:\\";
//文本格式篩選
save.Filter = "心語文件*.dxl|*.dxl|文本文件*.txt|*.txt";
//設置顯示文件類型
save.FilterIndex = 1;
//關閉對話框時是否還原當前目錄
save.RestoreDirectory = true;
//調用保存對話框方法
if (save.ShowDialog() == DialogResult.OK)
{
//取得文件路徑
string path = save.FileName;
//設置默認文件擴展名
save.DefaultExt = ".txt";
//創建寫入器對象
StreamWriter sw = new StreamWriter(path);
//調用寫入方法
sw.WriteLine(txtFileName.Text);
//關閉寫入器
sw.Close();
}
}
/// <summary>
/// 方法名稱:btnCopy_Click
/// 方法作用:複製文件
/// 作者:心語
/// 完成時間:2010年5月25日20:05:11
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCopy_Click(object sender, EventArgs e)
{
//檢查是否一存在文件
if (File.Exists("D:\\CopyToFile\\Copy.txt") == true)
{
MessageBox.Show("目標文件夾已有此文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
//複製
File.Copy("D:\\Copy.txt", "D:\\CopyToFile\\Copy.txt");
MessageBox.Show("複製成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
/// <summary>
/// 方法名稱:btnMove_Click
/// 方法作用:移動文件
/// 作者:心語
/// 完成時間:2010年5月25日20:11:22
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnMove_Click(object sender, EventArgs e)
{
if (File.Exists("D:\\MoveToFile\\Move.txt") == true)
{
MessageBox.Show("目標文件夾已有此文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
//移動
File.Move("D:\\Move.txt", "D:\\MoveToFile\\Move.txt");
MessageBox.Show("移動成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
/// <summary>
/// 方法名稱:btnDelete_Click
/// 方法作用:刪除文件
/// 作者:心語
/// 完成時間:2010年5月25日20:14:46
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
if (Directory.Exists("D:\\Move.txt") == false)
{
MessageBox.Show("目標文件夾不存在此文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
//刪除
Directory.Delete("D:\\Move.txt");
MessageBox.Show("刪除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
}
C#對文件的操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
Linux基本操作命令
wbzjacky
2019-02-24 13:12:38
真實的模擬***綜合實驗
wbzjacky
2019-02-24 13:12:37
三層交換機的HSRP、vlan、端口聚合
wbzjacky
2019-02-24 13:12:37
HSRP和二層交換機的端口聚合、vlan
wbzjacky
2019-02-24 13:12:37
如果同事暗中傷害你,應該怎麼辦?
這個饅頭有餡
2019-02-24 13:59:08
職場中,抱怨越多的員工,越被領導瞧不起!
這個饅頭有餡
2019-02-24 13:59:08
老程序員被裁,應屆生卻能月薪 1.3 萬?這你能忍?
前端高達
2019-02-24 13:48:04
遇到到處蹭吃卻從不請客吃飯的主怎麼辦?
樑軍年
2019-02-24 13:26:35
高標準機房綜合配線安裝
wbzjacky
2019-02-24 13:12:38
IPsec ***實驗
wbzjacky
2019-02-24 13:12:37
CISCO路由AAA的Easy ***
wbzjacky
2019-02-24 13:12:37
CISCO訪問控制列表 企業網絡管理的必殺技
wbzjacky
2019-02-24 13:12:37