Welcome to MSDN Blogs Sign in | Join | Help

How to get the ProgramFiles folder

While writing a code, I had a requirement to use the programfiles folder to pull out some files from there.

Here is the code to get it:

Console.WriteLine(System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));

Posted by Deepak Verma | 1 Comments
Filed under:

Get Apppool Framework version

I would like to share the following code snippet. This is to get the framework version that is used for a managed applications in the application pool of IIS 7.0

namespace iistest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine(":" + p.GetAppPoolVersion("DefaultAppPool") + ":");
        }
        public string GetAppPoolVersion(String apppool)
        {
             String metabasePath = "IIS://localhost/W3SVC/AppPools/" + apppool;
            DirectoryEntry appPools = new DirectoryEntry(metabasePath);
            return appPools.Properties["ManagedRuntimeVersion"][0].ToString();
 
        }
      
 
    }
}
Posted by Deepak Verma | 1 Comments
Filed under: ,

Trapping Application's events in Silverlight

When you create a Silverlight application the App.xaml.cs code behind file has the Application event handlers added for you

public App()
       {
           this.Startup += this.Application_Startup;
           this.Exit += this.Application_Exit;
           this.UnhandledException += this.Application_UnhandledException;
 
           InitializeComponent();
       }

In this post I will discuss about the Application.Exit and will use another for Application.Startup.

Application.Exit

Application.exit event helps the  developer to detect the shut down of a Silverlight application. This helps to perform common application closing tasks such as saving data for the next session.

Let's see an example where the application will store a value in a cookie when user exits.

Open App.xaml.cs and add the code you want to be execute just before the application is shut down

 private void Application_Exit(object sender, EventArgs e)
        {
            Cookie.SetCookie("level","5");
        }
 
Wherein, I have wrapped the Cookie functions of Tip of the day #18 in a class.
 
Now the cookie value can be used when user loads the application again.
 
public Page()
        {
            InitializeComponent();
            level.Text = Cookie.GetCookie("level");
        }
 
Posted by Deepak Verma | 1 Comments
Filed under:

Dynamic data Wizard 0806 (Preview) for VS SP1

With Visual Studio 2008 SP1 shipping, Dynamic data wizard (preview) installer for SP1 can be downloaded from Codeplex.

To install the Dynamic Data Wizard, follow these steps:

  1. Upgrade to Visual Studio 2008 Service Pack 1.
  2. Download Wizard from Codeplex
  3. Unzip the .zip file into a folder on your computer.
  4. From either a VS 2008 command prompt (elevated command prompt for Vista) or .NET Framework 2.0 SDK command prompt, move to that folder and run Install.cmd.

Known installation issues:

  • Starting this release wizard may not execute correctly on VS SP1 Beta
  • Also may not execute correctly if only framework is upgraded to .Net 3.5 SP1. If only .Net framework is upgraded to SP1 you may receive a dialog box at the finish of wizard "Could not reformat the document. The original format was restored.".
    To resolve this issue upgrade to VS 2008 SP1.
  • Don't use the uninstall script from any of the previous Dynamic data releases for VS SP1 Beta to uninstall the wizard after VS 2008 SP1 upgrade is complete.
Posted by Deepak Verma | 1 Comments
Filed under:

Dynamic creation & binding of Silverlight datagrid.

For a Silverlight application, I have a requirement to dynamically create datagrid from xml data.

Following two samples show the format of xml returned from a webservice.

1. <SqlXml empid=”varchar” deptid=”varchar” empname=”varchar”>

<row empid="1001 " deptid="1 " empname="Sam" />

<row empid="1002 " deptid="3 " empname="Bill" />

</SqlXml>

2. <SqlXml empid=”varchar” deptid=”varchar” empname=”varchar”>

<row deptid="1" deptname="Fin" />

<row deptid="3 " deptname="Sales" />

</SqlXml>

Now, I want to write a Silverlight application that can parse the above xml format and create the datagrid columns dynamically.

Following is  the solution I used to create my application.

Step 1: Create the datagrid

The important part for creating datagrid columns dynamically is to set AutogenerateColumns property to false otherwise it may throw an exception

DataGrid dg = new DataGrid();

dg.AutoGenerateColumns = false;

Step 2: Read the xml and add columns to the datagrid

while (xmlReader.MoveToNextAttribute())
{
DataGridTextColumn dtextcol = new DataGridTextColumn();
dtextcol.IsReadOnly = true;
dtextcol.Header = xmlReader.Name;
dg.Columns.Add(dtextcol);
}

Step 3: Parse the xml to a list of dictionary strings and assign it to Itemsource property

while(xmlReader.Read())
{
  if (xmlReader.HasAttributes)
  {
       Dictionary<string, string> d = new Dictionary<string, string>();
       while (xmlReader.MoveToNextAttribute())
       {
        d.Add(xmlReader.Name, xmlReader.Value);
       }
       list.Add(d);
  }
}
//move the reader to elements
xmlReader.MoveToElement();
}
}


Step 4:
Assign the list to datagrid’s itemsource property

dg.ItemsSource = list;


Step 5:
Now, how can I bind the dictionary object to the datagridcolumn
I thought of implementing it as

dtextcol.DisplayMemberBinding = d[xmlReader.Name];

I guess indexers aren't supported as of Silverlight Beta 2. The workaround is to use converter and a converter param.
so the above code will change to

while (xmlReader.MoveToNextAttribute()) 
{ 
         DataGridTextColumn dtextcol = new DataGridTextColumn(); 
         dtextcol.IsReadOnly = true; 
         dtextcol.Header = xmlReader.Name; 
         indexingConverter convert = new indexingConverter(); 
         Binding bind = new Binding { Converter = convert, ConverterParameter = xmlReader.Name }; 
         dtextcol.DisplayMemberBinding = bind; 
        dg.Columns.Add(dtextcol); 
} 
 
class indexingConverter : IValueConverter 
{ 
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
Dictionary<string, string> columnData = (Dictionary<string, string>)value; 
return columnData[parameter.ToString()]; 
} 
 
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
throw new NotImplementedException(); 
} 
} 
 

Wherein the first parameter passed to Convert function is the dictionary object and the third parameter is the attribute name.

Following is the full code

namespace datagrid 
{ 
public partial class Page : UserControl 
{ 
public Page() 
{ 
InitializeComponent(); 
} 
 
private void constructdg() 
{ 
//generic xml returned from a service 
String xml = "<SqlXml empid=\"varchar\" deptid=\"varchar\" empname=\"varchar\"><row empid=\"1001\" deptid=\"1\" empname=\"Sam\" /><row empid=\"1002\" deptid=\"3\" empname=\"Bill\" /></SqlXml>"; 
DataGrid dg = new DataGrid(); 
dg.AutoGenerateColumns = false; 
XmlReader xmlReader = XmlReader.Create(new StringReader(xml)); 
//list containing the row elements 
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>(); 
//create the columns of the datagrid from the first node 
while(xmlReader.Read()) 
{ 
if (xmlReader.HasAttributes) 
{ 
//check if the first node 
if (xmlReader.IsStartElement("SqlXml")) 
{ 
//construct the columns of the dg 
while (xmlReader.MoveToNextAttribute()) 
{ 
DataGridTextColumn dtextcol = new DataGridTextColumn(); 
dtextcol.IsReadOnly = true; 
dtextcol.Header = xmlReader.Name; 
indexingConverter convert = new indexingConverter(); 
Binding bind = new Binding { Converter = convert, ConverterParameter = xmlReader.Name ; 
dtextcol.DisplayMemberBinding = bind; 
dg.Columns.Add(dtextcol); 
} 
} 
else 
{ 
//construct the rows 
Dictionary<string, string> d = new Dictionary<string, string>(); 
while (xmlReader.MoveToNextAttribute()) 
{ 
d.Add(xmlReader.Name, xmlReader.Value); 
} 
list.Add(d); 
} 
//move the reader to elements 
xmlReader.MoveToElement(); 
} 
} 
dg.ItemsSource = list; 
LayoutRoot.Children.Add(dg); 
} 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
constructdg(); 
} 
} 
class indexingConverter : IValueConverter 
{ 
public object Convert(object value, Type targetType, object parameter, ystem.Globalization.CultureInfo culture) 
{ 
Dictionary<string, string> columnData = (Dictionary<string, string>)value; 
return columnData[parameter.ToString()]; 
} 
public object ConvertBack(object value, Type targetType, object parameter, ystem.Globalization.CultureInfo culture) 
{ 
throw new NotImplementedException(); 
} 
} 
}
Posted by Deepak Verma | 2 Comments
Filed under:
 
Page view tracker