En el pasado, algunas aplicaciones leían y escribían archivos de configuración ini en lugares no aconsejables según el estado de seguridad actual y el sistema de operación de microsoft windows (os) estaba bastante contento. En Windows 7, 8 y 10, el sistema operativo protege esas carpetas y almacena la nueva versión en el perfil de usuario VirtualStore.
El C# y el código de VB.net debajo de verificación si existe el archivo en la ruta VirtualStore (es decir, "C: \ Users \\ AppData \ Local \ VirtualStore \ Archivos de programa (x86) \ App de ejemplo") y si no existe, regístrese en el lugar original ("C: \ Archivos de programa (x86) \ Aplicación de ejemplo"). CheckFile() devolverá la ruta completa del archivo.
FullFilePath = checkfile ("C: \ Archivos de programa (x86) \ Ejemplo App", "Data.ini"));
También funciona con otras carpetas (como "C: \ Windows") que su código heredado puede tratar de ensuciar.
A continuación se muestra el código 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;
}
}
A continuación se muestra el código de 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
Prueba este http: // stackoverflow.com/questions/867485/c-sharp-getting-the-path-of-appdata – djserva
Puede acceder al archivo como si todavía estuviera en 'C: \ Archivos de programa (x86) \ Ejemplo de aplicación \ data.ini'. Windows File Virtualization hará el trabajo por usted. Consulte esto para obtener más información sobre Virtualización de archivos http://www.codeproject.com/Articles/66275/Windows-Vista-File-and-Registry-Virtualization –
Consulte mi pregunta actualizada – user525717