简体   繁体   English

如何在Windows窗体应用程序中保存文本文件

[英]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.

相关问题 如何保存 Windows 窗体应用程序的状态(或将数据保存在带有数组的文本框中) - How to save state of Windows Form Application (or save data in text boxes with arrays) 如何将Excel保存为Windows窗体应用程序的一部分 - How to save an excel as part of windows form application 如何将文件保存在Windows应用程序的文件夹中 - How to save file in a folder in windows application 将文本保存在文件中而不删除 windows 形式 c# - Save text in a file without deleting in windows form c# 如何在Windows窗体应用程序中从磁盘保存/检索图像? - How to save/retrieve images from disk in windows form application? 如何在 c# windows 表单申请中永久保存凭据 - how to save credentials permanently in c# windows form application C #Windows表单应用程序如何删除所选的列表框行和文本文件 - C# Windows form application how to delete selected listbox line and text file 如何将任何应用程序的选定文本放入 windows 窗体应用程序 - How to get selected text of any application into a windows form application 使用字典从Windows Form Application上的文本文件删除一行 - Deleting a line from a text file on Windows Form Application using Dictionary 如何将数据从Windows窗体保存到XML文件? - How can I save data from a Windows Form to an XML file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM