Skip to content

Instantly share code, notes, and snippets.

@fresky
fresky / CalculateFileSize.cs
Created August 24, 2012 01:41
Calculate File or Folder Size
public double CalculateSize()
{
if(Directory.Exists(Path))
return new DirectoryInfo(Path).GetFiles(Path, SearchOption.AllDirectories).Aggregate<FileInfo, double>(0, (current, file) => current + file.Length);
else if (File.Exists(Path))
return new FileInfo(Path).Length;
return 0;
}
@fresky
fresky / Program.cs
Created August 24, 2012 05:57
WriteXML
XmlWriterSettings xws = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true
};
XDocument doc;
using (XmlWriter xw = XmlWriter.Create(new StreamWriter(@"D: est.xml"), xws))
{
doc = new XDocument(
new XElement("GarbageCanList",
@fresky
fresky / ReadXML.cs
Created August 24, 2012 09:59
Read xml
XDocument doc = XDocument.Load("xml.xml");
foreach (var xElement in doc.Descendants("Root"))
{
foreach (var s in xElement.Elements())
{
Console.WriteLine(string.Format("{0} : {1}", s.Name, s.Value));
}
}
@fresky
fresky / BinaryFormatter.cs
Created August 24, 2012 10:01
Serialize using BinaryFormatter
Example example = new Example();
example.ID = 10;
example.NameList.Add(new NamePair(){Name = "name1", Value = "value1"});
example.NameList.Add(new NamePair() { Name = "name2", Value = "value2" });
Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
Console.WriteLine("Writing Employee Information");
bformatter.Serialize(stream, example);
@fresky
fresky / AssignValue.cs
Created August 24, 2012 10:02
Assign value to itself by ++
int y = 1;
y = y++;
Console.WriteLine(y);
@fresky
fresky / DeleteFileFolder.cs
Created August 31, 2012 05:24
Delete the file and folder even it's read only
private static void deletePath(string path)
{
FileSystemInfo fsi;
if (File.Exists(path))
{
fsi = new FileInfo(path);
}
else if (Directory.Exists(path))
{
fsi = new DirectoryInfo(path);
@fresky
fresky / DirectoryCopy.cs
Created September 7, 2012 05:05
Copy directory in C#
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
@fresky
fresky / ToDictionary.cs
Created September 12, 2012 01:04
ToDictionary
List<People> peoples = new List<People>();
for (int i = 0; i < 10; i++)
{
peoples.Add(new People() { Name = i.ToString() });
}
foreach (var source in peoples.ToDictionary(people => people.Name))
{
Console.WriteLine(source.Key);
@fresky
fresky / CopyDirectory.cs
Created September 17, 2012 08:17
Copy directory in C#
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
//Copy all the files
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
@fresky
fresky / XMLSerialize.cs
Created September 28, 2012 05:20
XML Serialize without namespace and xml declaration
ObjectToSerialize objectToSerialize = new ObjectToSerialize();
StringBuilder sb = new StringBuilder();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlWriter xmlWriter = XmlWriter.Create(new StringWriter(sb), settings);
xmlSerializer.Serialize(xmlWriter, objectToSerialize, ns);
Console.WriteLine(sb.ToString());