Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / AssignValue.cs
Created August 24, 2012 10:02
Assign value to itself by ++
int y = 1;
y = y++;
Console.WriteLine(y);
@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 / 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 / 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 / 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 / Screen.cs
Created August 14, 2012 02:03
Get screen resolution
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeigth
@fresky
fresky / Extension.cs
Created July 25, 2012 09:08
Extension method for c#
public static class Extention
{
public static bool IsAbstrct(this Type type)
{
return type.IsAbstract;
}
public static bool IsEnumerableType(this Type t)
{
return typeof(IEnumerable<string>).IsAssignableFrom(t);