2019-5-16-WPF-光标初始化的时候-temp-文件夹满了无法创建

title author date CreateTime categories
WPF 光标初始化的时候 temp 文件夹满了无法创建
lindexi
2019-05-16 19:16:27 +0800
2019-5-16 16:2:30 +0800
WPF

在 WPF 切换光标的时候,如果是通过本地资源的方法传入 stream 的,需要先复制到临时文件夹里面的文件,然后读取文件指针释放文件。如果此时的 temp 文件夹满了,那么复制文件的时候就无法继续了,于是就无法创建完成光标

最近有老师找我说软件无法使用了,我尝试调试他的电脑,发现任何修改光标的代码就无法继续,因为无法创建光标

大概的修改光标的代码是这样写的,从解决方案里面放一个光标文件,设置为资源通过访问解决方案文件 拿到资源

var uri = new Uri("pack://application:,,,/Text.cur");
var resource = Application.GetResourceStream(uri);
Cursor = new Cursor(resource.Stream);

看到的堆栈如下

   在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   在 System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
   在 System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
   在 System.IO.Directory.CreateDirectory(String path)
   在 System.IO.FileHelper.CreateAndOpenTemporaryFile(String& filePath, FileAccess fileAccess, FileOptions fileOptions, String extension, String subFolder)
   在 System.Windows.Input.Cursor.LoadFromStream(Stream cursorStream)
   在 System.Windows.Input.Cursor..ctor(Stream cursorStream, Boolean scaleWithDpi)
   在 System.Windows.Input.Cursor..ctor(Stream cursorStream)
   在 FawlalnejajerelaWhallgemcurkear.MainWindow..ctor() 位置 D:lindexi程序FawlalnejajerelaWhallgemcurkearFawlalnejajerelaWhallgemcurkearMainWindow.xaml.cs:行号 32

通过读源代码,发现在 LoadFromStream 方法里面是这样写的

        private void LoadFromStream(Stream cursorStream)
        {
            string filePath = null;
 
            try
            {
                // Generate a temporary file based on the memory stream.
                // 从 temp 文件夹创建一个文件
                using (FileStream fileStream = FileHelper.CreateAndOpenTemporaryFile(out filePath))
                {
                	// 复制到文件
                    cursorStream.CopyTo(fileStream);
                }
 
                // 从文件里面读取光标
                // create a cursor from the temp file
                _cursorHandle = UnsafeNativeMethods.LoadImageCursor(IntPtr.Zero, filePath,
                    NativeMethods.IMAGE_CURSOR,
                    0, 0,
                    NativeMethods.LR_DEFAULTCOLOR |
                    NativeMethods.LR_LOADFROMFILE |
                    (_scaleWithDpi? NativeMethods.LR_DEFAULTSIZE : 0x0000));
                if (_cursorHandle == null || _cursorHandle.IsInvalid)
                {
                     throw new ArgumentException(SR.Get(SRID.Cursor_InvalidStream));
                }
            }
            finally
            { 
            	// 尝试删除这个文件,因为光标已经读取了
                FileHelper.DeleteTemporaryFile(filePath);
            }
        }

在 FileHelper.CreateAndOpenTemporaryFile 将会读取到一个 temp 文件夹里面的文件,但是如果这个文件无法访问,那么将不能继续

在我的设备上是很难做到让 temp 文件夹无法访问的,但是可以通过通过修改环境变量修改当前进程使用的系统 Temp 文件夹的路径设置一个无法访问的文件夹作为 temp 文件夹

做一个无法访问的文件夹很简单,只需要右击属性安装,去掉用户就可以了

运行代码就会发现提示对路径访问拒绝

System.UnauthorizedAccessException:“对路径“D:lindexi无法访问文件夹WPF”的访问被拒绝。”

可以的解决方法有两个

  1. 通过环境变量修改 temp 文件夹作为程序自己内部的数据文件夹,这和 UWP 的相同,每个程序都可以有自己独立的 temp 文件夹,可以解决有一些逗比软件会更改整个 temp 文件夹或里面某些文件夹的访问权限或有逗比在 temp 文件夹写入了 65535 个文件让其他程序无法写入文件。从微软官方文档 可以知道 temp 文件夹的文件限制。
  2. 只对光标的修改将解决方案里面的文件修改为输出的文件,此时将会调用 LoadFromFile 方法,这个方法是读取文件不需要复制文件,相对性能比较快

上面提供的两个方法,第一个方法除了解决光标的问题,还可以解决其他问题。第二个方法可以提升一点性能,同时两个方法可以一起使用

这个问题提交给微软,欢迎小伙伴点击 Full temporary folder will crash cursor initialization 帮我点赞

通过修改环境变量修改当前进程使用的系统 Temp 文件夹的路径 - walterlv

GetTempFileNameA function (fileapi.h)

Path.GetTempFileName Method (System.IO)

原文地址:https://www.cnblogs.com/lindexi/p/12085706.html