我正在尝试做一个程序,它在手机的独立存储内存中存储很多(5-15).txt文件。我注意到使用Windows Phone Power Tools等程序来读取这些文件是多么容易,因此我决定对它们进行加密。我使用这个链接作为教程:Windows Phone 7:解密来自孤立存储的许多文件
http://msdn.microsoft.com/en-us/library/windows/apps/hh487164(v=vs.105).aspx
加密正常工作,因为我明显节约一次一个文件。但是,我在尝试解密时遇到问题。 我应该如何编辑我的代码,以便解密很多.txt文件?下面是我的代码,我使用的时刻:
private void IsoRead()
{
System.IO.IsolatedStorage.IsolatedStorageFile local =
System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
string[] filenames = local.GetFileNames("./DataFolder/*.txt*");
foreach (var fname in filenames)
{
//retrieve byte
byte[] ProtectedByte = this.DecryptByte();
//decrypt with Unprotect method
byte[] FromByte = ProtectedData.Unprotect(ProtectedByte, null);
//convert from byte to string
fText = Encoding.UTF8.GetString(FromByte, 0, FromByte.Length);
this.Items.Add(new itemView() { LineOne = fname, LineTwo = fText });
}
}
而另外一个:
private byte[] DecryptByte()
{
// Access the file in the application's isolated storage.
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream readstream = new IsolatedStorageFileStream
("DataFolder\\"/*Here's where I'm having problems with*/, System.IO.FileMode.Open, FileAccess.Read, file);
// Read the written data from the file.
Stream reader = new StreamReader(readstream).BaseStream;
byte[] dataArray = new byte[reader.Length];
reader.Read(dataArray, 0, dataArray.Length);
return dataArray;
}
所以基本上程序有一个列表视图页面,得到的来自独立存储的文件。如果有人感动,它会进入一个新的页面,显示写入的内容。
奖金问题:我可以加密WP7/WP8中的文件夹吗?
编辑:在IsoRead中添加一行代码。
你究竟有什么样的问题呢?性能相关? – FunksMaName 2014-12-09 12:02:15
@FunksMaName我的问题是:我应该如何编辑我的代码,以便我可以解密很多.txt文件? 我编辑了我的文章,因此现在更容易注意到其他人的问题。 – user3616427 2014-12-09 12:19:52
好的,我现在得到问题了。我已经将这个例子改编为答案,以便它适用于多个文件。 http://msdn.microsoft.com/en-us/library/windows/apps/hh487164%28v=vs.105%29.aspx我认为你在该行有问题,因为你不是试图读取一个有效的文件。 “DataFolder \\”不是指向一个文件,而是一个文件夹,因此您可能需要提供一个有效的文件名。 – FunksMaName 2014-12-09 16:54:09