Wednesday, November 4, 2009

How to find out the current path/virtual directory of a website?

Most of the time, when we are setting the image url, we will use "~" symbol before the image file name. I was wondering what is the meaing of "~" symbol and how it works? How does ASP.NET knows whether the website is running from the root of IIS or from a Virtual Directory? Then, translate this symbol into a correct URL?

For example, setting the image button image url to "~/images/ok.jpg". We will have 2 situations:

1. The website is running from the IIS root. This means, you will have

http://www.testing.com/images/ok.jpg

2. The website is running from a virtual directory in IIS. You will have

http://www.testing.com/news/images/ok.jpg

Programmatically, we have to use System.Web.VirtualPathUtility class to determine the URL of the image.

"~/images/ok.jpg"
  • VirtualPathUtility.IsAbsolute() will returns false. Meaning, once the URL contains the "~" symbol, it becomes relative. So, VirtualPathUtility.IsAppRelative() will return true.
  • For ASP.NET to translate this URL into a runtime URL, we have to call VirtualPathUtility.ToAbsolute(). The result will be "/images/ok.jpg" and "/news/images/ok.jpg" for the above mentioned situation respectively.
  • To compute the physical path, you have to call AppDomain.CurrentDomain.BaseDirectory + relative path (you have to remove the "~" symbol and replace "/" with "\").

Thursday, January 22, 2009

Activator.CreateInstance

According to the second reference, the Activator.CreateIntance is same as the "new" keyword.

To load the assembly from GAC:

System.Reflection.AssemblyName asmName = new System.Reflection.AssemblyName(name);
System.Reflection.Assembly asm = System.Reflection.Assembly.Load(asmName);
object
obj = asm.CreateInstance(typeName);

References:
(1)
http://blogs.msdn.com/haibo_luo/archive/2005/11/17/494009.aspx

(2)
Q&A of Activator.CreateInstance
http://bytes.com/groups/net-c/586685-activator-createinstance-question

(3)
http://mironabramson.com/blog/post/2008/08/Fast-version-of-the-ActivatorCreateInstance-method-using-IL.aspx

(4)
http://blog.lozanotek.com/archive/2006/03/22/8311.aspx

Tuesday, January 20, 2009

How to upload file using WebClient

To use System.Net.WebClient class, first, you have to create an ASPX page in the website as shown in MSDN website. Then, execute the following:

System.Net.WebClient cli = new System.Net.WebClient();

cli.UploadFile(url,
"POST",
file_name);

Reference:
http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile(VS.71).aspx
http://www.developerfusion.com/forum/thread/38843/
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=115

Wednesday, January 14, 2009

Handling Exception

Performance testing on catching Exception

http://www.yoda.arachsys.com/csharp/exceptions.html

When to throw exception?

http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx

Singleton

Check out the discussion of singleton here:
http://www.yoda.arachsys.com/csharp/singleton.html
Sample code extracted from the above URL :

public sealed class Singleton
{
  static Singleton instance=null;
  static readonly object lock_obj = new object();

  Singleton()
  {
  }

  public static Singleton Instance
  {
      get
      {
          if (instance==null)
          {
              lock (lock_obj)
              {
                  if (instance==null)
                  {
                      instance = new Singleton();
                  }
              }
          }

          return instance;
      }
  }
}