Tuesday, July 12, 2005
C# Genrerate Thumbnail Source Code
{
System.IO.FileInfo info = new System.IO.FileInfo(fileName);
using(Bitmap bitmap1 = new Bitmap(fileName, false))
{
int i= 0;
int TWidth;
int THeight;
if(bitmap1.Width >= 90 && bitmap1.Height >=90 )
{
if(bitmap1.Width > bitmap1.Height )
{
TWidth = 90;
i = bitmap1.Width/90;
THeight = bitmap1.Height / i;
}
else
{
THeight = 90;
i = bitmap1.Height/90;
TWidth = bitmap1.Width / i;
}
}
else
{
TWidth = bitmap1.Width;
THeight = bitmap1.Height;
}
using(System.Drawing.Image image =
System.Drawing.Image.FromFile(fileName))
using(Bitmap bitmap = new Bitmap(image, TWidth, THeight))
{
bitmap.Save(path + "/t__" + info.Name , image.RawFormat);
}
}
}
C# Return a web page in a string
{
string strResponse = "";
try
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("big5");
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encode);
strResponse = streamReader.ReadToEnd();
//-----release the reader and stream resource-----
streamReader.Close();
stream.Close();
}
catch(Exception exp)
{
throw exp;
}
return strResponse;
}
C# Get result of a web page with POST variables
{
string strResponse = "";
try
{
string postData = Param;
byte[] byte1=System.Text.Encoding.Default.GetBytes(postData);
// Set the content type of the data being posted.
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
webRequest.Method = "POST";
webRequest.ContentType="application/x-www-form-urlencoded";
// Set the content length of the string being posted.
webRequest.ContentLength=byte1.Length;
System.IO.Stream newStream=webRequest.GetRequestStream();
newStream.Write(byte1,0,byte1.Length);
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("big5");
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);
strResponse = streamReader.ReadToEnd();
//-----release the reader and stream resource-----
newStream.Close();
streamReader.Close();
stream.Close();
}
catch(Exception exp)
{
throw exp;
}
return strResponse;
}
C# Send mail, send web page by email
using System.Web.Mail;
namespace NEWS_RANK.util.net
{
public class Mail
{
private static string smtpserver = "127.0.0.1";
public static void sendMail(string To, string From, string Subject, string Body)
{
try
{
//-----Mail properties----
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
mMessage.Body = Body;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}
public static void sendMailHttpUrl(string To, string From, string Subject, string HttpUrl,string Query,string HttpMethod)
{
try
{
//-----Mail properties------
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
string mailContent = "";
if( HttpMethod != null && HttpMethod.ToLower().Equals("post") )
{
Uri baseUri = new Uri(HttpUrl);
string path = baseUri.GetLeftPart(System.UriPartial.Path);
string param = "";
if( Query != null && Query.Length > 1 )
param = Query;
mailContent = HttpUtil.HttpPOSTConnection(path,param);
}
else mailContent = HttpUtil.HttpConnection(HttpUrl);
mMessage.Body = mailContent;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}
}
}
Saving a web page to a file using C#
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.BinaryReader streamReader = new System.IO.BinaryReader(stream);
byte[] b = streamReader.ReadBytes((int)webResponse.ContentLength);
//-----release the reader and stream resource-----
if (System.IO.File.Exists(SavedPath))
{
//Console.WriteLine("{0} already exists!", SavedPath);
System.IO.File.Delete(SavedPath);
}
System.IO.FileStream fs = new System.IO.FileStream(SavedPath, System.IO.FileMode.CreateNew);
// Create the writer for data.
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
// Write data to Test.data.
w.Write( b );
w.Close();
fs.Close();
streamReader.Close();
stream.Close();
}
Thursday, March 10, 2005
SpAcDaemon Virus
To my supprise, when I went to My Computer -> System Properties,
I was looking at a picture of Bart Simpson and some weird info
like this:
Manufactured and supported by:
SPAC
thespacdude@yahoo.com
and when I clicked on Support Information, it shows:
SPAC
ERROR: Water detected in the BIOS.
Something wonderful has happened,
your computer is alive......
I googled it, I also tried McFee, Symantec, MS AntiSpyware, Adware, SpyBot, F-Secure, etc.
But none would detect any virus or trojans in my computer.
There're 2 strange behavior on my computer recently.
1. At Windows Startup, it plays some weird sound... you hear someone laughing.
2. It changed my OEM information, so I saw "Manufactured and supported by: SPAC"
After about 3 hours of digging thru my registry and system files, I found it.
The file is c:\windows\mssreprc.exe
also it creates a registry key
Name:SpAcDaemon Value: C:\WINDOWS\mssreprc.exe
under HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
It changed my C:\WINDOWS\system32\OEMINFO.INI to the following:
[general]
Manufacturer=SPAC
Model=thespacdude@yahoo.com
[Support Information]
Line1= ERROR: Water detected in the BIOS.
Line2=
Line3= Something wonderful has happened,
Line4= your computer is alive......
and created a Bart Simpson Logo as a bmp file
C:\WINDOWS\system32\Oemlogo.bmp
Saturday, February 26, 2005
Import mails from Thunderbird to Microsoft Outlook step by step
1. Download a program called IMAPSize
2. After installing IMAPSize, run it. You dont have to create an account, just click "no".
3. Click on "tools" -> mbox2eml
4. Locate C:\Documents and Settings\USERNAME\Application Data\Thunderbird\Profiles\xtsb8for.default\Mail\Local Folders\Inbox file, "Inbox" is the mbox file that Thunderbird stores all your inbox mails.
5. Create a folder such as C:\eml and save the eml files there
6. Click on convert, this will convert each of your mail in your Thunderbird mailbox into an eml file.
7. Now you can open Outlook Express.
8. Go to C:\eml, select all eml files, drag and drop them to the Inbox or whatever mail folder you wish in Outlook Express
9. Open Outlook, click on File - Import/export, then import from Outlook Express
Friday, January 07, 2005
How do I add install/uninstall option to a C# windows service program?
1. Add this to your service project
/*****************************
*
* install.cs
* Install the service
*
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace MyService
{
[RunInstaller(true)]
public class MyInstaller : Installer
{
public MyInstaller()
{
ServiceProcessInstaller spi=new ServiceProcessInstaller();
spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
spi.Password = null;
spi.Username = null;
ServiceInstaller si = new ServiceInstaller();
si.StartType =ServiceStartMode.Automatic;
si.ServiceName = new MyService().ServiceName;
this.Installers.Add(spi);
this.Installers.Add(si);
}
}
}
2. Add System.Configuration.Install reference to the project
3. The main function shuold look like this
// The main entry point for the process
static void Main(string[] args)
{
string opt=null;
// install the service if user entered "ServiceMonitor /install"
if(args.Length >0 )
{
opt=args[0];
}
if(opt!=null && opt.ToLower()=="/install")
{
try
{
TransactedInstaller ti= new TransactedInstaller();
MyInstaller mi = new MyInstaller();
ti.Installers.Add(mi);
String path=String.Format("/assemblypath={0}",
System.Reflection.Assembly.GetExecutingAssembly().Location);
String[] cmdline={path};
InstallContext ctx = new InstallContext("",cmdline);
ti.Context =ctx;
ti.Install(new Hashtable());
}
catch(System.Exception exp)
{
Console.WriteLine(exp);
}
}
// uninstall the service if user entered "ServiceMonitor /uninstall"
else if (opt !=null && opt.ToLower()=="/uninstall")
{
try
{
TransactedInstaller ti=new TransactedInstaller();
MyInstaller mi=new MyInstaller();
ti.Installers.Add(mi);
String path = String.Format("/assemblypath={0}",
System.Reflection.Assembly.GetExecutingAssembly().Location);
String[] cmdline={path};
InstallContext ctx = new InstallContext("",cmdline);
ti.Context=ctx;
ti.Uninstall(null);
}
catch(System.Exception exp)
{
Console.WriteLine(exp);
}
}
if(opt==null) // e.g. ,nothing on the command line
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
Wednesday, December 15, 2004
BIG5 字元衝突:: BIG5 string conflict
ASCII(5C) == “\”
A45C么 AE5C娉 B85C稞 C25C擺 A55C功
AF5C珮 B95C鈾 C35C黠 A65C吒 B05C豹
BA5C暝 C45C孀 A75C吭 B15C崤 BB5C蓋
C55C髏 A85C沔 B25C淚 BC5C墦 C65C躡
A95C坼 B35C許 BD5C穀 AA5C歿 B45C廄
BE5C閱 AB5C俞 B55C琵 BF5C璞 AC5C枯
B65C跚 C05C餐 AD5C苒 B75C愧 C15C縷
ASCII(7C) == “|”
AA7C泜 B47C揉 A87C育 BE7C魯 B27C琍
BC7C慝 C67C鸛 A97C尚 B37C逖 BD7C罵
A77C坑 B17C悴 BB7C誡 C57C疊 A67C帆
B07C院 BA7C漏 C47C辮 AB7C咽 B57C稅
BF7C糕 AC7C洱 B67C閏 C07C嚐 AD7C迢
B77C會 C17C舉 A47C弋 AE7C徑 B87C腮
C27C甕 A57C四 AF7C砝 B97C頌 C37C牘
"\"
Friday, December 10, 2004
How to run a piece of JavaScript code after a certain timed delay?
This allows you to execute a section of code after a specified amount of time
has passed.
<script type="text/javascript">
<!--
setTimeout("alert('Thirty seconds has passed.');",30000);
// -->
</script>
Allow EZ_Result to display +5Pages link
You can download ez_resuls.php from http://php.justinvincent.com/
Allow EZ_Result to display nav bar like this
365 Results: [start] 1 2 3 4 5 +5Pages [last]
For this to work, I added a few lines to the ez_results.php class as follows
/********************************************************
* NAVIGATION FORMATTING
*/
var $show_next_x_page = true;
var $text_hover_msg_next_x = '+NUMBER Pages';
var $text_next_x_page = '+NUMBER Pages';
In function build_navigation(), I added the following.
$current_page = ($_REQUEST['BRSR'] / $this->num_results_per_page) +1;
$page_left = $this->num_pages-$current_page ;
if($this->show_next_x_page)
if ( ($this->num_pages >= $this->num_browse_links) && (($_REQUEST['BRSR']
+ $this->num_results_per_page) < $this->num_results))
{
if(($page_left * $this->num_results_per_page >= $this->num_results
) || ($current_page * $this->num_results_per_page) < $this->num_results
)
{
if($page_left >= $this->next_x_pages){
$cur_a = $_REQUEST['BRSR'] + $this->num_results_per_page * $this->next_x_pages;
$out .= $this->create_link(preg_replace("/\?.*/",'',$_SERVER['PHP_SELF'])
. '?BRSR='. ($cur_a) .$this->qs,$this->merge_num('next_x_page',$this->next_x_pages),$this->merge_num('hover_msg_next_x',$this->next_x_pages),$this->get_style('next')).'
';
}
else{
$cur_a = $_REQUEST['BRSR'] + $this->num_results_per_page * $page_left;
$out .= $this->create_link(preg_replace("/\?.*/",'',$_SERVER['PHP_SELF'])
. '?BRSR='. ($cur_a) .$this->qs,$this->merge_num('next_x_page',$page_left),$this->merge_num('hover_msg_next_x',$page_left),$this->get_style('next')).'
';
}
}
}
Thursday, December 09, 2004
How do I identify unknown open ports and their associated applications.?
You can go to http://www.foundstone.com
and download Fport. It will show you something like this
FPort v2.0 - TCP/IP Process to Port Mapper
Copyright 2000 by Foundstone, Inc.
http://www.foundstone.com
Pid Process Port Proto Path
1764 inetinfo -> 25 TCP C:\WINDOWS\System32\inetsrv\inetinfo.exe
1764 inetinfo -> 80 TCP C:\WINDOWS\System32\inetsrv\inetinfo.exe
1620 Apache -> 81 TCP C:\Apache\Apache2\bin\Apache.exe
988 -> 135 TCP
4 System -> 139 TCP
1764 inetinfo -> 443 TCP C:\WINDOWS\System32\inetsrv\inetinfo.exe
4 System -> 445 TCP
1764 inetinfo -> 1038 TCP C:\WINDOWS\System32\inetsrv\inetinfo.exe
2536 mqsvc -> 1047 TCP C:\WINDOWS\System32\mqsvc.exe
5632 msnmsgr -> 1056 TCP C:\Program Files\MSN Messenger\msnmsgr.exe
5632 msnmsgr -> 1057 TCP C:\Program Files\MSN Messenger\msnmsgr.exe
5632 msnmsgr -> 1058 TCP C:\Program Files\MSN Messenger\msnmsgr.exe
5632 msnmsgr -> 1059 TCP C:\Program Files\MSN Messenger\msnmsgr.exe
5632 msnmsgr -> 1060 TCP C:\Program Files\MSN Messenger\msnmsgr.exe
3076 -> 1062 TCP
1736 FlashComAdmin -> 1111 TCP C:\Program Files\Macromedia\Flash Communica
tion Server MX\FlashComAdmin.exe
3796 OSDK62http -> 1422 TCP C:\Program Files\Openwave\SDK 6.2.2\program
\http\OSDK62http.exe
5480 firefox -> 1668 TCP C:\Program Files\Mozilla Firefox\firefox.ex
e
5480 firefox -> 1669 TCP C:\Program Files\Mozilla Firefox\firefox.ex
e
2536 mqsvc -> 1801 TCP C:\WINDOWS\System32\mqsvc.exe
2492 FlashCom -> 1935 TCP C:\Program Files\Macromedia\Flash Communica
tion Server MX\FlashCom.exe
2184 omtsreco -> 2030 TCP c:\oracle\ora92\bin\omtsreco.exe
2536 mqsvc -> 2103 TCP C:\WINDOWS\System32\mqsvc.exe
2536 mqsvc -> 2105 TCP C:\WINDOWS\System32\mqsvc.exe
2536 mqsvc -> 2107 TCP C:\WINDOWS\System32\mqsvc.exe
How to open a file as a string from a URL in PHP?
// This will read the text file from http://yoursite.com/test.txt
// and store the contents of test.txt into $contents
//
$handle = fopen("http://yoursite.com/test.txt", "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
Tuesday, December 07, 2004
WML 怎麼顯示中文?
下面 mb_convert_encoding($str, "UTF-8", "BIG-5"); 就可以把字串 convert
成 Unicode 啦
<?php
// send wml headers
header("Content-type: text/vnd.wap.wml");
echo "<?xml version=\"1.0\"?>";
echo "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""
. " \"http://www.wapforum.org/DTD/wml_1.1.xml\">";
?>
<wml>
<card>
<p>
<?php
$str = "哈囉";
$str = mb_convert_encoding($str, "UTF-8", "BIG-5");
print "<br/>Hello World! $str";
?>
</p>
</card>
</wml>
How to create a Hello World WML script using PHP?
header("Content-type: text/vnd.wap.wml");
echo "";
echo ". " \"http://www.wapforum.org/DTD/wml_1.1.xml\">";
?>
print "
Hello World!";
?>
Saturday, November 27, 2004
How do I schedule the server to generate static HTML files from other dynamic web pages?
What I do is create a shell script file and utilize OS' pipe function and a text browser to create the HTML file.
For example: create a text file called generate.sh
lynx -source http://someURL.com/yourCode.asp > yourStaticASP.html
lynx -source http://someURL.com/yourCode.php?Var=123 > yourStaticPHP.html
and do a chmod on the file, make sure it is executable, e.g., chmod u+x generate.sh
You may then schedule this script file in your cron table ;-)
How do I use a hyperlink to POST form variable?
<form name="aform" action="somewhere.php" method="post">
<input type="hidden" name="firstName" value=" <?php echo $firstName ; ?> ">
<input type="hidden" name="lastName" value=" <?php echo $lastName ; ?> ">
<input type="hidden" name="section" value="0">
</form>
Simply add a little JavaScript somewhere in the <head> ... </head> part of your page and we're done. The JavaScript would look something like this...
function clickpost(param)
{
document.forms.aform.elements .section.value = param;
document.forms.aform.submit;
return true;
}
Javascript download script
<!--
function beginDownload() {
idl = -1;
idl = location.search.indexOf("idl=n");
if (idl < 0) document.write('<iframe height="0" width="0"
src="PUT_URL_FOR_DOWNLOAD_FILE_HERE"></iframe>');
}
window.onLoad=beginDownload();
//-->
</script>
Some useful regular expressions...
HTML tag (?:(?:<[^>]+>))
HTML start (?:(?:<[^//][^>]*>))
[HTMLEndTag] (?:(?:[^>]+>))
[QuotedString] (?:(?:[\"](?:(?:[\\][\"])|[^\"
IP addr
(?:(?:\b([01]?\d\d?|2[0-4]\d
Credit Card (?:(?:(\d{3,4})[- ]?(\d{4})[- ]?(\d{4})[- ]?(\d{4})))
HyperLink
(?:(?:(ftp|http|https|telnet
How Do I get row number with SQL commands in MySQL?
Let's suppose we have a table look like this
Name PhoneNumber
-------------- --------------------
Peter 415-895-5689
John 408-854-4587
Louis 408-975-5986
And now I want to issue a SQL command which gives back a row number for each record which would look like this
RowNumber Name PhoneNumber
----------------- -------------------- ---------------------
1 Peter 415-895-5689
2 John 408-854-4587
3 Louis 408-975-5986
Under Oracle, this would be simple task, just issue the following SQL command
SELECT rownum, Name, PhoneNumber from table;However, MySQL does not offer such convinience as rownum. In MySQL, we need to do the following instead.
mysql> SET @rownum := 0;
mysql> SELECT @rownum := @rownum + 1 AS RowNumber, Name, PhoneNumber from table;Hope this tip can help you solve your current problem :)