过去,一些应用程序在当前安全状态下不建议的位置读取和写入ini配置文件,而微软Windows操作系统(OS)非常满意。在Windows 7、8和10中,OS保护这些文件夹,将新版本存储在用户配置文件VirtualStore下。
以下C#和VB.net代码检查文件是否存在于VirtualStore路径中(即“C:\Users\\AppData\Local\VirtualStore\Program Files (x86)\Example App”),如果不存在,则在原始位置(“C:\Program Files (x86)\Example App”)中进行检查。CheckFile()将返回文件的完整路径。
它还可以处理其他文件夹(如“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
C:\Program Files (x86)\Example App\data.ini
中一样访问它。Windows文件虚拟化将为您完成此工作。有关文件虚拟化的更多详细信息,请参见http://www.codeproject.com/Articles/66275/Windows-Vista-File-and-Registry-Virtualization。 - Harvey Kwok