简体   繁体   English

在Win7的%TEMP%中找不到文件

[英]File Not Found in %TEMP% on Win7

I have an odd bug where my code returns a file not found exception but the file seems to be exactly where it should be. 我有一个奇怪的错误,我的代码返回了一个文件未找到异常,但该文件似乎恰好在应有的位置。 My project has some code to run a system cmdlet and look for the results of the cmdlet in an XML output file. 我的项目有一些代码来运行系统cmdlet,并在XML输出文件中查找cmdlet的结果。 We tell the cmdlet to put this output XML in a custom subdir of the system TEMP dir, eg, C:\\WINDOWS\\TEMP\\SomeFolder\\output.xml. 我们告诉cmdlet将输出XML放入系统TEMP目录的自定义子目录中,例如C:\\ WINDOWS \\ TEMP \\ SomeFolder \\ output.xml。 We then use the .NET XmlDocument class to open and parse the XML file. 然后,我们使用.NET XmlDocument类打开并解析XML文件。

On WinXP, this works. 在WinXP上,这有效。 On my dev box, this works. 在我的开发箱中,这可行。 On a clean Win7 test machine, it does not. 在干净的Win7测试机上,不是。

My first thought was that I'm running into Vista/Win7 File Virtualization, but our application manifest specifies that our app run as Admin -- and from what I've read, that should bypass file virtualization. 我的第一个想法是,我正在运行Vista / Win7文件虚拟化,但是我们的应用程序清单指定我们的应用程序以Admin身份运行-从我读到的内容来看,应该绕过文件虚拟化。

The other wrinkle is that our code likes to use UNC file paths, even if the file is local to the machine. 另一个难题是,即使文件在计算机本地,我们的代码也喜欢使用UNC文件路径。 (We have a requirement that the code in question may need to run the cmdlet on a remote machine, and therefore the output XML could be on a remote machine too.) So we try to open the XML file via \\MATT-WIN7\\C$\\WINDOWS\\TEMP\\SomeFolder.xml rather than C:\\WINDOWS\\TEMP\\SomeFolder\\output.xml. (我们要求有问题的代码可能需要在远程计算机上运行cmdlet,因此输出XML也可以在远程计算机上。)因此,我们尝试通过\\ MATT-WIN7 \\ C打开XML文件。 $ \\ WINDOWS \\ TEMP \\ SomeFolder.xml而不是C:\\ WINDOWS \\ TEMP \\ SomeFolder \\ output.xml。

But I removed the UNC path code temporarily and a simple call to File.Exists() still says the XML file is not there, when Windows Explorer shows the file sitting exactly where I think it should be. 但是我暂时删除了UNC路径代码,并且对简单的File.Exists()调用仍然表示XML文件不存在,因为Windows资源管理器显示该文件恰好位于我认为应该的位置。

Is there some nuance of file virtualization that I have not read about yet? 我还没有读过文件虚拟化的细微差别吗?

My workaround is to move the output xml file somewhere else, but that will potentially break the "portability" of our code when it needs to run on a remote machine, because using the %TEMP% location is a location that can be resolved for remote computers pretty easily (via remote registry call to find the system environment variable). 我的解决方法是将输出xml文件移动到其他位置,但是当它需要在远程计算机上运行时,这可能会破坏代码的“可移植性”,因为使用%TEMP%位置是可以解析为远程位置的位置。计算机非常容易(通过远程注册表调用来查找系统环境变量)。

I would prefer to leave the file where it is, and fix our code so it actually finds the file! 我希望将文件保留在原处,并修复我们的代码,以便它实际上可以找到该文件!

There is a user-specific override for the %TEMP% environment variable that points to %USERPROFILE%\\AppData\\Local\\Temp , not to %SYSTEMROOT%\\Temp . %TEMP%环境变量有一个用户特定的替代,它指向%USERPROFILE%\\AppData\\Local\\Temp ,而不是%SYSTEMROOT%\\Temp Make sure your code is looking in the temp folder you expect it to look at. 确保您的代码正在您希望其查看的temp文件夹中。

Update : Based on your comments, it seems that the problem is that your app is not actually being elevated on the test machine, but is elevated on your dev machine. 更新 :根据您的评论,看来问题是您的应用程序实际上并未在测试计算机上提升,但在开发计算机上却得到提升。 I suspect the following: 我怀疑以下情况:

  • you either have UAC disabled on your dev machine or you are running VS as administrator. 您要么在开发计算机上禁用了UAC,要么以管理员身份运行VS。 Big no-no on both. 两者都很大。 :-) :-)
  • your binary is not code-signed and is not in one of the two trusted locations - %SystemRoot%\\system32 or %ProgramFiles%. 您的二进制文件没有代码签名,并且不在两个受信任位置之一-%SystemRoot%\\ system32或%ProgramFiles%中。 For security reasons UAC does not even prompt the user for elevation for apps that have elevation manifest, but are not code-signed or in a trusted location. 出于安全原因,对于具有海拔清单但未进行代码签名或位于受信任位置的应用,UAC甚至不会提示用户进行海拔提升。

You can create a self-signed certificate to code-sign your binary and add that certificate to the test machines, to get the UAC prompt. 您可以创建一个自签名证书来对二进制文件进行代码签名,然后将该证书添加到测试计算机中,以获取UAC提示。 Once you've confirmed that your app is properly being elevated, your code to access the system %TEMP% folder should work correctly. 确认已正确提升您的应用程序之后,用于访问系统%TEMP%文件夹的代码将正确运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM