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.
Hello,
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.");
}
}