您的位置:首页 > 其它

通过File文件类的加密和解密文件

2007-12-30 14:45 309 查看
通过File文件类的加密和解密文件主要是对IO命名空间中的文件类的Encrpty和Decrpty方法的使用。

源代码如下: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;
using System.Diagnostics;
namespace FileCryptographic
{
public partial class Form1 : Form
{
public string sourceFile="C://Test.txt";
public Form1()
{
InitializeComponent();
}

private void btnEncry_Click(object sender, EventArgs e)
{
try
{
File.Encrypt(sourceFile);
this.richTextBox1.Text = File.ReadAllText(sourceFile);

Process.Start("explorer.exe", "C://");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
//throw;
}
}

private void btnOpen_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.txtPosition.Text = this.openFileDialog1.FileName;
sourceFile=this.txtPosition.Text;
/*
using (FileStream fs = new FileStream(sourceFile, FileMode.Open))
{
BufferedStream bs = new BufferedStream(fs);
StreamReader sr = new StreamReader(bs);
this.richTextBox1.Text = sr.ReadToEnd();
}
*/
}
}

private void btnDecry_Click(object sender, EventArgs e)
{
try
{
File.Decrypt(sourceFile);
this.richTextBox1.Text = File.ReadAllText(sourceFile);

Process.Start("explorer.exe", "C://");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
//throw;
}
}

private void Form1_Load(object sender, EventArgs e)
{

File.WriteAllText(sourceFile, Properties.Resources.test, Encoding.UTF8);
this.richTextBox1.Text = File.ReadAllText(sourceFile);

Process.Start("explorer.exe", "C://");
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: