获取存储在 VirtualStore 中的日志文件的真实路径 [英] Get real path of a log file stored in VirtualStore

查看:27
本文介绍了获取存储在 VirtualStore 中的日志文件的真实路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将日志文件存储在一个位置,根据管理员设置,该位置可以重定向到 VirtualStore 中的文件夹.例如,它们有时最终会出现:

My application stores log files in a location which, depending on admin settings, can get redirected to a folder in the VirtualStore. They sometimes end up in, for example:

日志文件在:

C:\Users\-my username-\AppData\Local\VirtualStore\Program Files (x86)\ *my-application* \logs

C# 认为它在这里:

C:\Program Files (x86)\ my-application \logs

C:\Program Files (x86)\ my-application \logs

这只是部分代码的问题 - 一个试图在记事本中打开日志文件的按钮.它运行 Process.Start( path-where-application-thinks-log-files-are );

This is only a problem in one part of the code - a button which tries to open the log file in notepad. It runs Process.Start( path-where-application-thinks-log-files-are );

如果我使用 File.Exists( path-where-application-thinks-log-files-are );我是真的 - 因为 c# 知道查看 VirtualStore 位置.但是当我尝试启动该文件时,它失败了.

If I test this using File.Exists( path-where-application-thinks-log-files-are ); I get true - because c# knows to look in the VirtualStore location. But when I try to launch the file, it fails.

所以我的问题是,从 Process.Start() 命令的角度来看,有没有办法将路径转换为正确的位置?

So my question is, is there a way to convert a path into the correct location, from the point of view of the Process.Start() command?

推荐答案

你的问题的答案是你不能.

The answer to your question is that you cannot.

文件和注册表虚拟化是一种临时兼容性黑客,存在于当前版本的 Windows 中,以便有缺陷的应用程序暂时继续工作.Microsoft 不提供处理重定向文件的功能.执行此操作的应用程序处于错误状态,需要修复.

File and Registry virtualization is a temporary compatibility hack, present in the current version of Windows so that buggy applications will temporarily continue to work. Microsoft provides no capability to cope with redirected files. Applications that do it are in a buggy state and need to be fixed.

来自 Windows 开发博客:

今天,许多应用程序仍然设计为将文件写入程序文件、Windows 目录或系统根目录(通常是 C 驱动器)文件夹.

User Account Control Data Redirection

Today, many applications are still designed to write files to the Program Files, Windows directories, or system root (typically the C drive) folders.

虚拟化仅用于协助应用程序与现有程序的兼容性.专为 Microsoft Windows 7 设计的新应用程序不应对敏感系统区域执行写入操作,也不应依赖虚拟化来纠正应用程序的错误行为.始终开发以标准用户权限使用的应用程序,不要依赖以管理员权限运行的应用程序.使用标准用户权限而不是管理员权限测试您的应用.

Virtualization is intended only to assist in application compatibility with existing programs. New applications designed for Microsoft Windows 7 should NOT perform write operations to sensitive system areas, nor should they rely on virtualization to provide redress for incorrect application behavior. Always develop applications for use with standard user privileges and don’t count on the application running under administrator privileges. Test your application with standard user privileges and not administrator privileges.

如果您正在使用在 Windows 7 之前开发的应用程序体验 UAC 虚拟化,请重新设计您的应用程序以将文件写入适当的位置.

If you are experiencing UAC virtualization with applications developed prior to Windows 7, re-design your applications to write files to the appropriate locations.

解决问题的理想方法是禁用应用程序的文件和注册表虚拟化.这样,您的应用程序将无法再将文件保存到敏感位置 - 并且会收到 Access denied 错误.

The ideal way to solve your problem is to disable File and Registry Virtualization of your application. That way your application will no longer be able to save files to sensitive locations - and will get an Access denied error.

您可以通过在应用程序的程序集清单中添加一个条目来告诉 Windows 您的应用程序编写正确:

You do this by adding an entry to your application's assembly manifest, telling Windows that your application is properly written:

AssemblyManifest.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
   <assemblyIdentity 
      version="1.0.0.0"
      processorArchitecture="X86"
      name="client"
      type="win32"
   /> 

   <description>Sugrue Contoso</description> 

   <!-- Disable file and registry virtualization -->
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges>
            <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
         </requestedPrivileges>
      </security>
   </trustInfo>
</assembly>

这样,任何将日志文件写入 %ProgramFiles% 子树的尝试都将正确失败.

This way any attempts to write a log file to the %ProgramFiles% subtree will correctly fail.

正确编写的 Windows 应用程序不会在 Program Files 中存储数据.来自 Windows 7 客户端软件徽标计划的技术要求, 第 8-9 页:

Correctly written Windows applications do not store data in Program Files. From Technical requirements for the Windows 7 Client Software Logo Program, page 8-9:

用户应该拥有一致且安全的默认体验文件的安装位置,同时保留选项将应用程序安装到他们选择的位置.也是必须将应用程序数据存储在正确的位置以允许几个人使用同一台计算机而不会损坏或覆盖彼此的数据和设置.

Install to the correct folders by default

Users should have a consistent and secure experience with the default installation location of files, while maintaining the option to install an application to the location they choose. It is also necessary to store application data in the correct location to allow several people to use the same computer without corrupting or overwriting each other's data and settings.

Windows 提供了文件系统中的特定位置来存储程序和软件组件、共享的应用程序数据,以及特定于用户的应用程序数据:

Windows provides specific locations in the file system to store programs and software components, shared application data, and application data specific to a user:

  • 默认情况下,应用程序应安装到 Program Files 文件夹中.用户数据或应用程序数据不得存储在此位置,因为为此配置了安全权限文件夹(强调)
  • 必须在计算机上的用户之间共享的所有应用程序数据都应存储在 ProgramData 中
  • 所有特定用户专有且不得与计算机的其他用户共享的应用程序数据必须存储在用户\<用户名>\AppData
  • 切勿直接写入Windows"目录和/或子目录.使用正确的方法安装文件,例如字体或驱动程序
  • 在每台机器"安装中,用户数据必须在首次运行时写入,而不是在安装过程中写入.这是因为没有正确的用户位置以在安装时存储数据.尝试通过应用程序修改机器上的默认关联行为安装后水平将不成功.相反,默认值必须在每个用户级别上声明,这可以防止多个用户覆盖彼此的默认设置.

在您的情况下,应该存储日志文件:

In your case, log files should either be stored:

  • 在每个用户的 LocalAppData 文件夹中(通常解析为 C:\Users\Suglue\AppData\Local)
  • CommonAppData 文件夹中(通常解析为 C:\ProgramData)
  • in the per-user LocalAppData folder (typically resolves to C:\Users\Sugrue\AppData\Local)
  • in the CommonAppData folder (typically resolves to C:\ProgramData)

选择权在你.大概您想要一个多个用户可以添加到的单个日志文件.在这种情况下,您需要 Common AppData 文件夹.您可以使用 <检索此路径code>SHGetFolderPathCSIDL_COMMON_APPDATA,或更新的 SHGetKnownFolderPath:

The choice is yours. Presumably you want a single log file which multiple users can add to. In which case you want the Common AppData folder. You can retrieve this path using SHGetFolderPath with the CSIDL_COMMON_APPDATA, or the newer SHGetKnownFolderPath:

SHGetFolderPath(0, CSIDL_COMMON_APPDATA, SHGFP_TYPE_CURRENT, out path);

用户可以写入此文件夹,因为默认情况下,用户拥有创建文件和文件夹的权限:

Users are able to write to this folder, because rights to create files and folders are there are granted to Users by default:

你不能.
但同时:你不应该.

You can't.
But at the same time: you shouldn't.

考虑如果没有发生重定向会发生什么.在 Windows XP 上以标准用户身份运行时会发生什么?

Consider what happens if there is no redirection happening. What happens when you run as a standard user on Windows XP?

这篇关于获取存储在 VirtualStore 中的日志文件的真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆