5 Replies - 731 Views - Last Post: 02 April 2016 - 11:21 AM Rate Topic: -----

#1 Rev0luti0n   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-February 16

How to "ask" administration in the files in my program

Posted 28 March 2016 - 11:37 AM

Hello ! I am creating a program, and i have a silly problem. My program edits some times, some files in the program directory which is in C:\Program Files (x86)\MyProgram. The problem is that when i try to edit the files inside my program, it throws an unhandled exception of insufficient permission. I don't understand, how other programs does edit their own file in their directory ??
Is This A Good Question/Topic? 0
  • +

Replies To: How to "ask" administration in the files in my program

#2 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: How to "ask" administration in the files in my program

Posted 28 March 2016 - 12:48 PM

i think what you need to do is alter the access level of your application, you can add the following to the manifest and it should work

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />



Disclaimer: this is from a SO thread
Was This Post Helpful? 1
  • +
  • -

#3 JacobH   User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 181
  • Joined: 07-September 15

Re: How to "ask" administration in the files in my program

Posted 28 March 2016 - 01:00 PM

If your application is often used for purpose that do not need admin privileges, but at times for certain features needs it, I suggest catching the exception and alerting the user to perform the applications operations requested they need to run the program with admin rights. A message box if possible should suffice :).
Was This Post Helpful? 1
  • +
  • -

#4 Rev0luti0n   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-February 16

Re: How to "ask" administration in the files in my program

Posted 28 March 2016 - 01:38 PM

View PostDamage, on 28 March 2016 - 12:48 PM, said:

i think what you need to do is alter the access level of your application, you can add the following to the manifest and it should work

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />



Disclaimer: this is from a SO thread

Thank you man !!! It seems to work ! :)

View PostJacobH, on 28 March 2016 - 01:00 PM, said:

If your application is often used for purpose that do not need admin privileges, but at times for certain features needs it, I suggest catching the exception and alerting the user to perform the applications operations requested they need to run the program with admin rights. A message box if possible should suffice :)/>.

Nope, it wants admin rights all time :( This could be an alternative, but a solution like Damage game me, and seems to work, is the best !!!
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to "ask" administration in the files in my program

Posted 28 March 2016 - 02:16 PM

*
POPULAR

It's definitely not "the best", because applications should run with as few privs as they require.

The real question is, why are you editing files in your own program's install directory, instead of using the purpose-built AppData folders?

The reason you're running into this problem in the first place is because Microsoft tightened security measures to require Administrative privs to write to certain areas on the disk, such as under the root directory (C:\), the Windows directory, and the Program Files directories. This is to protect users from malicious applications overwriting other application or system data.

To alleviate this problem, MS expanded how we use AppData. Try this: put the following into your File Explorer's address bar: %appdata% (percent signs included).

It expands to something like "C:\Users\username\AppData\Roaming". Each application that uses AppData has a folder here. This area is where your program-specific files should go. This will also create a separate enclave for each user.

Your program is essentially guaranteed write access to this folder.

How you fetch the path to this folder varies based on what you're using, but it's typically as simple as:

var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);


This is the correct way to deal with this problem. Forcing admin is a security risk (if there's a security hole in your program, with administrative rights it becomes vastly more dangerous).
Was This Post Helpful? 5
  • +
  • -

#6 Rev0luti0n   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 15-February 16

Re: How to "ask" administration in the files in my program

Posted 02 April 2016 - 11:21 AM

I didn't knew that option. Thank you very much !!

This post has been edited by andrewsw: 02 April 2016 - 01:42 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1