Monday, May 14, 2012

Quick way to generate RSA key with LinqPad

To generate private RSA key, we can use LinqPad Expression Executing below expression, will generate RSA key in result window.

new System.Security.Cryptography.RSACryptoServiceProvider (1024).ToXmlString (true) 

if needed, press F4 to add reference, and add reference to System.Security.dll

In Powershell
$rsa = New-Object  System.Security.Cryptography.RSACryptoServiceProvider (1024);
public key
$rsa.ToXmlString($false)
private key
$rsa.ToXmlString($true)

Monday, April 9, 2012

Powershell Find large images

Save the below code in red to .psm1 file eg. ImageFilter.psm1 and then issue following command in powershell console: Import-Module .\ImageFilter.psm1

then you can call the GetFilteredImages function, all params are optional

eg. GetFilteredImages -path c:\temp -exportFile test.txt -width 600 -height 600


*********************************************

Add-Type -Assembly System.Drawing


Function GetFilteredImages
{
Param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[String]
$path = (Get-Location -PSProvider FileSystem).ProviderPath
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[String]
$exportFile = "$path\result.csv"
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[Int]
$width = 1000
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[Int]
$height = 500
)
Process
{
Try
{
Get-ChildItem -Path $path -Recurse -Include *.jpg, *.png, *.gif |
ForEach-Object {
$Image = [System.Drawing.Image]::FromFile($_.FullName)
New-Object -TypeName System.Management.Automation.PSObject -Property @{
Name = $_.Name
Length = $_.Length
Width = $Image.Width
Height = $Image.Height
Fullname = $_.Fullname
}| Where-Object { $_.Width -gt $width -and $_.Height -gt $height}
$Image.Dispose()
} | Select-Object Name, Height, Width, Length, FullName| Export-Csv -NoTypeInformation -Path $exportFile
}
Catch
{
write-warning "Error : $($_.Exception.Message)"
}
}
}

Thursday, February 17, 2011

Get single record per group in Foxpro

SELECT f.* from (SELECT country, min(id) as MinId FROM mytable GROUP BY country) as X INNER JOIN mytable as f ON f.country = x.country AND f.id = x.MinId order BY f.country

Monday, January 31, 2011

id of selected RadioButton

To get ID of Selected RadioButton using jQuery (change nameOfRadio to actual radio name)
var id = $("input:radio[name=nameOfRadio ]:checked").attr('id');

Thursday, October 14, 2010

Luhn Checksum in Foxpro

Based on Excel Vba from http://www.excelforum.com/excel-programming/482390-excel-vba-calculating-checksums.html

?IsLuhnChecksumOK("0000000000000000")
Function IsLuhnChecksumOK(Number_String)
Local Digit
Local i
Local N
Local Result
Local SumDigits
Local nDigits
N = 0
SumDigits = 0
nDigits = Len(Number_String)
For i = nDigits To 1 Step -1
Digit = Val(Substr(Number_String, i, 1))
N = N + 1
If Mod(N , 2) = 0
Digit = Digit * 2
Endif
If Digit > 9
Digit = Digit - 9
Endif
SumDigits = SumDigits + Digit
Next i
Result = Mod(SumDigits ,10)
Return Result = 0
Endfunc

Function GetLuhnCheckDigit(Number_To_Check)
Local J As Integer
Local X
X = IsLuhnChecksumOK(Number_To_Check)
If X = False Then
For J = 0 To 9
X = IsLuhnChecksumOK(Bitand(Number_To_Check , J))
If X = True
*'Check digit found
*GetLuhnCheckDigit = J
Return J
Endif
Next J
Else
*'No check digit needed returns -1
Return -1
Endif
Endfunc

Friday, October 1, 2010

NHibernate.Driver.SQLite20Driver Exception

To fix below error, make sure to add reference System.Data.Sqlite and Copy Local = True in the properties window.


FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

* Database was not configured through Database method.

----> NHibernate.HibernateException : Could not create the driver from NHibernate.Driver.SQLite20Driver.
----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> NHibernate.HibernateException : The IDbCommand and IDbConnection implementation in the assembly System.Data.SQLite could not be found. Ensure that the assembly System.Data.SQLite is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use element in the application configuration file to specify the full name of the assembly.

Tuesday, July 27, 2010

Excel last row column

lnLastRow = loExcel.activesheet.UsedRange.ROWS.COUNT
lnLastCol = loExcel.activesheet.UsedRange.COLUMNS.COUNT