首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得应用程序的AppData VirtualStore路径?

如何获得应用程序的AppData VirtualStore路径?
EN

Stack Overflow用户
提问于 2012-07-04 16:03:28
回答 2查看 8.2K关注 0票数 6

我想在VirtualStore中获得应用程序的路径。

例如,我需要的文件位于这个目录中(我从注册表中获得了这个路径)

C:\程序文件(X86)\示例App\data.ini

我怎么走这条路?

C:\Users\User388\AppData\Local\VirtualStore\Program文件(X86)\示例App\data.ini

更新:

这不是我的应用程序中的路径。

当我只知道winodow用户名和程序文件中的路径时,我询问了如何在app数据中获取路径。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-04 16:14:26

假设Example App是运行代码的应用程序,则使用

代码语言:javascript
复制
string strFilePath = Path.Combine(Application.ExecutablePath, "Data.ini");

乍一看,第二个位置并不像一个设置的位置,但是对于这个位置,您可以尝试使用ApplicationEnvironment类。试着做些像

代码语言:javascript
复制
string strFilePath = Path.Combine(Application.UserAppDataPath, "Data.ini");

我希望这能帮到你。

编辑:有关您的答案,请参阅此链接https://stackoverflow.com/a/3916868/626442

票数 2
EN

Stack Overflow用户

发布于 2017-08-11 08:47:05

过去,一些应用程序根据当前的安全状态在不可取的地方读写ini配置文件,microsoft windows操作系统(os)非常高兴。在windows 7、8和10中,操作系统保护这些文件夹,将新版本存储在用户配置文件VirtualStore下。

下面的C#和VB.net代码检查文件是否存在于VirtualStore路径中(即“C:\User\AppData\\Program(X86)\示例应用程序”,如果它不存在,请检查原始位置(“C:\程序文件(X86)\示例应用程序”)。CheckFile()将返回文件的完整文件路径。

FullFilePath = CheckFile("C:\Program (X86)示例应用程序“、”data.ini“);

它也适用于其他文件夹(如"C:\Windows"),您的遗留代码可能会试图破坏这些文件夹。

下面是C#代码:

代码语言:javascript
复制
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;
    }
}

下面是VB.net代码:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11332431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档