当前位置: 首页>>代码示例>>C#>>正文


C# DirectoryInfo类代码示例

本文整理汇总了C#中System.IO.DirectoryInfo的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo类的具体用法?C# DirectoryInfo怎么用?C# DirectoryInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


DirectoryInfo类属于System.IO命名空间,在下文中一共展示了DirectoryInfo类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
        try 
        {
            // Determine whether the directory exists.
            if (di.Exists) 
            {
                // Indicate that the directory already exists.
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            di.Create();
            Console.WriteLine("The directory was created successfully.");

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:35,代码来源:DirectoryInfo

示例2: CopyAll

//引入命名空间
using System;
using System.IO;

class CopyDir
{
    public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        if (source.FullName.ToLower() == target.FullName.ToLower())
        {
            return;
        }

        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        // Copy each file into it's new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }

    public static void Main()
    {
        string sourceDirectory = @"c:\sourceDirectory";
        string targetDirectory = @"c:\targetDirectory";

        DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
        DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

        CopyAll(diSource, diTarget);
    }

    // Output will vary based on the contents of the source directory.
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:48,代码来源:DirectoryInfo

示例3: new DirectoryInfo(String path)

//引入命名空间
using System;
using System.IO;

public class WildcardTest {

    private static void Main(string[] args) {

        DirectoryInfo dir = new DirectoryInfo("c:\\");
        FileInfo[] files = dir.GetFiles("*");
            
        foreach (FileInfo file in files) {
            Console.Write("Name: " + file.Name + "  ");
            Console.WriteLine("Size: " + file.Length.ToString());
        }
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:17,代码来源:DirectoryInfo


注:本文中的System.IO.DirectoryInfo类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。