Winforms showing different values from xml - c#

I have a windows service project which contains a Test.xml file. In the properies it is changed to copy always.
I have created a setup for service and after installing the service the Test.xml is present in the C:\program files(86)\service folder.
Now I want to edit the Test.xml file throgh a UI. So i have created a small Winforms project 'EditXml'. The EditXml is accessing the Test.xml like this
document = XDocument.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.xml"));
I have added the EditXml.exe also to the setup of services. So after installing the service the EditXml.exe is also present in the folder.
But when I edit using EditXml I cant see the changes in test.xml.
Why values are different from Test.xml? Is this a correct approach? Let me know if question is not clear.

I think that you have an issue with inssuficient permissions and windows file virtualization. When your installer creates a file inside Program Files folder, it usually sets permission only for read operations.
When you try to modify the file using an application running at user account that has now write permissions, windows makes an illusion that the file has been successfully written, but puts it in a virtual store - check if such directory/file exists in c:\users\CurrentUserName\Local\VirtualStore\Program Files (x86)\YourFolderName
Only your application can see modified file version, but all other application see original version written by installer.
To be able to modify the file using an application, you need to add write permissions to the file.

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.

Create app with data already present on install

I'm currently programming an app that need to access to some Excel files.
So what I need is to create a folder in the app files with these Excel files in it but I want that folder to be created at the app install, so they would be accessible for every device that install the app.
The files also need to be modifiable in the future by the user.
The problem is that I don't know how to do it right. Should I just create a new folder in the Solution Explorer and put the Excel files in it ? Should I create the folder programmatically and force the user to put them manually in that folder ?
I don't really know how to do it so that the application will not be too complicated to be modified by the user.
EDIT : Also, if I put the files in the Assets, will the user be able to change them later ?
So what I need is to create a folder in the app files with these Excel files in it but I want that folder to be created at the app install, so they would be accessible for every device that install the app. The files also need to be modifiable in the future by the user.
For your requirement, you could use ApplicationData.LocalFolder to store Excel files, LocalFolder has full access permission. LocalFolder exists in the app's sandbox path and will be created after the app is installed.
Also, if I put the files in the Assets, will the user be able to change them later ?
Assets folder exists in Windows.ApplicationModel.Package.Current.InstalledLocation and it is read only that often use to store some static resource. You can't modify the file at run time.
For more details about file access permissions please refer this document.
Forget the installer. It is deprecated and the new apps on Windows are installed over the store.
If you want to copy some files (Excel templates), you have to put them in your resources. At startup you can check if there is a folder in your App-Data folder with your files, if you don't find them, you can copy it from your resources. So even if the file is deleted, your app can copy them in the next start.
If you use Windows Forms with .NET, you can check this page:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.localuserappdatapath?view=netframework-4.8
If you are writing an UWP-app, check this page:
https://learn.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data

How to attach file to windows service?

Is it possible to attach a flat file to windows-service?
My windows-service uses few flat file (for data purposes). Usually, if it's a normal executable I would place those files in the same directory as exe. How can I achieve that with windows-service?
I've done some research on this, but all I found was:
1. Pass a path to those files as a parameter to windows-service.
2. Make a fixed path and just require those files there
But I don't like those solutions. I was wondering if it's possible to attach those files to the windows service while installing it?
How about adding these files inside the project as Embedded Resources? They won't show up on the disk, but you could still properly read them from inside the assembly itself.
Here's some reference: https://support.microsoft.com/en-ie/kb/319292
You can look up the directory that your application is installed in at runtime, using the Application.StartupPath property from System.Windows.Forms. This works for both applications and services. You should avoid hard-coding the path that you think your application is installed in, because the end user may have installed it somewhere else. You should also avoid using the current directory (i.e., opening the file by name only, without a specific path) because the user may be running your application with a different current directory.
Note that installutil does not make a copy of your service executable. So the service runs from the same directory that it was in when you installed it, and any files you place in that directory should still be there when the service is running.

How do I configure VS installer to put files in LocalApplicationData

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]

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