简体   繁体   中英

How to get the AppData VirtualStore path of an application?

I want to get application's path in VirtualStore .

For example, the file which I need is in this directory (I'm getting this path from registry)

C:\\Program Files (x86)\\Example App\\data.ini

How can I get this path?

C:\\Users\\User388\\AppData\\Local\\VirtualStore\\Program Files (x86)\\Example App\\data.ini

UPDATE:

This paths in not my application.

I asked how it possible to get path in app data when only know winodows username and path in program files

Assuming that Example App is the application running the code the first directory is retrieved using

string strFilePath = Path.Combine(Application.ExecutablePath, "Data.ini");

The second doesn't at first glance look like a set location, but for this you can experiment with the Application and Environment classes. Try something like

string strFilePath = Path.Combine(Application.UserAppDataPath, "Data.ini");

I hope this helps.

Edit: See this link https://stackoverflow.com/a/3916868/626442 for your answer.

In the past, some applications read and write ini configuration files in not advisable places by current security status and the microsoft windows operation system (os) was quite happy. In windows 7, 8 and 10, the os protects those folders, storing the new version under the user profile VirtualStore.

The C# and the VB.net code below check if the file exists in the VirtualStore path (ie "C:\\Users\\\\AppData\\Local\\VirtualStore\\Program Files (x86)\\Example App") and if it don't exist, check in the original place ("C:\\Program Files (x86)\\Example App"). CheckFile() will return the full file path of the file.

FullFilePath = CheckFile("C:\\Program Files (x86)\\Example App", "data.ini"));

It also work with other folders (like "C:\\Windows") that your legacy code may try to mess.

Below is the C# code:

public void Main()
{
    string Path = "";
    string File = "data.ini";
    string FullFilePath = "";

    // we can get
    Path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    //Path = "C:\Program Files (x86)\Example App"
    FullFilePath = CheckFile(Path, File);

    Interaction.MsgBox("FullFilePath: " + FullFilePath, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Example Debug");
}

/// <summary>
/// CheckFile() - Check the String with Path and Filename. Make sure path is padded to the right with a \
/// </summary>
/// <param name="FilePath">Path of the file</param>
/// <param name="FileName">File name</param>
/// <returns>String with Path and Filename</returns>
/// <remarks>
/// Support the file search in user VirtualStore first and original path later.
/// </remarks>
public string CheckFile(string FilePath, string FileName)
{
    string OriginalPath = "";
    string VirtualStorePath = "";

    // Make sure path is padded to the right with a \
    if (FilePath.EndsWith("\\")) {
        OriginalPath = FilePath;
    } else {
        OriginalPath = FilePath + "\\";
    }

    VirtualStorePath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\VirtualStore\\" + OriginalPath.Substring(3);
    //MsgBox("VirtualStorePath: " & VirtualStorePath & vbNewLine & "OriginalPath: " & OriginalPath,
    //       MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "AIMS Debug")

    // return first VirtualStorePath if the file exists in user VirtualStore
    if (IO.File.Exists(VirtualStorePath + FileName)) {
        FilePath = VirtualStorePath;
        return VirtualStorePath + FileName;
    }
    if (IO.File.Exists(OriginalPath + FileName)) {
        return OriginalPath + FileName;
    } else {
        Interaction.MsgBox("No file in CheckFile(FilePath: " + FilePath + Constants.vbNewLine + "FileName: " + FileName + ")", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Example Debug");
        // we don't have this file
        return OriginalPath + FileName;
    }
}

Below is the VB.net code:

Sub Main()
        Dim Path As String = ""
        Dim File As String = "data.ini"
        Dim FullFilePath As String = ""

        ' we can get
        Path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
        'Path = "C:\Program Files (x86)\Example App"
        FullFilePath = CheckFile(Path, File)

        MsgBox("FullFilePath: " & FullFilePath, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Example Debug")
    End Sub

    ''' <summary>
    ''' CheckFile() - Check the String with Path and Filename. Make sure path is padded to the right with a \
    ''' </summary>
    ''' <param name="FilePath">Path of the file</param>
    ''' <param name="FileName">File name</param>
    ''' <returns>String with Path and Filename</returns>
    ''' <remarks>
    ''' Support the file search in user VirtualStore first and original path later.
    ''' </remarks>
    Function CheckFile(ByVal FilePath As String, ByVal FileName As String) As String
        Dim OriginalPath As String = ""
        Dim VirtualStorePath As String = ""

        ' Make sure path is padded to the right with a \
        If FilePath.EndsWith("\") Then
            OriginalPath = FilePath
        Else
            OriginalPath = FilePath & "\"
        End If

        VirtualStorePath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\VirtualStore\" & OriginalPath.Substring(3)
        'MsgBox("VirtualStorePath: " & VirtualStorePath & vbNewLine & "OriginalPath: " & OriginalPath,
        '       MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "AIMS Debug")

        ' return first VirtualStorePath if the file exists in user VirtualStore
        If IO.File.Exists(VirtualStorePath & FileName) Then
            FilePath = VirtualStorePath
            Return VirtualStorePath & FileName
        End If
        If IO.File.Exists(OriginalPath & FileName) Then
            Return OriginalPath & FileName
        Else
            MsgBox("No file in CheckFile(FilePath: " & FilePath & vbNewLine & "FileName: " & FileName & ")",
                   MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Example Debug")
            ' we don't have this file
            Return OriginalPath & FileName
        End If
    End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM