Posts

Showing posts from June, 2009

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" )

Asp.Net upload multiple files

http://www.codeproject.com/KB/aspnet/multiuploads.aspx

SQL Delete in batches

This is a sample of how to delete a large chunk of data in batches. This is to avoid putting to much data into transaction log which can cause long rollback in case of failure. DECLARE @Table1 TABLE ( id int ) DECLARE @Table2 TABLE ( id int ) INSERT INTO @Table1 SELECT 1 UNION SELECT 2 UNION SELECT 3 INSERT INTO @Table2 SELECT 1 UNION SELECT 2 UNION SELECT 4 SELECT * FROM @Table1 DECLARE @BatchSize int SET @BatchSize = 10 WHILE ( Exists ( SELECT 1 FROM @Table1 t1 LEFT JOIN @Table2 t2 ON t1.Id = t2.Id WHERE t2.Id IS NULL )) BEGIN TRY BEGIN TRAN PRINT 'Deleting' DELETE TOP (@BatchSize) @Table1 FROM @Table1 t1 LEFT JOIN @Table2 t2 ON t1.Id = t2.Id WHERE t2.Id IS NULL COMMIT TRAN END TRY BEGIN CATCH ROLLBACK END CATCH SELECT * FROM @Table1