【问题标题】:How to get the AppData VirtualStore path of an application?如何获取应用程序的 AppData VirtualStore 路径?
【发布时间】:2012-07-05 03:43:26
【问题描述】:

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

例如,我需要的文件在这个目录下(我从注册表中获取这个路径)

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

我怎样才能得到这个路径?

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

更新:

此路径不在我的应用程序中。

我问只知道程序文件中的winodows用户名和路径时如何获取应用程序数据中的路径

【问题讨论】:

标签: c# .net windows-7


【解决方案1】:

假设Example App 是运行代码的应用程序,则使用检索第一个目录

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

第二个乍一看并不像一个固定的位置,但为此您可以尝试ApplicationEnvironment 类。尝试类似

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

我希望这会有所帮助。

编辑:请参阅此链接https://stackoverflow.com/a/3916868/626442 以获得您的答案。

【讨论】:

    【解决方案2】:

    过去,一些应用程序读写ini配置文件在当前的安全状态下是不可取的,微软windows操作系统(os)是相当满意的。在 Windows 7、8 和 10 中,操作系统会保护这些文件夹,将新版本存储在用户配置文件 VirtualStore 下。

    下面的 C# 和 VB.net 代码检查文件是否存在于 VirtualStore 路径中(即“C:\Users\\AppData\Local\VirtualStore\Program Files (x86)\Example App”),如果不存在'不存在,检查原始位置(“C:\ Program Files(x86)\ Example App”)。 CheckFile() 将返回文件的完整文件路径。

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

    它也适用于您的旧代码可能会尝试弄乱的其他文件夹(例如“C:\Windows”)。

    下面是 C# 代码:

    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代码:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      相关资源
      最近更新 更多