2
Answers

NP116 Creating Files

Maha

Maha

16y
1.8k
1

Hi Guys

 

NP116 Creating Files

 

http://www.aspfree.com/c/a/C-Sharp/A-Look-at-C-Sharp-File-and-FileInfo-Classes/1/

 

Following program is in the above website. According to the author program must create 3 files in the first subdirectory (MyFolder1). But program is creating 3 files outside the MyFolder directory.    

 

Please modify the program to create 3 files in the first subdirectory (MyFolder1).

 

Thank you

 

using System;

using System.IO;

 

namespace IOProject

{

  class Class1

  {

    static void Main(string[] args)

    {

      try

      {

        Console.WriteLine("Creating the Directory");

        DirectoryInfo dirInfo = new DirectoryInfo(

        Path.Combine("D:" + Path.DirectorySeparatorChar,"MyFolder"));

        if(dirInfo.Exists == false)

        {

          dirInfo.Create();

          Console.WriteLine("The folder {0} has been created.", dirInfo.FullName);

        }

 

        Console.WriteLine("Creating 3 subdirectories");

        for(int i = 1; i < 4; i++)

        {

          dirInfo.CreateSubdirectory("MyFolder" + i);

        }

 

        Console.WriteLine("Creating 3 files in the first subdirectory");

        for(int i = 1; i < 4; i++)

        {

          FileInfo aFile = new FileInfo(@"D:MyFolderMyFolder1" + "File" + i + ".txt");

          aFile.Create();

          Console.WriteLine("The file {0} has been created at {1} and the file size = {2}",

          aFile.FullName, aFile.CreationTime, aFile.Length);

          Console.WriteLine();

        }

      }

      catch(IOException ex)

      {

        Console.WriteLine(ex.Message);

      }

      finally

      {

        Console.ReadLine();

      }

    }

  }

}

Answers (2)