主题:CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/***.dll”错误处理[转载]

本文介绍了解决.NET编译过程中遇到的CS0016错误的方法。该错误通常是因为临时目录权限不足导致,解决方案是在Windows目录下的Temp文件夹安全选项卡中为NetWork Service用户分配全部权限。
刚装完.NET环境,在编译时出现了如下错误:
编译器错误信息:CS0016:  未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary  ASP.NET  Files/***/*****.dll”--“拒绝访问。”
错误的处理:  出现CS0016的原因一般是临时目录的权限不够,解决的办法是给Windows目录下的临时文件夹Temp的安全选项卡中加入NetWork  Service用户并赋予全部权限。 
using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using SystemException = System.Exception; [assembly: ExtensionApplication(typeof(MainPluginHost.PluginLoader))] namespace MainPluginHost { public class PluginLoader : IExtensionApplication { // 包含所有需要嵌入的资源 private readonly string[] _embeddedResources = { "BW.dll","bzxl.dll","fgbz.dll","gjl.dll","gtbbz.dll","ktj.dll", "MyCadPlugin.dll","pkbz.dll","pmhz.dll","pmxlg.dll","th.dll","tk.dll", "tyck.dll","tytk.dll","tzcl.dll","wjpm.dll","wjtj.dll","wjxgbz.dll", "xgbz.dll","zs.dll","zw.dll", "System thesaurus.txt", "qrcode.png" }; // 存储提取的资源路径 public static Dictionary<string, string> ExtractedResources { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // 存储已加载的程序集,避免重复加载 private static readonly HashSet<string> LoadedAssemblies = new HashSet<string>(StringComparer.OrdinalIgnoreCase); public void Initialize() { try { LoadEmbeddedResources(); } catch (SystemException ex) { // 静默处理错误,不显示日志 // 可以在这里记录到文件,但不显示给用户 LogToFile($"PluginLoader初始化失败: {ex.Message}"); } } public void Terminate() { // 清理临时文件 foreach (var path in ExtractedResources.Values) { try { File.Delete(path); } catch { /* 忽略清理错误 */ } } ExtractedResources.Clear(); LoadedAssemblies.Clear(); } private void LoadEmbeddedResources() { var availableResources = Assembly.GetExecutingAssembly().GetManifestResourceNames(); foreach (string resourceName in _embeddedResources) { try { string fullResourcePath = FindMatchingResource(availableResources, resourceName); if (fullResourcePath == null) { // 静默跳过未找到的资源 continue; } // 根据文件类型处理 string extension = Path.GetExtension(resourceName).ToLower(); switch (extension) { case ".dll": // 检查是否已经加载过该程序集 string assemblyName = Path.GetFileNameWithoutExtension(resourceName); if (!LoadedAssemblies.Contains(assemblyName)) { LoadAssemblySafely(fullResourcePath, resourceName); LoadedAssemblies.Add(assemblyName); } break; case ".txt": case ".png": default: // 提取到文件系统 string tempPath = ExtractToTempFile(fullResourcePath, resourceName); ExtractedResources[resourceName] = tempPath; break; } } catch (SystemException ex) { // 静默处理单个资源加载失败,不显示警告对话框 LogToFile($"处理资源失败 {resourceName}: {ex.Message}"); } } } // 安全加载程序集,捕获重复命令注册异常 private void LoadAssemblySafely(string resourceName, string originalName) { try { byte[] assemblyData = ExtractResource(resourceName); Assembly.Load(assemblyData); } catch (SystemException ex) { // 如果是重复命令错误,静默处理 if (IsDuplicateCommandException(ex)) { // 静默跳过,不记录日志 return; } // 对于其他类型的异常,记录到文件但不显示给用户 LogToFile($"加载程序集失败 {originalName}: {ex.Message}"); } } // 检查是否是重复命令注册异常 private bool IsDuplicateCommandException(SystemException ex) { // 检查异常类型和消息特征 if (ex is Autodesk.AutoCAD.Runtime.Exception acadEx) { return true; // AutoCAD异常,很可能是重复命令 } // 检查异常消息是否包含重复命令的特征 string message = ex.Message.ToLower(); return message.Contains("duplicatekey") || message.Contains("duplicate") || message.Contains("command") || message.Contains("already exists"); } // 提取资源到临时文件 private string ExtractToTempFile(string resourceName, string originalName) { // 创建临时目录 string tempDir = Path.Combine(Path.GetTempPath(), "MyCadPlugin"); Directory.CreateDirectory(tempDir); // 保持原始文件名 string fileName = Path.GetFileName(originalName); string tempPath = Path.Combine(tempDir, fileName); // 避免文件锁定冲突 if (File.Exists(tempPath)) { try { File.Delete(tempPath); } catch { /* 忽略删除失败 */ } } // 写入文件 File.WriteAllBytes(tempPath, ExtractResource(resourceName)); return tempPath; } private string FindMatchingResource(string[] allResources, string resourceName) { // 1. 精确匹配(区分大小写) string exactMatch = allResources.FirstOrDefault(r => r.EndsWith(resourceName, StringComparison.Ordinal)); if (exactMatch != null) return exactMatch; // 2. 忽略大小写匹配 string ignoreCaseMatch = allResources.FirstOrDefault(r => r.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase)); if (ignoreCaseMatch != null) return ignoreCaseMatch; // 3. 使用文件名匹配(忽略路径差异) string fileNameOnly = Path.GetFileName(resourceName); string fileNameMatch = allResources.FirstOrDefault(r => r.EndsWith(fileNameOnly, StringComparison.OrdinalIgnoreCase)); return fileNameMatch; } private byte[] ExtractResource(string resourceName) { using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { if (stream == null) { throw new FileNotFoundException($"资源流为空: {resourceName}"); } byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); return buffer; } } // 静默记录到文件(可选) private static void LogToFile(string message) { try { string logPath = Path.Combine(Path.GetTempPath(), "MyCadPlugin", "plugin_log.txt"); Directory.CreateDirectory(Path.GetDirectoryName(logPath)); File.AppendAllText(logPath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}\n"); } catch { // 忽略日志记录失败 } } } // 资源访问器示例 public static class ResourceAccessor { // 获取文本资源内容 public static string GetThesaurusText() { if (PluginLoader.ExtractedResources.TryGetValue("System thesaurus.txt", out string path)) { return File.ReadAllText(path); } throw new FileNotFoundException("词典文件未加载"); } // 获取图像资源路径(可直接用于AutoCAD界面) public static string GetQrCodeImagePath() { if (PluginLoader.ExtractedResources.TryGetValue("qrcode.png", out string path)) { return path; } throw new FileNotFoundException("二维码图像未加载"); } } } 严重性 代码 说明 行 详细信息 错误(活动) CS0246 未能找到类型或命名空间名“ExtensionApplicationAttribute”(是否缺少 using 指令或程序集引用?) 9 错误(活动) CS0246 未能找到类型或命名空间名“IExtensionApplication”(是否缺少 using 指令或程序集引用?) 13 错误(活动) CS0246 未能找到类型或命名空间名“ExtensionApplication”(是否缺少 using 指令或程序集引用?) 9 错误(活动) CS0246 未能找到类型或命名空间名“Autodesk”(是否缺少 using 指令或程序集引用?) 1 错误(活动) CS0246 未能找到类型或命名空间名“Autodesk”(是否缺少 using 指令或程序集引用?) 133
最新发布
10-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值