如何让我的代码在模拟时访问程序集

问题描述

我正在尝试使用 WindowsIdentity.RunImpersonated 读取共享文件夹中的图像。问题是在模拟过程中,我的代码无法访问 System.Drawing.Common 程序集,可能是因为被模拟的用户无法访问它,我该怎么办。

WindowsIdentity.RunImpersonated(safeAccessTokenHandle,() => {
            string[] files = Directory.GetFiles(path,pattern);

            if (files.Length > 0)
            {
                foreach (string file in files)
                {
                    string[] f = file.Split('.');
                    FileStream fstream = new FileStream(file,FileMode.Open);
                    Bitmap bitmap = new Bitmap(fstream);
                    MemoryStream ms = new MemoryStream();
                    bitmap.Save(ms,ImageFormat.Jpeg);
                    byte[] byteImage = ms.ToArray();
                    model.Archive.Add(Convert.ToBase64String(byteImage));
                    fstream.Close();
                }
            }
        });

这是我得到的错误:

FileLoadException:无法加载文件或程序集“System.Drawing.Common,Version=5.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”。访问被拒绝。

解决方法

这对我有用,如果有人遇到这个问题 在 WindowsIdentity.RunImpersonated

之前添加这行代码
Assembly.Load("System.Drawing.Common,Version=5.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51");

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...