关于异常System.IO.DirectoryNotFoundException

什么是System.IO.DirectoryNotFoundException

找不到文件或目录的部件时引发的异常。

继承关系

DirectoryNotFoundException

例子

using System;
using System.IO;

class Program
{

    static void Main(string[] args)
    {
        try
        {

            //  Specify a directory name that does not exist for this demo.
            string dir = @"c:78fe9lk";

            // If this directory does not exist, a DirectoryNotFoundException is thrown
            // when attempting to set the current directory.
            Directory.SetCurrentDirectory(dir);
        }
        catch (DirectoryNotFoundException dirEx)
        {
            // Let the user know that the directory did not exist.
            Console.WriteLine("Directory not found: " + dirEx.Message);
        }
    }
}

说明

DirectoryNotFoundException 使用值为0x80070003 的 HRESULT COR_E_DIRECTORYNOTFOUND。 请注意, DirectoryNotFoundException 当 COM 互操作程序看到 HRESULT STG_E_PATHNOTFOUND (其值为0x80030003)时,也会引发此异常。

如果你的代码没有 PathDiscovery 权限,则此异常的错误消息可能只包含文件或目录名称,而不是完全限定的路径。

原文地址:https://www.cnblogs.com/yilang/p/13695135.html