c# vista does not giving access to app data folder - c#

I am doing a project where I have to generate some logs[which are text files]. I am generating the logs by a thread [thread is essential] to application data folder. But vista does not giving me access and throwing an exception "access denied". I am very much confused. Any suggestion will be very helpful. Thanks.

Run your application as administrator, or give full control of the folder to the Users group (which is unsafe).
You can also force UAC elevation in your application.

do you get the path to application data by using the following method?
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
Usually, that directory is writable by local user, but that's not the case of web applications which run unprivileged.
In order to respond to your above comment, you can programmatically require administrator privileges via UAC. Create app.manifest in your solution and put the following code into it
<?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="highestAvailable" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
Hope to have been of help to you.
[Edit] Ruel just added the UAC link while I was writing. My answer is then duplicate ;)
Kudos to him

When you say "application data folder", do you mean the location you installed the app to, or do you mean what the operating system considers the "application data folder"?
static void Main(string[] args)
{
string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), #"MyCompany\MyApp");
Directory.CreateDirectory(folder);
using (StreamWriter writer = new StreamWriter(Path.Combine(folder, "app.log"), false))
{
writer.WriteLine("Logged.");
}
}

Related

How to get access to file to delete for emulators files?

I'm facing issue to remove full directory because some file get this error System.UnauthorizedAccessException: 'Access to the path 'pstore.bin' is denied.'
I've tried to give permission to single File but same, tested this but doesn't work too:
var di = new DirectoryInfo(item);
di.Attributes &= ~FileAttributes.Normal;
Directory.Delete(item, true);
Not sure how I can remove this file, I've tested almost everything I found on this forum, and not sure what is wrong. To be clear this file is from Android Studio AVD Emulator.
Updating exception notes:
**System.UnauthorizedAccessException: 'Access to the path 'pstore.bin' is denied.'**
This exception was originally thrown at this call stack:
[External Code]
TwitterSuite_v._1._0.Form1.fullZipAll.AnonymousMethod__0(string) in Form1.cs
[External Code]
To make the C# application run with administrator privileges, add a manifest file (*.manifest) to the application. To create manifest file, follow this path in Visual Studio IDE:
Project > Add New Item > Application Manifest File
For the application to run with administrative privileges, you need to add the element <requestedExecutionLevel> in the app.manifest file:
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<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">
<!-- In order for the program to run with administrator privileges, you need to add the following line to the *.manifest file. -->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application></application>
</compatibility>
</assembly>
If this solution doesn't work for you, get the error code in the thrown exception and update the question:
try
{
var di = new DirectoryInfo(item);
di.Attributes &= ~FileAttributes.Normal;
Directory.Delete(item, true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Try to verify for what reason this error is occurring:
The file can be read-only. UnauthorizedAccessException
There may be a file that does I/O operation during deletion. IOException
The caller does not have the required permission. SecurityException
The directory described by this DirectoryInfo object doesn't exist or could not be found. DirectoryNotFoundException
References
Create and Embed an Application Manifest (UAC)
System.UnauthorizedAccessException: Access to the path "..." is denied
How do I force my .NET application to run as administrator?
How do I create/edit a Manifest file?

Manifest does not work in VS2015

I'm trying to allow my program to write to the HKLM Keys. In order to achieve that I tried to create a manifest file:
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<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>
</assembly>
But even with this file I'm still getting an exception:
"System.UnauthorizedAccessException" in mscorlib.dll
Cannot write to the registry key.
I'm using VS2015. I click create new element then pick Manifest file. Set the name to MyProgram.exe.manifest. Then in Project properties I select the proper manifest. And finally I'm changing execution level in Manifest file to requireAdministrator and rebuild the program.
But when I'm executing the MyProgram.exe it never shows that UAC window and when I try to write to HKLM in throws an exception. So I suspect the manifest is not working properly.
Please advise.
Thank you!!
You can do like this:
using administrator run program.
No permission to set permissions.

Run application with admin privileges from startup folder [duplicate]

This question already has answers here:
How to run a program automatically as admin on Windows 7 at startup?
(9 answers)
Closed 7 years ago.
I have copied the shortcut of my application in startup folder for all users. I want my application to start with admin privileges when the the system is restarted and a non-admin user logs in. This is important because my application is working with the windows services. For stooping/starting a windows service admin privileges are required.
Please let me know how can i do it.
You cannot force it to start with Admin priviliges -That's up to the user who runs your application.
However you can make sure that the application runs only in admin mode by adding this to your Manifest file:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Notice that it would pop up a UAC prompt on every start-up.
Goodluck.
Add the following code to your Manifest:
<?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" />
<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>
</asmv1:assembly>

Visual Studio 2010 Setup Project marks application as requiring elevation

EDIT: I would still like an answer to this question if anyone can provide one, however I have decided to split out the non-administrative part of this project into another project which will call this project as required.
I have a project which manipulates some registry keys under HKLM - which obviously requires elevation. The application knows when it needs this elevation and will re-launch itself with runas to get elevation.
In the development environment this works fine, and the application also runs fine on intended targets in this form.
When I pump the project through a VS2010 setup project and install the project on the target system, the EXE gets marked as requiring elevation and requests it whenever it gets run before the application logic determines if it needs it.
I have searched for an hour or so and found nothing which relates, though I suspect the power of my search terms is insufficiently strong (or I lack the right ones) to find an appropriate result.
How can I stop the setup project from marking the executable as requiring elevation?
Subsequent to the comment below I added a custom manifest to the project with the same result, the manifest looked right to me:
<?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="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
</application>
</compatibility>
</asmv1:assembly>
Here's the output of the project's build (in the obj folder since that's what's being pulled in by the setup project):
http://i.stack.imgur.com/elrQh.png
The outwardly obvious thing is the lack of the UAC shield on the executable. After building the setup project, then installing it via the resultant MSI, the installed executable is like this:
http://i.stack.imgur.com/A33qz.png
Noting the now existing UAC shield.
Before adding the custom manifest, it was set to use a default one (which I suspect has the same content).

Cannot read files from install directory

I have a wpf application which reads from .xaml files to dynamically create views. However, when installed on a non-dev machine, the program says that the program has unauthorized access to the path where the files are stored. We are using relative pathing to read the files. Any suggestions?
Only way you'll have granted access to that Program Files folder is Creating and Embedding an Application Manifest (UAC).
As an example:
Executable: IsUserAdmin.exe
Manifest:IsUserAdmin.exe.manifest
Sample application manifest file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="IsUserAdmin"
type="win32"/>
<description>Description of your application</description>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
But you definitively must go and read the API.

Categories