Posts

Free Icon Collection

FAM FAM FAM - Very good icon collection.

Case insensitive List Contains method

private static bool Contains(List< string > list, string value ) { bool contains = null != list.Find( delegate ( string str) { return str.ToLower().Equals( value .ToLower()); }); return contains; }

VIM Commands

Replace first character in the line with quote (in the whole file) %s/^/’/ Replace last character in the line with quote (in the whole file) %s/$/’/ Replace more than 1 space with nothing (in the whole file) %s/ \+// : - go to command mode % – apply to the whole file ^ – first character in the line $ – last character in the line s/x/y – substitute x with y

Running IronPython from C#

Download IronPython public static void TestPython() { string code = @"100 * 2 + 4 / 3" ; ScriptEngine engine = Python.CreateEngine(); ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression); int res = source.Execute< int >(); Console.WriteLine(res); } public static void TestPython2() { ScriptEngine engine = Python.CreateEngine(); ScriptRuntime runtime = engine.Runtime; ScriptScope scope = runtime.CreateScope(); string code = @"emp.Salary * 0.3" ; ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression); Employee emp = new Employee(1000, "Bernie" ,1000); scope.SetVariable( "emp" , emp); double res = ( double )source.Execute(scope); Console.WriteLine(res); } public static void TestPython3() { DataTable dt = new DataTable( "test" ); dt.Columns.Add( "One" , typeo...

C# Open process and capture standard output

[Test] public void TestStartProcess() { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = "echo" ; //Open as readonly process.StartInfo.Arguments = "Hello World!" ; process.StartInfo.UseShellExecute = false ; process.StartInfo.RedirectStandardOutput = true ; process.StartInfo.WorkingDirectory = @"c:\temp" ; process.Start(); StreamReader reader = process.StandardOutput; string output = reader.ReadToEnd(); Console.WriteLine(output); }

C# Read file from zip file to string

public void ReadFromZipFile() { string sampleZipFile = @"C:\temp\myzip.zip" ; string result; using (MemoryStream memory = new MemoryStream()) { using (ZipFile zip = ZipFile.Read(sampleZipFile)) { ZipEntry e = zip[ "myfile.txt" ]; e.Extract(memory); } using (StreamReader reader = new StreamReader(memory)) { memory.Seek(0, SeekOrigin.Begin); result = reader.ReadToEnd(); } } Console.WriteLine(result); }

C# Get File Name/Extension

Path.GetFileName( @"c:\temp\myfile.txt" ) Path.GetFileNameWithoutExtension( @"c:\temp\myfile.txt" ) Path.GetExtension( @"c:\temp\myfile.txt" )