//=========================第一部分:主界面功能設計=============================
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 FileOptionApplication
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
/// <summary>
/// 讀寫檔案操作
/// </summary>
private void button3_Click(object sender, EventArgs e)
int p = comboBox1.SelectedIndex;
if (p == -1)
{
MessageBox.Show("請您選擇檔案寫入方式", "警告資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
string filecontent = richTextBox1.Text.Trim();
MyFileOption myoption = new MyFileOption();
string filepath = @"c:\1.txt";
bool i = myoption.WriteTextFile(filepath, filecontent, Convert.ToInt16(comboBox1.SelectedIndex));
if (i == true)
{
MessageBox.Show("儲存成功", "儲存資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
MessageBox.Show("寫入檔案時出錯", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
/// 檔案磁盤操作
private void button4_Click(object sender, EventArgs e)
Int16 p = Convert.ToInt16(comboBox2.SelectedIndex);
MessageBox.Show("請您選擇磁盤檔案操作方式", "警告資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
string sourcepath = "c:\\1.txt";
string targetpath = "c:\\2.txt";
bool i = myoption.DiskFileOption(sourcepath, targetpath, p);
MessageBox.Show("磁盤檔案操作成功", "儲存資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("磁盤檔案操作時出錯", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
private void button1_Click(object sender, EventArgs e)
richTextBox1.Text = null;
richTextBox1.Focus();
/// 讀出文本檔案内容
private void button2_Click(object sender, EventArgs e)
MyFileOption myoption = new MyFileOption();
string filepath = @"c:\1.txt";
Int16 i = 0;
string filecontent = "";
myoption.ReadTextFile(filepath, out i, out filecontent);
if (i == 0)
MessageBox.Show(filecontent, "錯誤資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
richTextBox1.Text = filecontent;
else if (i == 1)
MessageBox.Show("讀取檔案成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else if (i == 2)
MessageBox.Show(filecontent, "錯誤資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
/// 檔案基本屬性設定
private void button5_Click(object sender, EventArgs e)
if (checkBox1.Checked && checkBox2.Checked)
File.SetAttributes(filepath, FileAttributes.ReadOnly | FileAttributes.Hidden);
MessageBox.Show("檔案已經改為隻讀且隐藏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (!checkBox1.Checked && !checkBox2.Checked)
File.SetAttributes(filepath, FileAttributes.Archive);
MessageBox.Show("檔案已經改為正常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (checkBox2.Checked)
{
File.SetAttributes(filepath, FileAttributes.ReadOnly);
MessageBox.Show("檔案已經改為隻讀", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (checkBox1.Checked)
File.SetAttributes(filepath, FileAttributes.Hidden);
MessageBox.Show("檔案已經改為隐藏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
/// 檔案夾操作
private void button6_Click(object sender, EventArgs e)
Int16 p = Convert.ToInt16(comboBox3.SelectedIndex);
MessageBox.Show("請您選擇檔案夾操作方式", "警告資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
string sourcepath = @"c:\1";
string targetpath = @"c:\2";
string[] filesname = null;
bool i = myoption.DirectoryOption(sourcepath, targetpath, p, out filesname);
MessageBox.Show("磁盤檔案夾操作成功", "儲存資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (filesname != null)
foreach (string somestring in filesname)
{
richTextBox1.Text += somestring + "\r\n";
}
MessageBox.Show("磁盤檔案夾操作時出錯", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
u實驗步驟(3):
向項目中添加名為FileOption.cs的類檔案,并準備填寫關于檔案操作的各種方法,如圖3-8所示:
圖3-8 建立FileOption.cs圖
本文轉自 qianshao 51CTO部落格,原文連結:http://blog.51cto.com/qianshao/210961,如需轉載請自行聯系原作者