1. Add a reference to System.Configuration.dll
2. Add using System.Configuration;
3. Use the following code:
Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
c.ConnectionStrings.ConnectionStrings.Add( new ConnectionStringSettings("conn 1", "Server=local; Database=Test; Password=myPassword; User Id=myUser;"));
ConfigurationSection section = c.GetSection("connectionStrings");
if (section != null)
{
if (!section.IsReadOnly())
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
}
}
c.Save();
Thursday, September 28, 2006
Monday, September 25, 2006
Call one constructor from another
Avoid duplicating code...
class C1
{
public C1() { }
public C1(string s):this() { }
}
class C1
{
public C1() { }
public C1(string s):this() { }
}
Sunday, September 24, 2006
Defining a generic method in a non-generic class
For instance:
class Class2
{
public static void GenericMethod <X>(int n)
where X:new()
{
X myXinstance = new X();
}
}
The method can be called like this:
Class2.GenericMethod <System.Collections.ArrayList>(100);
class Class2
{
public static void GenericMethod
where X:new()
{
X myXinstance = new X();
}
}
Class2.GenericMethod <System.Collections.ArrayList>
Friday, September 22, 2006
Getting the identity of an assembly
Use the following code to create an identity object for an assembly. It can be used when passing it to the GetStore method when trying to one an isolated file storage (iso) store that requires that the identity of the assembly be provided.
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
string assemblyPath = a.FullName;
System.Security.Policy.Url url = new System.Security.Policy.Url(assemblyPath);
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
string assemblyPath = a.FullName;
System.Security.Policy.Url url = new System.Security.Policy.Url(assemblyPath);
Monday, September 04, 2006
Using the new delegate Syste.Predicate
Here's a small case when the new C# 2.0 feature, Predicate<T> may be useful:
Consider an array of file paths retrieved from disk:
string[] s = new string[4] { @"C:\a.jpg", @"C:\b.txt", "c.bmp", "d.swf" };
We can find all the image files (i.e those that have a certain extension) by doing this:
Predicate<string> p = new Predicate<string>(CheckIfImageFile);
string[] newS = Array.FindAll<string>(s, p);
Where CheckIfImageFile is defined here:
static bool CheckIfImageFile(string fullFilePath)
{
string commonImageExtensions = "*.png;*.jpg;*.gif;*.bmp";
if (commonImageExtensions.IndexOf(Path.GetExtension(fullFilePath)) > 0) return true;
return false;
}
Consider an array of file paths retrieved from disk:
string[] s = new string[4] { @"C:\a.jpg", @"C:\b.txt", "c.bmp", "d.swf" };
We can find all the image files (i.e those that have a certain extension) by doing this:
Predicate<string> p = new Predicate<string>(CheckIfImageFile);
string[] newS = Array.FindAll<string>(s, p);
Where CheckIfImageFile is defined here:
static bool CheckIfImageFile(string fullFilePath)
{
string commonImageExtensions = "*.png;*.jpg;*.gif;*.bmp";
if (commonImageExtensions.IndexOf(Path.GetExtension(fullFilePath)) > 0) return true;
return false;
}
Saturday, September 02, 2006
Undocumented C# keywords
I found some interesting undocumented keywords (http://www.eggheadcafe.com/articles/20030114.asp):
__makeref, __refvalue, __reftype, __arglist.
Here are a few examples:
int i = 10;
TypedReference tr = __makeref(i);
Type t = __reftype(tr);
int j = __refvalue( tr,int);
protected void Page_Load(Object sender, EventArgs e)
{
int x=85;
string y = "a stringy thingy";
double d=19.45;
WriteToPage(__arglist(x,y,d));
}
public void WriteToPage(__arglist)
{
ArgIterator ai = new ArgIterator(__arglist);
while(ai.GetRemainingCount() >0)
{
TypedReference tr = ai.GetNextArg();
Response.Write(TypedReference.ToObject(tr)+"");
}
}
__makeref, __refvalue, __reftype, __arglist.
Here are a few examples:
int i = 10;
TypedReference tr = __makeref(i);
Type t = __reftype(tr);
int j = __refvalue( tr,int);
protected void Page_Load(Object sender, EventArgs e)
{
int x=85;
string y = "a stringy thingy";
double d=19.45;
WriteToPage(__arglist(x,y,d));
}
public void WriteToPage(__arglist)
{
ArgIterator ai = new ArgIterator(__arglist);
while(ai.GetRemainingCount() >0)
{
TypedReference tr = ai.GetNextArg();
Response.Write(TypedReference.ToObject(tr)+"");
}
}
Friday, September 01, 2006
Debugging Windows Services In Visual Studio 2005
To do that, change the body of the Main method of the Windows service:
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyService() };
ServiceBase.Run(ServicesToRun);
#else
MyService service = new MyService();
service.MyOnStart();
#endif
You will get two error messages at the beginning but you can continue debugging.
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyService() };
ServiceBase.Run(ServicesToRun);
#else
MyService service = new MyService();
service.MyOnStart();
#endif
You will get two error messages at the beginning but you can continue debugging.
Subscribe to:
Posts (Atom)