Open lindexi opened 5 years ago
Another crash case: If the Windows disk is full, the cursor creation will also crash.
If we don't cache the generated Cursor
instances, the temp folder will soon be full and cause the exception above.
Question: what is Switch.System.Windows.AllowExternalProcessToBlockAccessToTemporaryFiles
How to solve it? The evil way is change the temp path.
Environment.SetEnvironmentVariable("TEMP", tempFolder);
Environment.SetEnvironmentVariable("TMP", tempFolder);
The tempFolder
is your custom folder full path. Yes, I fire a new comment just want to tell you should use the full path not a relative path.
Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
Problem description:
The cursor will crash at initialization when the temporary folder full.
Actual behavior:
I set a cursor from a resource and use this code.
I can find the cursor will crash at initialization when the temporary folder full.
Expected behavior:
The cursor can be set.
Minimal repro:
Run this code when the temporary folder full.
And the other way is set a can not visit folder as the temp folder. And you can see the code in https://github.com/lindexi/lindexi_gd/tree/8e346e750fe2075e1366a2a098d63af3b50db177/FawlalnejajerelaWhallgemcurkear that I set a folder name as
D:\lindexi\不存在文件
that is an unauthorized access folder.Reason
The Cursor.LegacyLoadFromStream will generate a temporary file based on the memory stream. But the Path.GetTempFileName Method limit the number of the files is 65535 and it will throw IOException when overcount.
The LoadFromStream code in https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Input/Cursor.cs,090cb505b6310a4e
The LegacyLoadFromStream code in https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Input/Cursor.cs,935d59bd1efe76e4,references
Actual LoadFromStream may ignore LegacyLoadFromStream. But FileHelper.CreateAndOpenTemporaryFile will generate the file in WPF folder by default and use Path.GetRandomFileName to create a file without check access. And it also crashes when the temp folder can not write.
solution
Why set the environment variable is safe? Because all the process can call the
Path.GetTempFileName
that will throw IOException when the other process uses it to create and do not delete.public Cursor(string cursorFile)
replacepublic Cursor(Stream cursorStream)
that the first will call LoadFromFile and the LoadFromFile will load the file and do not need to copy to temp folder.Reference
WPF 光标初始化的时候 temp 文件夹满了无法创建