简体   繁体   中英

Saving text files to a published app in c#

I have created an application and published it using 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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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