Posts

Showing posts from June, 2009

Free Icon Collection

FAM FAM FAM - Very good icon collection.

Case insensitive List Contains method

privatestaticbool Contains(List<string> list, stringvalue)
{
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 IronPythonpublicstaticvoid 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);
}

publicstaticvoid 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);
}

publicstaticvoid TestPython3()
{
DataTable dt = new DataTable("test");
dt.Columns.Add("One", typeof(int));
dt.Columns.Add("Two", typeof(int));
dt.Columns.Add("…

C# Open process and capture standard output

[Test]
publicvoid 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

publicvoid 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

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
UNIONSELECT 2
UNIONSELECT 3

INSERT INTO @Table2
SELECT 1
UNIONSELECT 2
UNIONSELECT 4

SELECT * FROM @Table1

DECLARE @BatchSize int
SET @BatchSize = 10

WHILE (Exists(SELECT 1 FROM @Table1 t1 LEFTJOIN @Table2 t2 ON t1.Id = t2.Id WHERE t2.Id ISNULL ))
BEGIN TRY
BEGINTRAN
PRINT'Deleting'
DELETETOP(@BatchSize) @Table1 FROM @Table1 t1 LEFTJOIN @Table2 t2 ON t1.Id = t2.Id WHERE t2.Id ISNULL
COMMITTRAN
END TRY
BEGIN CATCH
ROLLBACK
END CATCH

SELECT * FROM @Table1