2

New to C#, I'm developing a windows form application inside visual studio. The app uses ICSharpCode.SharpZipLib library to update a zip's content.

When the zip file is inside a non system partition, say like D:\myzip.zip the files gets updated correctly. But, if the zip is in the system partition, C:\myzip.zip the app returns an error; Could not find file C:\myzip.zip even though the file is there!

This has to do with admin privileges because when I run the app as administrator it works.

The question is; how could I enable those privileges by default?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
numediaweb
  • 16,362
  • 12
  • 74
  • 110
  • possible duplicate of [How to force my .NET App to run as administrator on Windows 7?](http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7) – Hans Passant Feb 23 '14 at 11:24
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Feb 23 '14 at 11:25

2 Answers2

3

You can add app manifest to exact your app to run as administrator (msdn).

Add the following code to your manifest file:

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

Adding app manifest step by step:

  1. Right-click on the project in the Solution Explorer.
  2. Click Add New Item.
  3. Find and select Application Manifest File:

enter image description here

Edit app manifest file:

 <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">       
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
        </security>
      </trustInfo>

      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>    
        </application>
      </compatibility>
    </asmv1:assembly>
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • I already tried that but the when I run the release app, it doesn't ask for admin privileges by default; I mean, without doing right-click run as admin.. – numediaweb Feb 23 '14 at 11:24
  • @numediaweb I added step by step how add app manifest to your app. Check this out. – kmatyaszek Feb 23 '14 at 11:32
  • I already did that by updating `app.manifest` in solution explorer.. but somehow after playing with the project settings.. now it works! – numediaweb Feb 23 '14 at 11:50
-1

you want to enable this by default on you'r computer only right? because on other computer you can't, this what security all about

knightsb
  • 337
  • 3
  • 11
  • I want that the app when run on other computers (and mine included) asks for admin privileges so it can write to files inside, say `C:\Program Files (x86)\..` – numediaweb Feb 23 '14 at 11:26
  • Clarification questions should be as comments to the initial question and not as answers, downvoted – John Demetriou Jul 11 '17 at 13:27