gpt4 book ai didi

c# - 获取存储在 VirtualStore 中的日志文件的真实路径

转载 作者:行者123 更新时间:2023-11-30 14:11:19 24 4
gpt4 key购买 nike

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

日志文件位于:

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

C#认为它在这里:

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

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

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

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

最佳答案

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

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

来自Developing for Windows blog :

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.

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.

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 错误。

您可以通过向应用程序的程序集 list 添加一个条目来完成此操作,告诉 Windows 您的应用程序已正确编写:

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% 子树的尝试都会正确地失败。

正确编写的 Windows 应用程序不会将数据存储在 Program Files 中。来自 Technical requirements for the Windows 7 Client Software Logo Program , 第 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 provides specific locations in the file system to store programs and software components, shared application data, and application data specific to a user:

  • Applications should be installed to the Program Files folder by default. User data or application data must never be stored in this location because of the security permissions configured for this folder (emphasis added)
  • All application data that must be shared among users on the computer should be stored within ProgramData
  • All application data exclusive to a specific user and not to be shared with other users of the computer must be stored in Users\<username>\AppData
  • Never write directly to the "Windows" directory and or subdirectories. Use the correct methods for installing files, such as fonts or drivers
  • In “per-machine” installations, user data must be written at first run and not during the installation. This is because there is no correct user location to store data at time of installation. Attempts by an application to modify default association behaviors at a machine level after installation will be unsuccessful. Instead, defaults must be claimed on a per-user level, which prevents multiple users from overwriting each other's defaults.

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

  • 在每个用户的 LocalAppData 文件夹中(通常解析为 C:\Users\Sugrue\AppData\Local)
  • CommonAppData 文件夹中(通常解析为 C:\ProgramData)

选择权在你。大概您想要一个日志文件,多个用户可以将其添加到其中。在这种情况下,您需要 Common AppData 文件夹。您可以使用 SHGetFolderPath 检索此路径与 CSIDL_COMMON_APPDATA , 或更新的 SHGetKnownFolderPath :

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

用户可以写入此文件夹,因为创建文件和文件夹的权限默认授予用户:

enter image description here

总结

你不能。
但与此同时:你不应该。

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

关于c# - 获取存储在 VirtualStore 中的日志文件的真实路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20570754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com