tags:

views:

55

answers:

3

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.

+3  A: 

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.

Ruel
But I cant tell user to run the software in admin mode. And can I give user full control programmaticaly ?
Barun
I wouldn't recommend that, It's just a solution but not a good one. All I can suggest is force UAC elevation: http://community.bartdesmet.net/blogs/bart/archive/2006/10/28/Windows-Vista-_2D00_-Demand-UAC-elevation-for-an-application-by-adding-a-manifest-using-mt.exe.aspx
Ruel
But what is the problem ? why vista not giving me access. I am not doing any illegal job. just writing a file to my application folder. What is the wrong with that ?
Barun
Because only `SYSTEM` and `Administrator` user groups can modify/create stuff to that folder.
Ruel
No I dont thing you are right. This is my own application folder in System.Environment.SpecialFolder.ApplicationData, and as I know I can use that without any admin privilege. I already doing file creation with main thread. But it not giving access to another sub thread.
Barun
`Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)` returns: `C:\Users\Username\AppData\Roaming`. You might need to create another directory inside that.
Ruel
Yes I created my own Directory.
Barun
+2  A: 

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"&gt;
  <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

djechelon
Yes I am getting the path exactly using that method. But my main thread is not creating the log. I used another thread to creating the log.
Barun
It's not a thread problem. If you program has no write privileges, no thread will succeed. You might question about **what** special folders, rather than the temporary, are writable
djechelon
But when I using main thread it is working perfectly.
Barun
Does your main thread own a file handler? I mean, did you close your file from the main thread or didn't open it at all before opening it from the secondary thread? Did you just do new Thread().Start() to start your secondary thread or did you do something fancy that might have affected your spawned thread's security context?
djechelon
I dont think so. This is a fresh log file named as log_[current date and time] which is the sub thread creating. So no open-close issue. But when I using main thread to creating file this is not automated. Means user clicking a button to generate it. But for sub thread it is going to do it automatically. I am so confused and dont have a clue.
Barun
+1  A: 

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.");
    }
}
Tergiver
not installation folder. it is operating system considers the "application data folder"
Barun