C # create a folder

Source: Internet
Author: User

C # Directory class is required for folder operations. Static methods such as creation, deletion, movement, and enumeration are provided. This class cannot be inherited.

 

The following code creates a folder.

?
1234 if
(!Directory.Exists(sPath))
{     Directory.CreateDirectory(sPath);}

 

The following is the sample code of Directory class on msdn.

Http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

 

The following code first checks whether the specified folder exists. If yes, delete it. Otherwise, create it. Next, move the folder, create a file in it, and count the number of files in the folder.

?
12345678910111213141516171819202122232425262728293031323334353637383940414243 using
System;
using
System.IO;
 class
Test
{    public
static void
Main()     {        // Specify the directories you want to manipulate.        string
path = @"c:\MyDir";        string
target = @"c:\TestDir";         try        {            // Determine whether the directory exists.            if
(!Directory.Exists(path))             {                // Create the directory it does not exist.                Directory.CreateDirectory(path);            }             if
(Directory.Exists(target))             {                // Delete the target to ensure it is not there.                Directory.Delete(target,
true);            }             // Move the directory.            Directory.Move(path, target);             // Create a file in the directory.            File.CreateText(target +
@"\myfile.txt");             // Count the files in the target directory.            Console.WriteLine("The number of files in {0} is {1}",                target, Directory.GetFiles(target).Length);        }
        catch
(Exception e)         {            Console.WriteLine("The process failed: {0}", e.ToString());        }
        finally
{}    }}

The following code calculates the folder size.

?
12345678910111213141516171819202122232425262728293031323334353637383940 // The following example calculates the size of a directory// and its subdirectories, if any, and displays the total size// in bytes. using
System;
using
System.IO;
 public
class
ShowDirSize {    public
static long
DirSize(DirectoryInfo d)     {   
        long
Size = 0;            // Add file sizes.        FileInfo[] fis = d.GetFiles();        foreach
(FileInfo fi in
fis)         {     
            Size += fi.Length;   
        }        // Add subdirectory sizes.        DirectoryInfo[] dis = d.GetDirectories();        foreach
(DirectoryInfo di in
dis)         {            Size += DirSize(di);  
        }        return(Size); 
    }    public
static void
Main(string[] args)
    {        if
(args.Length != 1)         {            Console.WriteLine("You must provide a directory argument at the command line.");   
        }
        else        
            DirectoryInfo d =
new DirectoryInfo(args[0]);            long
dsize = DirSize(d);            Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.",
d, dsize);
        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.