C# 使用密码保护文件
我有一个C#应用程序,我想在其中创建一个文件:C# 使用密码保护文件,c#,winforms,file,io,password-protection,C#,Winforms,File,Io,Password Protection,我有一个C#应用程序,我想在其中创建一个文件: string fileName = "test.txt"; // a sample file name // Delete the file if it exists. if (System.IO.File.Exists(fileName)) { System.IO.File.Delete(fileName); } // Create th
string fileName = "test.txt"; // a sample file name
// Delete the file if it exists.
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
// Create the file.
using (System.IO.FileStream fs = System.IO.File.Create(fileName, 1024))
{
// Add some information to the file.
byte[] info = new System.Text.UTF8Encoding(true).GetBytes("This is some text in the file.");
fs.Write(info, 0, info.Length);
}
string s = "";
using (System.IO.StreamReader sr = System.IO.File.OpenText(fileName))
{
while ((s = sr.ReadLine()) != null)
{
textBox1.Text += s;
}
}
我需要通过程序外的密码保护该文件,并且该文件在程序内无需密码即可访问
如何更改代码段以执行此任务?无法使用现成的密码保护文本文件。 您需要使用其他应用程序来封装它 最简单的方法是使用Zip文件 通过以下方式形成: 这将导致两个步骤
您不能使用现成的密码保护文本文件。 您需要使用其他应用程序来封装它 最简单的方法是使用Zip文件 通过以下方式形成: 这将导致两个步骤
如果不想创建压缩/解压缩的zip文件,也可以对文件进行加密
如果不想创建压缩/解压缩的zip文件,也可以对文件进行加密
如果您自己还没有尝试过,请参考System.Security.Cryptography命名空间并试一试。如果您自己还没有尝试过,请参考System.Security.Cryptography命名空间并试一试。它给我错误ZipFile()classit给我错误ZipFile()类
using (var zip = new ZipFile())
{
zip.Password= "VerySecret!!";
zip.AddFile("test.txt");
zip.Save("Archive.zip");
}