c# – Thao tác với tập tin thư mục


  1. Create class “FileDirc” have some method
  • void AddDir():create folders as structure

  • void AddFile(): add 03 file to CSharp folders as

  • void WriteLog():  create and write xml file as

<?xml version=”1.0″ encoding=”utf-8″ ?>
<log>
<folder>
<size>123468 Bytes</size>
<content>3 Files, 5 Folders</content>
</folder>
</log>

  • void ViewCont ():

  • Using built in attribute make “ViewCont” only run on Debug mode

The anwser:
– Ran class

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace XML
{
    class Program
    {
        static void Main(string[] args)
        {
            FinalTest01 obj = new FinalTest01();
            obj.AddDir();
            obj.AddFile();
            obj.WriteLog();
            obj.ViewCont();
            Console.ReadLine();
        }
    }
}

– Source class

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Xml;

namespace XML
{
    class FinalTest01
    {
        string dir = @"FinalTest";
        long size = 0;
        long dirnumber = 0;
        long filenumber = 0;
        public FinalTest01()
        {
        }

        public void AddDir()
        {
            DirectoryInfo DirInfo = Directory.CreateDirectory("FinalTest");
            DirInfo.CreateSubdirectory("Datas");
            DirInfo.CreateSubdirectory("References");
            DirectoryInfo CodeDirInfo = DirInfo.CreateSubdirectory("Codes");
            CodeDirInfo.CreateSubdirectory("VB");
            CodeDirInfo.CreateSubdirectory("CSharp");
        }

        public void AddFile()
        {
            string CodeDirInfo = "FinalTest\\Codes";
            File.CreateText(CodeDirInfo + "\\CSharp\\Class01.cs").Close();
            File.CreateText(CodeDirInfo + "\\CSharp\\Class02.cs").Close();
            File.CreateText(CodeDirInfo + "\\CSharp\\Class03.cs").Close();

            TextWriter txtWrite = File.AppendText(CodeDirInfo + "\\CSharp\\Class03.cs");

		txtWrite.WriteLine("https://ngotuongdan.wordpress.com");
            for (int i = 0; i < 1024; i++)
            {
                txtWrite.WriteLine("Curent date is: " + DateTime.Now.ToString());
            }

            txtWrite.Flush();
            txtWrite.Close();
        }

        public void WriteLog()
        {
            GetContent();

            XmlTextWriter xmlWriter = new XmlTextWriter("log.xml", Encoding.Default);
            xmlWriter.Formatting = Formatting.Indented;
            xmlWriter.Indentation = 3;

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("log");
            xmlWriter.WriteStartElement("folder");
            xmlWriter.WriteElementString("size", size.ToString());
            xmlWriter.WriteElementString("content", filenumber + " files and " + dirnumber + " folders");
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();

            xmlWriter.Flush();
            xmlWriter.Close();
        }

        [Conditional("DEBUG")]
        public void ViewCont()
        {
            GetContent();
            Console.WriteLine("Folder FinalTest is {0} bytes, contains {1} files and {2} folders !", size, filenumber, dirnumber);
        }

        private void GetContent()
        {
            string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
            FileInfo f;

            foreach (string var in files)
            {
                f = new FileInfo(var);
                if (f.Extension != "")
                    size += f.Length;
            }

            dirnumber = Directory.GetDirectories(dir, "*.*", SearchOption.AllDirectories).Length;
            filenumber = files.Length;
        }
    }
}
Advertisements

Phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s