Rewriting the xml file in my application folder - c#

Am using WPF-C#, when am closing my application i need to write some xml document. its located in my application folder (../Data/test.xml). its working while debuging. when i deploy my application cant able to access the xmlfile in the program files folder. how i can resolve this issue.. the following image is the exception http://img197.imageshack.us/i/errqu.jpg/

Your users cannot modify a file located in Program Files.
If you have a configuration file you want to modify, you need to store it in the user's own ApplicationData folder, e.g.:
C:\Users\Bill\AppData\Local\SoftCo Software Inc\SuperApp\appconfig.xml
If you want standard users to be able to modify this config file, then your installation program is supposed to grant Everyone Full Control to the file. Your installation program is known to be running as an administrator, so it will have permissions to change permissions.
But you should not do that; this allows one user to alter program settings that will affect another user. These settings should be per-user (and stored in %AppData%, which the user already will have access to).

It is because of Windows UAC. You can fix the issue either
using Run As Administrator option, from right click menu in the application.
Modify the AppManifest file.
Change the file location user writable locations.

Related

Why my .exe file doesn't work when I install the program?

I have a problem when I install my Windows Forms App in a computer. The .exe file doesn't do anything when I click it. To start the aplication, I have to run the program as administrator.
It is developed in C#, .Net Framework 4.7.2 and I install it in a computer with Windows 10.
THanks!
You should not save anything to the application folder i program files. You should use a separate folder for any data that might change. There are a few locations to chose from:
ProgramData contains application data that is not user specific.This data will be available to all users on the computer. Any global data should be put in here.
AppData folder contains configuration settings, downloaded
information/files for a particular user. So, for example any user
specific preferences and profile configurations can be stored in the
AppData folder. The AppData folder is further divided into three
subfolders
Roaming - This folder contains data that can move with your
user profile from a computer to another.
Local - This folder contains
data that will not move with your user profile.
LocalLow - You can put
in lowlevel access information such as information related to web
browser running in a protected mode in this folder.
source
The location for these folders can be accessed by using Special Folders
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

What is the right way to create a config file that can be modified by a user with no admin rights?

My C# program has an app.exe.config file. User can install the program anywhere. If the user chooses to install the program in the "Program Files" folder, the program will have privilege problems modifying files in Windows 7, so I've externalized the files that can be modified to the AppData folder. Except app.exe.config file, that should also be modifiable by the program when user changes its settings. Apparently it should be in the same folder as where app.exe.
I think this is a quite trivial problem, however I cannot find the right solution.
Copying the config file elsewhere, modifying it and copying back to Program Files;
Creating a second config file elsewhere;
Not giving the user the choice of installation folder;
Forcing the program to be run as admin.
All of these solutions would lead to a bad design. What solution wouldn't?
EDIT: Maybe some of my premises are wrong and it is possible to have the config file installed to AppData or anywhere else without changing the executable's location. I haven't yet found for sure if this is possible, however.
The Application Settings Architecture should help you - you can define user settings as well as application settings that are stored in a user.config file.
Reference to MSDN: http://msdn.microsoft.com/en-us/library/0zszyc6e(v=vs.110).aspx
"Non-default user-scoped settings are stored in a new file, user.config, where user is the user name of the person currently executing the application. You can specify a default for a user-scoped setting with DefaultSettingValueAttribute. Because user-scoped settings often change during application execution, user.config is always read/write."
Change app.config file permissions upon installation. If you are using WIX (I'm just assuming since it's the most popular solution nowadays) have a look at http://wixtoolset.org/documentation/manual/v3/xsd/util/permissionex.html

Unauthorized Access Exception writing file from .exe

My WPF application writes an XML file to a folder within the CommonApplicationData folder on a Windows 7/64 machine. This works fine from Visual Studio 2010. When running from the .EXE file, I get a System.UnauthorizedAccessException when writing the file.
Is this a problem with my initial setup of the folder? Or is this related to the permissions of the executable file itself? Not quite sure how to handle this one??
Paul
I think that it is a problem with permissions to the folder.
Probably Visual Studio runs your application as an administrator and the .EXE file is executed as a normal user.
Perhaps you want to re-evaluate storing that data (XML) in that location all the time. Limited users won't be able to write to it. Sure, you can force admin privs but your users may not always have that option (and it's kinda a hack anyway).
The question below seems to outline a work-around depending on the user's priv level.
writing files to Common Application Data folder denied
Right click on the *.exe file and "Run as administrator".

Create a Setup File Windows.Net C#

I have created a windows application setup program, it needs to have a text file in the application folder. The file is also included while creating the setup.
Once the setup successfully completes and my program tries to modify the file based on user input, its simple throwing an exception.
I am using Windows 7 Home Premium OS.
Any suggestion/help will be great to overcome this issue.
This is normal on a Vista or Win7 machine. Or a properly secured XP machine for that matter. The normal install location for programs, like c:\program files\your company\your app, is read only for most users. UAC is a counter-measure to malware messing with programs.
You'll need to store the text file into a writable location, the AppData folder. In the Setup project, right-click "File system on target machine" and select User's Application Data Folder. Find that file back at runtime through Environment.GetFolderPath, passing Environment.SpecialFolder.ApplicationData. Or use "User's Personal Data Folder" if the user should be able to find it back easily through the Documents folder.
What exception is being thrown? It could be a UAC issue.

file write permission issue under "Program Files" folder

I am using inno setup to make a installation package for my application, and my application is written by C# + .Net 2.0 + VSTS 2008. Inno setup => http://www.jrsoftware.org/isinfo.php and I install my application under Program Files/Foo folder (Foo is my application name). My application is targeting to Windows Vista.
The issue I found is my program cannot write to the folder Program Files/Foo. And I need the permission of write to this folder in order to save some configuration files. The strange thing I notice is the folder Program Files/Foo is marked as readonly and I have checked all folders under Program Files are marked with read only, such as Office.
My questions are,
Why all folders are marked as read only under Program Files? It means we should not write to individual application folders under Program Files? If not, where should we write information to disk like user last selected configuration information of an individual application?
If we could write to individual application folders under Program Files, what is the solution? I do not want my application to Run As administrator to solve this issue, and if there are solution to write to this folder, I want to require minimal permission if possible.
You should write user specific config data to the Application Data folder for the current user, using the special folders enum and the Enivronment.GetFolderPath.
Best Practice is to not store config data in the Program Files folder. Instead, store your application's data in %AppData%\YourApplicationName. Depending on whether you want to store your config data per-user or in a shared common folder, use one of the following enums to get the folder path:
string userAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string commonAppData = Envrionment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
By default, Vista users do not run programs as Administrators and hence those programs have only read access to the folders under "Program Files". Users can change this behavior by disabling UAC and you could ask your users to do that, but in an office setting users might not have that option. That's why you use AppData instead -- applications can always read and write data to the AppData folder.
Information on UAC can be found at Microsoft's site. Although this page is fairly long, it's a starting point for understanding UAC:
http://msdn.microsoft.com/en-us/library/bb530410.aspx
A common solution would be to install configuration files to the Application Data folder i.e. like follows:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Categories