首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

C#:如何检查是否可以读取和/或删除目录

在C#中,可以使用System.IO.Directory类来检查目录是否可以读取和/或删除。以下是一些示例代码:

检查是否可以读取目录:

代码语言:csharp
复制
string directoryPath = @"C:\example\directory";
if (Directory.Exists(directoryPath))
{
    try
    {
        string[] files = Directory.GetFiles(directoryPath);
        Console.WriteLine("Directory can be read.");
    }
    catch (UnauthorizedAccessException)
    {
        Console.WriteLine("Directory cannot be read.");
    }
}
else
{
    Console.WriteLine("Directory does not exist.");
}

检查是否可以删除目录:

代码语言:csharp
复制
string directoryPath = @"C:\example\directory";
if (Directory.Exists(directoryPath))
{
    try
    {
        Directory.Delete(directoryPath);
        Console.WriteLine("Directory can be deleted.");
    }
    catch (UnauthorizedAccessException)
    {
        Console.WriteLine("Directory cannot be deleted.");
    }
}
else
{
    Console.WriteLine("Directory does not exist.");
}

请注意,以上代码示例仅适用于C#编程语言,并且需要使用System.IO命名空间。此外,在尝试读取或删除目录时,请确保您具有适当的权限。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券