简体   繁体   English

使用C#将文本文件保存到已发布的应用程序

[英]Saving text files to a published app in c#

I have created an application and published it using clickonce. 我已经创建了一个应用程序,并使用clickonce发布了它。 I want to save data entered into the application in a text file. 我想将输入到应用程序中的数据保存在一个文本文件中。 The published app has a folder and two files. 已发布的应用程序有一个文件夹和两个文件。 I want my text file to be saved in the folder which by default is called "Application Files" however my text file is being saved in the debug folder in my project. 我希望将文本文件保存在默认情况下称为“应用程序文件”的文件夹中,但是将文本文件保存在项目的调试文件夹中。 Thus if i install the application on someone else's pc, they won't be able to see the text file. 因此,如果我在其他人的PC上安装该应用程序,则他们将看不到该文本文件。 Any help with saving this please? 请帮忙保存一下吗?

When you deploy the application then by default the file will be saved in the same directory as your application executable. 部署应用程序时,默认情况下,文件将与应用程序可执行文件保存在同一目录中。 It only saves to debug because this is where the executable is stored when you compile. 它仅保存用于调试,因为在编译时这是可执行文件的存储位置。 If this isn't what you want then the easiest way to do this is create an app.config file with a key called (for example) FileLocation and then set that to wherever you want to save the file. 如果这不是您想要的,那么最简单的方法是使用名为(例如)FileLocation的项创建一个app.config文件,然后将其设置为您要保存文件的位置。

If you wanted to save it for example to a sub-directory of your application called ApplicationFiles then simply set the value to be "ApplicationFiles" - the application will default to your application path and then append Output to it. 例如,如果要将其保存到应用程序的子目录中,称为ApplicationFiles,则只需将值设置为“ ApplicationFiles”-该应用程序将默认为您的应用程序路径,然后将Output附加到该路径。

Your config file should look as follows: 您的配置文件应如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="FileLocation" value="Application Files"/>
    </appSettings>
</configuration>

To get the value use 获得价值使用

string fileLoc = ConfigurationManager.AppSettings["FileLocation"];

Then use this value to save your file 然后使用此值保存文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM