string filePath = @"C:\test.doc"; byte[] byteArray = File.ReadAllBytes( filePath );
Programming
Programming
Tuesday, April 26, 2011
Read File into Byte Array
Labels:
c#,
Read File into Byte Array
C# Convert String to Byte Array
string s = "Test String"; byte[] byteArray = Encoding.ASCII.GetBytes( s );
Labels:
c#,
Convert String to Byte Array
C# Convert Byte Array to String
byte[] buffer = new byte[10]; // todo: populate the buffer with string data
string s = Encoding.ASCII.GetString( buffer );
Monday, April 25, 2011
Pass parameters to the timer in C#
In C#, there are 3 kinds of timer type definitions:
1. System.Windows.Forms.Timer
2. System.Threading.Timer
3. System.Timers.Timer
In my project, I choose to use the third one: System.Timers.Timer
Here is the sample usage of this kind of timer:
System.Timers.Timer timer = new System.Timers.Timer();
timer. Interval = 10000; // set the interval as 10000 ms
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed); // set the event to execute
timer.AutoReset = true; // false: only execute once
timer.Enabled = true; // to decide if execute the event of timer specified
…
public void timer_Elapsed(object sender, EventArgs e)
{
…
}
From the above sample, we know it is quite easy to use it.
Next, I have a problem if I need pass some parameters to be used in the timer event defined above: timer_Elapsed(…). The two input parameters of this event interface are fixed, you can’t add more in.
There is a tricky to achieve it. Since we can’t add more input parameters in this event, we can set the parameters into the unused property of that sender object. This way is simple, but not good.
After researching, i find a better way to deal with it: creating a new timer to extend the system timer , and then defining the additional parameters inside this new timer.
Here are the sample codes:
// define a class to extend the timer,
// then define those additional parameters to be used in the event.
// for example: add one more parameter: param1.
namespace XXX
{
public class TaskTimer : Timer{
private string param1;
public string Param1
{
get
{
return param1;
}
set
{
param1= value;
}
}
public TaskTimer() : base()
{
}
}
}
// usage of this timer
using XXX;
…
TaskTimer timer = new TaskTimer();
timer.Interval = 10000;
…
// the event definition
public void timer_Elapsed(object sender, EventArgs e)
{
String param1 = (TaskTimer) sender).Param1;…
}
Saturday, April 23, 2011
C# Delegate
// This "DoMath" delegate can point to ANY method that:
// - Returns an int
// - Accepts two ints as parameters
public delegate int DoMath(int x, int y);
private void button1_Click(object sender, EventArgs e)
{
int result;
DoMath a = new DoMath(Add);
result = a(3, 4);
MessageBox.Show(Convert.ToString(result));
UseDelegate(a);
}
public void UseDelegate(DoMath x)
{
int r = x(6, 6);
MessageBox.Show(Convert.ToString(r));
}
//This method can be pointed to by the DoMath delegate
public int Add(int x, int y)
{
return x+y;
}
//This method can be pointed to by the DoMath delegate
public int Subtract(int x, int y)
{
return x - y;
}
//This method can NOT be pointed to by the DoMath delegate
//because it has a different signature (only one argument)
public string Test(string y)
{
return "Test method";
}
}
// - Returns an int
// - Accepts two ints as parameters
public delegate int DoMath(int x, int y);
private void button1_Click(object sender, EventArgs e)
{
int result;
DoMath a = new DoMath(Add);
result = a(3, 4);
MessageBox.Show(Convert.ToString(result));
UseDelegate(a);
}
public void UseDelegate(DoMath x)
{
int r = x(6, 6);
MessageBox.Show(Convert.ToString(r));
}
//This method can be pointed to by the DoMath delegate
public int Add(int x, int y)
{
return x+y;
}
//This method can be pointed to by the DoMath delegate
public int Subtract(int x, int y)
{
return x - y;
}
//This method can NOT be pointed to by the DoMath delegate
//because it has a different signature (only one argument)
public string Test(string y)
{
return "Test method";
}
}
Labels:
C# Delegate,
Delegates,
Pointer
C# Split String
=== Example program for splitting on spaces (C#) ===
using System;
class Program
{
static void Main()
{
string s = "there is a cat";
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
=== Output of the program ===
there
is
a
cat
using System;
class Program
{
static void Main()
{
string s = "there is a cat";
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
=== Output of the program ===
there
is
a
cat
Labels:
C# Split String,
Split
.........,,,,,,,,.....,...............,~:..,:::+++?????????,,,::,,:,,,,,,,,,,,,,
..,,,.,,,,,,,,,,......................,~...,~,::==+++++?III?,:~~,::,,,:::::,,,::
...,,,,,,,,,,,:,....,................,:,,,:,:~,,:=+=+++?III7,~::,,:,,,,,,,,,,,,,
.............,,,...,................,:~~.~=~=+~:,.,===??II77?~,,,.....,..,,,,,,,
............:,,...,..................:+=:~=+~:=~,,...,~??II77::,,.,...,...,..,,.
............:,..............,,......,~??+:++,~::~~:,..,:=+I77.,,,.,,,,,.........
...........,.,..............:,:::....~???=~.:,~,,:~:,,:,::.+7::,,.,,,,,...,...,.
..........,................:~~:::...,==?I?=........,,::=,,,.:,,,,,,,,,,..,,.,,,.
.............,.............:,,~:=,..,++++?+,,:,,,..,,+=7.,,~I..,,.,,,,,,.,,.,.,.
....,.,...................,:=:,+=~..~=+=++??=,=~:::~+?~I,..:=:,.,,,,,,,.,,,.,,,.
......,,..................~~~====~:..:+?+~+++=,~===+????:,..,:::..,,,,,..,,.,,,.
.......,...................:++++=+==.,,+++~=++=,===+????I:,,,.,.,...,,,,,,,,,,,,
...,.........................~====++?,.:==+=~=+=:~=+?I++=7?I~,:,,,,,,,,,,,,,,,,,
..............................,,.~=+??~..,+==~~=~:=~:..~,+?I=:,,,,,,.,,,,,,,,,,,
..............................,,,:=????+...:~~:~==~,:=,.~+I?:::,,,,,,,,,.,,,,,,,
..............................,,::=++???+~.,..::==++:?~I++++,,,,,,,,,,,,,,,.,,,,
.......,,....................,,:~:~=+++++++:,,..,,:,,,,~===~:::,.,,.,,,,,,,,,,.,
......,,.....................,::~~::~=+++++==:,..,.....,+=+:,:,,.,,,,,,,,,,.,,,,
.....,,......................,~:~~~~:~~=======~:,....~:,:+,~:,,,.........,......
.....,,.......................:~:~~~~:::~~~====~::,,...,=,.,,::,.,,......,......
.......,......................,~~:~~~:::::::~==~=~::?===....,,,,,..,,,...,......
....,,,....,...................:~~~~=~::,,,,:::~~:::+?+..:.,~,....,,,,.......,..
........,......................,~~~~==~:,,,,,,,,,,,:=?.....,,,,,,..,,...,.......
.,.....,,......................,,~~===~:,,,,,,............,,,~:,...,..,,........
,....,,.....................,...,,~==~::,,,,.......,........,~,:..,,,,,..,,,,,..
....,.........,............,,,...,,~~~::,,,,,,......,.......::~,..,,,,,.,...,,..
...,....,,....,...........:::,:....,~~::,:::,,,.............,::,......,...,.....
.........,...,,..........,~~==,~...,,:::::~::,,...............:,,~,,,,,,,,......
..............,..........:=+?++~,:...,,:~~=~::,.,.............,,.,...,..,,...,..
..............,..........=??I??++~:,...,~~~~::,,,,.............,,,,.,,,,,,..,,,,
..............,.......,~:+?II???+==:,,....,~::,,::..............,,.,,,,,,.,,,,,.
..............,,...::,~=,=?II???++=~::::,,,....,::...............:~:,.,,,,,.,,,,
.............,,~=~::,~==:=+II????+=~~=+++=:,:~~:.....,............:~,:=~,,,,,,,,
..........,.=~=~=++~:~=~,=?IIIIII+==+????????++==:,..................~~~=+,.,,..
.....,..====??????+??::~:+?IIIIII?+??IIIIIII?I???~~:~,,...,........,::,:~==.,,..
,,,,,~++???????:??~~~~~~+?IIIIIIII?IIIIII?IIIIII?~:::,,,..,,......,.,::,:~~+,,..
,.=++????????+?+=~~~~~=~=??IIIIIIIIIIIIII???IIIII=:~::,,,,~~~,......,,,:,:=+,,.,
?????+++????++=+=:~~=~=~~????IIIIIIIIIII?I?IIIIII+~~~:,,,,==~~........,:,:~+,,,,
???+=+==+???++~++~~~~~~==???IIIIIIIIIIIIIIIIIIIII+~~~::,,,=:=~:.......,:,:~=,...
++++~~==+++++?==~~~~~=~~=??IIIIIIIIIIIIIIIIIIIIII+=~~~::,,==~+=:......,:,::++,,,
++===:==++++++~+::~~==~~+?IIIIIIIIIIIIIIIIIIIIIII+=~~:::,,==~:=~,......,,:~~I:,:
++=~=~~=+?+=++~=::~~=~~~+???IIIII????IIIIIIIIIIII+=~::::::~==~:=~,......:~~~?::,
++==~=:~=+==+=~=:~~~~~~~+?????????????IIIIIIIIIII+~:~::~~:~=~::~~:.......,,~+~::
++===~~~====+=~~:~~~~~~~=????I???????IIIIIIIIIIII+~:::::::=~~::~~:,.......,,:?::
++==~~~:~~~~=~~~:~~~~~~~+???I???????IIIIIIIIIIIII+~:::,,::=~~:::~:,........~~~,:
++====~~~~~~=~::::~::~:~+??III???????IIIIIIIIIIII+~::::,::~~~:,:~:,,.......,~:,,
++=====~:~~:~~:~~~~:~~~~+??I?+++??????IIIIII+?III+~::::,,,:::,.~~~:::.......,~~,
=++====~~:~::::~~~~~~~~~+??I??+++~????IIII?+?IIII+~:::,,,,::,..:~~::::.......~:,
~+++++=~~::::::,:::::~:~??I?+++==~????III+++++?II=::::,,,,,,...:~~::::,......,I~
:=++++=~~:~:,,,,::::::::??III+==~++????I=+=+?+II?=:,::,,,,,,...:~:::~:,........:
:~++++==~~::,,,,:::::~:~?I?+?+=?==~++??I~=?+IIII+~:::,,,,,,,...,~:::::,,,......,
::++++==~~::,,.,:,,,~~:~?II??=====+++++++==+???++~:,,,,,,,,,...,~::::,,:........
:~=+++===~~::.,,,:,~:::~?II??+==+=+=++??++=+?III=~:,,,,,,,,,...,::,::,:,,:......
:~:+++===~:~:,..,,:::::~??I??=+=+~:=++++~+=++III=~,,,,,,..,,...:~:,::,:,:::.....
:~~=++=~=~~::,,.,,~::~::+?????+~~~:=??++~~=??+??=~,,,.....,,...:~,::::::::~,....
,:~~++==~=~~::,..,,,~==~=????+++~::~??~~~=+++???~~:.......,,..,~:,:,:::,:::~,...
,:~~++==~~=~:::,.,,,:~~~:++++++~~~=+??+==~~+++++~:,..........,,~,,:,:,,:::~~~...
,:~~:=+=~~=~::,,,....::::======~~~=++++=~~~:====::,..........,:,,,::::,~:::~~~,.
,,:~~:==:~=~~,::,......,::~~~~:~~~=+++++=~~,:~~~::...........,:,,,::::,::::~~~~:
,,:~~:~==~~=~::::,......:::~::::~=====++=~::::::::...........:,,,,:::,,~:~:~+=~=
,,,:~:~~==~~~~:,:,,.....,,::::::~======++=~::,,,,,..........,:,,,,:::,::~~~~~~~=
,,,::~~:~==~:~:,,,,.....,,,,,::~=======++++=:,,,,,..........,,,,,::::,:~~~~=====
,,:,:~:~:===~:~:,,,,.....,.,,:~~======++++++==~:,,.........,:,,,,,::,,~~~~~~~==+
,,,,:::~~~===:,:,,,,.....,.,:~~==+====++++++++=~:,.........,,,,,,,::,:~=~~:=~===
,,,,,:::~~~===:,:,,,,,...,,::~==+++==+++++++++==,,........,:,,,,,:::,:==~~~===++
,,,,,,::~~:~==~:,,,,,,....,:~~==+++++++++++++===,,........,,,,,,,::,,~==~=~====+
..,,,.,,,,,,,,,,......................,~...,~,::==+++++?III?,:~~,::,,,:::::,,,::
...,,,,,,,,,,,:,....,................,:,,,:,:~,,:=+=+++?III7,~::,,:,,,,,,,,,,,,,
.............,,,...,................,:~~.~=~=+~:,.,===??II77?~,,,.....,..,,,,,,,
............:,,...,..................:+=:~=+~:=~,,...,~??II77::,,.,...,...,..,,.
............:,..............,,......,~??+:++,~::~~:,..,:=+I77.,,,.,,,,,.........
...........,.,..............:,:::....~???=~.:,~,,:~:,,:,::.+7::,,.,,,,,...,...,.
..........,................:~~:::...,==?I?=........,,::=,,,.:,,,,,,,,,,..,,.,,,.
.............,.............:,,~:=,..,++++?+,,:,,,..,,+=7.,,~I..,,.,,,,,,.,,.,.,.
....,.,...................,:=:,+=~..~=+=++??=,=~:::~+?~I,..:=:,.,,,,,,,.,,,.,,,.
......,,..................~~~====~:..:+?+~+++=,~===+????:,..,:::..,,,,,..,,.,,,.
.......,...................:++++=+==.,,+++~=++=,===+????I:,,,.,.,...,,,,,,,,,,,,
...,.........................~====++?,.:==+=~=+=:~=+?I++=7?I~,:,,,,,,,,,,,,,,,,,
..............................,,.~=+??~..,+==~~=~:=~:..~,+?I=:,,,,,,.,,,,,,,,,,,
..............................,,,:=????+...:~~:~==~,:=,.~+I?:::,,,,,,,,,.,,,,,,,
..............................,,::=++???+~.,..::==++:?~I++++,,,,,,,,,,,,,,,.,,,,
.......,,....................,,:~:~=+++++++:,,..,,:,,,,~===~:::,.,,.,,,,,,,,,,.,
......,,.....................,::~~::~=+++++==:,..,.....,+=+:,:,,.,,,,,,,,,,.,,,,
.....,,......................,~:~~~~:~~=======~:,....~:,:+,~:,,,.........,......
.....,,.......................:~:~~~~:::~~~====~::,,...,=,.,,::,.,,......,......
.......,......................,~~:~~~:::::::~==~=~::?===....,,,,,..,,,...,......
....,,,....,...................:~~~~=~::,,,,:::~~:::+?+..:.,~,....,,,,.......,..
........,......................,~~~~==~:,,,,,,,,,,,:=?.....,,,,,,..,,...,.......
.,.....,,......................,,~~===~:,,,,,,............,,,~:,...,..,,........
,....,,.....................,...,,~==~::,,,,.......,........,~,:..,,,,,..,,,,,..
....,.........,............,,,...,,~~~::,,,,,,......,.......::~,..,,,,,.,...,,..
...,....,,....,...........:::,:....,~~::,:::,,,.............,::,......,...,.....
.........,...,,..........,~~==,~...,,:::::~::,,...............:,,~,,,,,,,,......
..............,..........:=+?++~,:...,,:~~=~::,.,.............,,.,...,..,,...,..
..............,..........=??I??++~:,...,~~~~::,,,,.............,,,,.,,,,,,..,,,,
..............,.......,~:+?II???+==:,,....,~::,,::..............,,.,,,,,,.,,,,,.
..............,,...::,~=,=?II???++=~::::,,,....,::...............:~:,.,,,,,.,,,,
.............,,~=~::,~==:=+II????+=~~=+++=:,:~~:.....,............:~,:=~,,,,,,,,
..........,.=~=~=++~:~=~,=?IIIIII+==+????????++==:,..................~~~=+,.,,..
.....,..====??????+??::~:+?IIIIII?+??IIIIIII?I???~~:~,,...,........,::,:~==.,,..
,,,,,~++???????:??~~~~~~+?IIIIIIII?IIIIII?IIIIII?~:::,,,..,,......,.,::,:~~+,,..
,.=++????????+?+=~~~~~=~=??IIIIIIIIIIIIII???IIIII=:~::,,,,~~~,......,,,:,:=+,,.,
?????+++????++=+=:~~=~=~~????IIIIIIIIIII?I?IIIIII+~~~:,,,,==~~........,:,:~+,,,,
???+=+==+???++~++~~~~~~==???IIIIIIIIIIIIIIIIIIIII+~~~::,,,=:=~:.......,:,:~=,...
++++~~==+++++?==~~~~~=~~=??IIIIIIIIIIIIIIIIIIIIII+=~~~::,,==~+=:......,:,::++,,,
++===:==++++++~+::~~==~~+?IIIIIIIIIIIIIIIIIIIIIII+=~~:::,,==~:=~,......,,:~~I:,:
++=~=~~=+?+=++~=::~~=~~~+???IIIII????IIIIIIIIIIII+=~::::::~==~:=~,......:~~~?::,
++==~=:~=+==+=~=:~~~~~~~+?????????????IIIIIIIIIII+~:~::~~:~=~::~~:.......,,~+~::
++===~~~====+=~~:~~~~~~~=????I???????IIIIIIIIIIII+~:::::::=~~::~~:,.......,,:?::
++==~~~:~~~~=~~~:~~~~~~~+???I???????IIIIIIIIIIIII+~:::,,::=~~:::~:,........~~~,:
++====~~~~~~=~::::~::~:~+??III???????IIIIIIIIIIII+~::::,::~~~:,:~:,,.......,~:,,
++=====~:~~:~~:~~~~:~~~~+??I?+++??????IIIIII+?III+~::::,,,:::,.~~~:::.......,~~,
=++====~~:~::::~~~~~~~~~+??I??+++~????IIII?+?IIII+~:::,,,,::,..:~~::::.......~:,
~+++++=~~::::::,:::::~:~??I?+++==~????III+++++?II=::::,,,,,,...:~~::::,......,I~
:=++++=~~:~:,,,,::::::::??III+==~++????I=+=+?+II?=:,::,,,,,,...:~:::~:,........:
:~++++==~~::,,,,:::::~:~?I?+?+=?==~++??I~=?+IIII+~:::,,,,,,,...,~:::::,,,......,
::++++==~~::,,.,:,,,~~:~?II??=====+++++++==+???++~:,,,,,,,,,...,~::::,,:........
:~=+++===~~::.,,,:,~:::~?II??+==+=+=++??++=+?III=~:,,,,,,,,,...,::,::,:,,:......
:~:+++===~:~:,..,,:::::~??I??=+=+~:=++++~+=++III=~,,,,,,..,,...:~:,::,:,:::.....
:~~=++=~=~~::,,.,,~::~::+?????+~~~:=??++~~=??+??=~,,,.....,,...:~,::::::::~,....
,:~~++==~=~~::,..,,,~==~=????+++~::~??~~~=+++???~~:.......,,..,~:,:,:::,:::~,...
,:~~++==~~=~:::,.,,,:~~~:++++++~~~=+??+==~~+++++~:,..........,,~,,:,:,,:::~~~...
,:~~:=+=~~=~::,,,....::::======~~~=++++=~~~:====::,..........,:,,,::::,~:::~~~,.
,,:~~:==:~=~~,::,......,::~~~~:~~~=+++++=~~,:~~~::...........,:,,,::::,::::~~~~:
,,:~~:~==~~=~::::,......:::~::::~=====++=~::::::::...........:,,,,:::,,~:~:~+=~=
,,,:~:~~==~~~~:,:,,.....,,::::::~======++=~::,,,,,..........,:,,,,:::,::~~~~~~~=
,,,::~~:~==~:~:,,,,.....,,,,,::~=======++++=:,,,,,..........,,,,,::::,:~~~~=====
,,:,:~:~:===~:~:,,,,.....,.,,:~~======++++++==~:,,.........,:,,,,,::,,~~~~~~~==+
,,,,:::~~~===:,:,,,,.....,.,:~~==+====++++++++=~:,.........,,,,,,,::,:~=~~:=~===
,,,,,:::~~~===:,:,,,,,...,,::~==+++==+++++++++==,,........,:,,,,,:::,:==~~~===++
,,,,,,::~~:~==~:,,,,,,....,:~~==+++++++++++++===,,........,,,,,,,::,,~==~=~====+
Subscribe to:
Posts (Atom)