I've built a Windows Application using c#, on Windows 7.
Everything was working fine, so I've created a Setup Wizard project and then built it. Once I install the app, I can open it correctly, but when I try to make some action that writes a text file(with logging purposes) it crashes, thrwoing me the following error message:
UnauthorizedAccessException
Access to the path 'C:\Program Files (x86)\MSProgram\MSProgram\log.txt' is denied.
When I manually give that folder full rights, it works fine. Now, the question is the following:
How do I programmatically give the app rights for writing things in my app's directory? So every person that downloads it doesn't experience the same problem.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.
Don't. Applications should not write data into their installation directory directly. Doing so will make the application work poorly on Windows Vista and Windows 7, since it's not the proper way of saving data.
You should instead use Environment.GetFolderPath, and write into a good location, such as the user's application data folder (Environment.SpecialFolders.ApplicationData).
The solution is not to grant rights to that directry but to instead write to a folder that is more suitable for application logs. The "Program Files(x86)" and "Program Files" is a place for application installation, not logging.
A more appropriate location would be the per user data folders
Or the result of Environment.GetFolderPath
for the following values
Generally it's not good practise to write to the Program Files directories, I usually write log file to the AppData folder, which you can get at by using:
var logFilename = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "log.txt")
You will probably want to create a directory in there or give the log file a more descriptive name. You may also want to consider if multiple instances of your app could be running for the same user.
If you must write to the Program Files directory you will need either run the application with administrator privileges, either using run as administrator, or by requesting higher privileges on your application. Another possibility would be to setup your installer to give the installing user full folder rights, although how to do this will depend on the installer you are using.
Hope this helps.
Andy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With