How do I configure VS installer to put files in LocalApplicationData - c#

Everything in Visual Studio seems to lead one to putting data files with the application.The app.config goes there, when I create an .XML data file, there is a Copy to Output property that will automatically copy that file to the exe folder. Howerver, it seems that under Vista and Win7 UAC doesn't want the application to be able to write data to any file in the application directory. So I'm changing my evil ways so that I use the LocalApplicationData folder for files I want to read and write. (I just read the app.config so I'm leaving it alone)
I'm using a VS2010 Visual Studio Installer project to create the installer for this app and I can't seem to find a way to target the folder for my .xml file to the LocalApplicationData folder. I can click on the file and see a Folder property but the dialog only has options for Application Folder, User's Desktop and User's Program Menu. Is there some way to do this in the installer or do I have to write code that checks for the file and copies it over from the .exe folder when it doesn't exist? I figure I'm late to this particular party and there must be a canonical way of handling this.
Also, I'm wondering about debugging, is there something similar to the copy if newer functionality in the build process that will now copy this .xml file automatically over to the LocalApplicationData folder whenever I update it?

The Setup project doesn't expose LocalApplicationData in the Special Folders list. You can use it anyway by doing this:
Add a Custom Folder and set the DefaultLocation property to [LocalAppDataFolder]

Related

How to include externel user files into UWP side-loading package?

Say I use some .json files to descript some object data which effect to the program's behavior, I hope to use these files in the following scenarios
The default values, for this purpose, I need a set of files follows with the application to be packed and installed.
I wish it could be edited by human manually. (Because something have no interface to be modify on UI)
Both user and the program need to kwnow the location the files will be placed after installation.
In debugging stage, I could put these files in the user\AppData\Local.. folder and I know how to access them, but I don't know how to put files into the package and will them generated to anywhere after install?
Thank you for any suggestion.
ps.
I use the "Blank App (WinUI 3 in UWP)" template to create my
application.
I'm new in UWP and WinUI, I used to write traditional Windows Form programs.
How to include externel user files into UWP side-loading package?
You could place the json file into app's project and set the file property as Content, then it will deploy into installtion folder after package install. and please note the json file is readonly in the installtion folder.
so you could call CopyAsync method copy the file to the destination folder that app's local folder with full permission.
For more details about file access permissions please refer this document.

How to create text file in .NET project folder, not in bin/Debug folder where is by default

I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.
using (StreamWriter file = File.AppendText("log1.txt"))
{
file.WriteLine("Hello from the text file");
}
If the file does not exist, the application creates it in the autogenerated folder bin/Debug.
Is there a way to create this file in the project's directory, where I have .csproj file?
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug?
No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:
User Data
Roaming User Data
All User Data
You can acess the above folders in .NET application using the Environment.SpecialFolder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
As per your given code, try this :
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
file.WriteLine("Hello from the text file");
}
This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.
That's why .NET creates them there firstly?
If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.
You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.
If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.

Windows installer (MSI) does not copy a config file

I installed an MSI buillder tool on my visual studio 2017 and started deploying my desktop application with generated MSIs. The istaller is able to copy vital files and adds registery keys but it does not copy some additional config files which are required for logger. According to this page, switching "Copy to Output Directory Property" to "Copy always" supposed to take care the issue however, it still does not copy the config file into output directory on client's computer.
Can somebody give an advice about how I can diagnose this problem ?
Edit:
I think I can explicitly add logs files into MSI with following method but I have two concerns on this. Would I add the file into MSI with its global or relative path ? Secondly will it be a good practice ?
Edit 2:
For the reference for developers who has the same issue, looks like the method stated above adds files with its relative path. I added screenshot of difference page at source control.
It is completely normal to add individual files to a VS setup project. Every tool that generates an MSI works this way. VS setups are probably the exception with their "project output" type of input choice, where you get little idea of the actual files that will be installed. So you get the best control of the MSI content by adding each file individually, including that config file. Also, some files really don't belong in the Application Folder (that defaults to Program Files) because they are data files that belong somewhere like the User's Application Data.
The path where the MSI build gets its files from is nothing to do with where that file is deployed on the target system. You tell the MSI build where files will be deployed on the target system by using the File System view on target machine, where you get a list of destination folders to add files to.
Also, the copy to output directory stuff is nothing to do with the build of the MSI file. As far as I know, its main reason is to allow you to have all dependencies at the output build location of the code so that the program will work correctly from that location, and it happens to give you the opportunity to get all the files going into a setup from the same place. It does not mean "copy this file somewhere in such a way that it is automatically included in the MSI build and the deployed on the target system".
Once you get the MSI working and installing the config file, you may run into Windows Installer file overwrite rules that prevent you from overwriting files that have been updated after they were installed.

Creating folders inside LocalLow through Installer

I am developing deployment project for Vista. In Vista inside AppData folder there are Local, LocalLow and Roaming folders. What I want from installer is to create folder 'Data' inside LocalLow folder and put there file data.xml (AppData\LocalLow\Data\data.xml). Installer should make this operation for all existing user accounts.
How can I achieve this?
This is a screenshot of setup project('Data' folder configuration) which is not working:
Attached example creates the following path: \AppData\Roaming\LocalLow\Data\data.xml
I think a much better approach is to store the xml file in the application's install directory, then, when the application starts up, copy the file to the appropriate directory.
The primary issue is this: what if a user that wasn't on the machine when it was installed launches the application?
Since your installer didn't copy the file to their directory (because it didn't exist), your application would either have to do something or fail anyway.

Folder and file not added to User's Application Data folder

Ladies and Gentlemen , I have been stuck with this for a few hours and do not find an answer. I have a Setup project in Visual Studio that creates an installer for my C# application. What I want is to add a folder with an XML file from which my application can read and write to the User's Application Data folder. In the File System Editor window I added the User's Application Data folder. In this folder I added a new folder (renaming it to my app's name) and then place the XML file in there. I also set the AlwaysCreate to true for the folder. The installer should create the folder in C:\Users\UserName\AppData\Local and add the file to it. However, the installer does not create the folder or the XML file my application uses. What am I missing? Is there another way to install a read/write XML file? Thanks in advance!
Ok, I found what the issue was. If a file is added to the User's Application Data folder it is installed on the target computer at C:\Users\Username\AppData\Roaming and not into AppData\Local.
Therefore, I changed my application get the file from Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) instead of Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData).
Hope it helps someone else...

Categories

Resources