Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect the location of AppData\LocalLow

Tags:

c#

.net

I'm trying to locate the path for the AppData\LocalLow folder.

I have found an example which uses:

string folder = "c:\users\" + Environment.UserName + @"\appdata\LocalLow";

which for one is tied to c: and to users which seems a bit fragile.

I tried to use

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

but this gives me AppData\Local, and I need LocalLow due to the security constraints the application is running under. It returned blank for my service user as well (at least when attaching to the process).

Any other suggestions?

like image 671
Mikael Svenson Avatar asked Dec 20 '10 21:12

Mikael Svenson


People also ask

Where is LocalLow located?

LocalLow is the same folder as local, but it has a lower integrity level. For example, Internet Explorer 8 can only write to the LocalLow folder (when protected mode is on).

How do I open AppData LocalLow?

To access it, one has to select “Show hidden files and folders” in the folder options. When you open the AppData folder, you will see three folders: Local. LocalLow.

Can I delete AppData LocalLow?

Can I Delete the Local, LocalLow, and Roaming Folders? Yes, but no. Deleting these folders can not only possibly break Windows, but they will also indeed remove most of your program settings. Since these folders are protected, you'd have to boot into safe mode, then delete them and immediately regret it.


1 Answers

The Environment.SpecialFolder enumeration maps to CSIDL, but there is no CSIDL for the LocalLow folder. So you have to use the KNOWNFOLDERID instead, with the SHGetKnownFolderPath API:

void Main()
{
    Guid localLowId = new Guid("A520A1A4-1780-4FF6-BD18-167343C5AF16");
    GetKnownFolderPath(localLowId).Dump();
}

string GetKnownFolderPath(Guid knownFolderId)
{
    IntPtr pszPath = IntPtr.Zero;
    try
    {
        int hr = SHGetKnownFolderPath(knownFolderId, 0, IntPtr.Zero, out pszPath);
        if (hr >= 0)
            return Marshal.PtrToStringAuto(pszPath);
        throw Marshal.GetExceptionForHR(hr);
    }
    finally
    {
        if (pszPath != IntPtr.Zero)
            Marshal.FreeCoTaskMem(pszPath);
    }
}

[DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath( [MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
like image 50
Thomas Levesque Avatar answered Oct 03 '22 09:10

Thomas Levesque