- 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
[sourcecode language="CSharp"]
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();
}
}
}
[/sourcecode]
- Source class
[sourcecode language="CSharp"]
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("http://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;
}
}
}
[/sourcecode]
No comments:
Post a Comment