Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Access denied to path in a Windows Application

Tags:

c#

c#-3.0

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.

like image 683
Brian Roisentul Avatar asked Oct 15 '10 19:10

Brian Roisentul


People also ask

What C is used for?

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 ...

What is the full name of C?

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.

Is C language easy?

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.

What should I learn C or C++?

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.


3 Answers

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).

like image 150
Reed Copsey Avatar answered Nov 15 '22 19:11

Reed Copsey


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

  • %AppData%
  • %LocalAppData%

Or the result of Environment.GetFolderPath for the following values

  • SpecialFolder.ApplicationData
  • SpecialFolder.LocalApplicationData
  • SpecialFolder.CommonApplicationData
like image 45
JaredPar Avatar answered Nov 15 '22 18:11

JaredPar


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

like image 42
Andy Lowry Avatar answered Nov 15 '22 17:11

Andy Lowry