[英]How to save text file in windows form application
I am creating a windows form application. 我正在创建Windows窗体应用程序。 for security reason i want to store licence information into a text file and want to encrypt it.
出于安全原因,我要将许可证信息存储到文本文件中并希望对其进行加密。
var serializer = new XmlSerializer(typeof(Licence));
var saveData = new Licence
{
ProductId = txtProductID.Text,
ProductKey = txtProductKey.Text,
CreatedDate = validate.CreationDate,
ExpireDate = validate.ExpireDate,
DaysLeft = validate.DaysLeft
};
using (var writeFile = File.OpenWrite("data.txt"))
{
serializer.Serialize(writeFile, saveData);
}
using this code i can able to create text file successfully. 使用此代码,我可以成功创建文本文件。 but when i publish this project and install it it gives me error.
但是当我发布并安装该项目时,它给了我错误。 error message is...
错误消息是...
Access to the path "C:\\Program Files (x86)\\WebenixSystem\\Metro Whole Sale\\data.txt" is denied.
拒绝访问路径“ C:\\ Program Files(x86)\\ WebenixSystem \\ Metro Whole Sale \\ data.txt”。
************** Exception Text ************** System.UnauthorizedAccessException: Access to the path 'C:\\Program Files (x86)\\WebenixSystem\\Metro Whole Sale\\data.txt' is denied.
**************异常文本************** System.UnauthorizedAccessException:访问路径'C:\\ Program Files(x86)\\ WebenixSystem \\ Metro Whole Sale \\ data.txt'被拒绝。
How can i resolve this issue, and how can i encrypt this txt file? 如何解决此问题,以及如何加密此txt文件? please help...
请帮忙...
As the error suggests - you do not have permission to access the path you are trying to save the file to. 如错误所暗示-您无权访问要将文件保存到的路径。
As the file will be encrypted, you do not have to hide it. 由于文件将被加密,因此您不必隐藏它。 Therefore, you could save it in the documents folder of the users account.
因此,您可以将其保存在用户帐户的documents文件夹中。
Using this line of code will return the path to the 'My Documents' folder. 使用此行代码会将路径返回到“我的文档”文件夹。 You can create a new folder within there or save the file directly to this path.
您可以在其中创建一个新文件夹,或将文件直接保存到此路径。
Note this is a relative path so it will work for all users. 请注意,这是相对路径,因此适用于所有用户。
String pathToDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Also to note: The license information won't be shared between different users. 另请注意:许可证信息不会在不同用户之间共享。 If user will logon with different account, the license data won't be found, because each user has its own "MyDocuments", "AppData" etc...
如果用户将使用其他帐户登录,则将找不到许可证数据,因为每个用户都有自己的“ MyDocuments”,“ AppData”等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.