Skip to main content
 首页 » 编程设计

c#之调用 DirectoryEntry() 后发生 System.IO.FileNotFoundException

2024年02月23日14think

我正在调试我刚刚编写的一个小型 C# 应用程序。该应用程序似乎在我的开发箱上运行良好,但在其他机器上运行时会抛出以下错误(所有机器都是安装了 .net 4 的 Windows 7 pro x64):

    Application: WolApp.exe 
Framework Version: v4.0.30319 
Description: The process was terminated due to an unhandled exception. 
Exception Info: System.IO.FileNotFoundException 
Stack: 
   at WolApp.PowerModel..ctor() 
   at WolApp.PowerController..ctor() 
   at WolApp.Program.Main() 

第二个错误来自 kernelbase.dll,但是,一次一件事......

从堆栈跟踪来看,未处理的异常似乎被扔进了 PowerModel 构造函数内部,它仅包含以下内容:

public PowerModel() { 
entry = new DirectoryEntry(); 
} 

其中 entry 是 PowerModel 类的一个字段。我已经添加了以下 try catch block

 public PowerModel() { 
            try 
            { 
                entry = new DirectoryEntry(); 
            } 
            catch (System.IO.FileNotFoundException e) 
            { 
                string sSource; 
                string sLog; 
                string sEvent; 
 
                sSource = "WolApp"; 
                sLog = "Application"; 
                sEvent = e.Message; 
                EventLog.CreateEventSource(sSource,sLog); 
                EventLog.WriteEntry(sSource, sEvent); 
             } 
        } 

但仍然抛出相同的异常(并且仅在外部机器上)?

此外,Windows 错误报告的日志中还会显示一条附加消息,如下所示:

 1745074215  
   5  
   CLR20r3  
   Not available  
   0  
   wolapp.exe  
   1.0.0.0  
   4d657be0  
   WolApp  
   1.0.0.0  
   4d657be0  
   3  
   26  
   System.IO.FileNotFoundException  
 
   C:\Users\username\AppData\Local\Temp\WERC3EF.tmp.WERInternalMetadata.xml  
   C:\Users\username\AppData\Local\Microsoft\Windows\WER\ReportArchive\AppCrash_wolapp.exe_62bdee217c82fa90fcdaeb137c1659c897785b85_20036868  
 
   0  
   ce7dc05c-3f93-11e0-8907-0024e82e58f8  
   0  

请您参考如下方法:

DirectoryEntry是来自 System.DirectoryServices 的一个类命名空间并处理 Active Directory 中的条目。

它与磁盘目录中的条目没有任何关系......我最惊讶的是你会得到 FileNotFoundExeption有了这个……

(不适用) <罢工> 或者,如果它 不是 System.DirectoryServices.DirectoryEntry你在说什么,那么为这个类选择一个 极其的类名是错误的!