简体   繁体   English

WPF C# StreamWriter 不写入文件并且不引发异常

[英]WPF C# StreamWriter does not write to file and raises no exception

I use this piece of code to write (append) text to a log file in my WPF app.我使用这段代码将文本写入(附加)到我的 WPF 应用程序中的日志文件中。 The file's path is by default inside Program Files.默认情况下,该文件的路径位于 Program Files 中。 If I run the app as normal user, the file is not written (or not created), but no permissions exception is raised, despite of the try-catch statement.如果我以普通用户身份运行应用程序,则不会写入(或未创建)文件,但不会引发权限异常,尽管有try-catch语句。 If the user specifies a non-system folder for the log files (which can be done on runtime), everything works.如果用户为日志文件指定了一个非系统文件夹(可以在运行时完成),那么一切正常。 If the app is run as admin, everything works even with the default (system) folder.如果应用程序以管理员身份运行,则即使使用默认(系统)文件夹也能正常工作。 This is clearly a permissions problem.这显然是一个权限问题。 I don't understand why this code doesn't raise an exception, if the user is non-admin and tries to create/write files inside a system folder.如果用户不是管理员并尝试在系统文件夹中创建/写入文件,我不明白为什么这段代码不会引发异常。 In such case, File.AppendText should raise UnauthorizedAccessException , shouldn't it?在这种情况下, File.AppendText应该引发UnauthorizedAccessException ,不是吗? What is the problem here?这里有什么问题?

try
{
    using (StreamWriter w = File.AppendText(path))
    {
        w.WriteLine(line.ToString());
    }
}
catch (Exception ex)
{
    var message = $"{path}\n{ex.Message}";
    MessageBox.Show(message, "Log File Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

OS: Windows 10操作系统:Windows 10

Net Framework version 4网络框架版本 4

I use this piece of code to write (append) text to a log file in my WPF app.我使用这段代码将文本写入(附加)到我的 WPF 应用程序中的日志文件中。 The file's path is by default inside Program Files.默认情况下,该文件的路径位于 Program Files 中。 If I run the app as normal user, the file is not written (or not created), but no permissions exception is raised, despite of the try-catch statement.如果我以普通用户身份运行应用程序,则不会写入(或未创建)文件,但不会引发权限异常,尽管有 try-catch 语句。

You're hitting UAC virtualization .您正在使用UAC 虚拟化

AsRaymond Chen puts it :正如Raymond Chen 所说

UAC Virtualization kicks in for applications which were designed for versions of Windows prior to Windows Vista. UAC 虚拟化适用于为 Windows Vista 之前的 Windows 版本设计的应用程序。 These applications often assume that they are running with administrative privileges, and when they try to write to file or registry locations that are normally restricted to administrators, they expect them to succeed.这些应用程序通常假定它们以管理权限运行,并且当它们尝试写入通常仅限于管理员的文件或注册表位置时,它们希望它们能够成功。 So UAC Virtualization lets those writes succeed by secretly redirecting them to another location inside the user profile.因此,UAC 虚拟化通过秘密地将它们重定向到用户配置文件中的另一个位置来让这些写入成功。 The reverse happens when the application later tries to read the file or registry key: If the file or key exists in the redirected location, it is used instead of the one in the administrative location.当应用程序稍后尝试读取文件或注册表项时会发生相反的情况:如果文件或注册表项存在于重定向位置,则使用它而不是管理位置中的那个。

You can read the above-linked TechNet article for details .您可以阅读上面链接的 TechNet 文章了解详细信息 The short version is that you can find the files in the %LOCALAPPDATA%\VirtualStore directory and the keys under the HKEY_CURRENT_USER\Software\Classes\VirtualStore key.简短的版本是您可以在%LOCALAPPDATA%\VirtualStore目录中找到文件以及HKEY_CURRENT_USER\Software\Classes\VirtualStore键下的键。

Don't try and write to Program Files.不要尝试写入程序文件。 Write to somewhere suitable in AppData instead, either ApplicationData or CommonApplicationData .改为写入 AppData 中合适的地方, ApplicationDataCommonApplicationData

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

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