Tuesday, May 12, 2009

Made XML file from object collection witrh LINQ

Hi,

Last time i seem in the forum, he ask a question how to make XML file from the object collection or array with LINQ.Because of LINQ has a excellent support to XML.

Here Code snippet.

To this i just take simple example.

C#.NET


Private void MakeXMlFile()
{

Employee[] employees = new[] {
new Employee {FirstName = "Joe", LastName = "Rattz", Email="Test@hotmail.com",Phone="12345343"
},
new Employee {FirstName = "Ewan", LastName = "Buckingham",
Email="Test2@hotmail.com",Phone="122232345343"}};

XElement xBookParticipants =
new XElement("Employees",
employees.Select(p =>
new XElement("Employee",
new XElement("FirstName", p.FirstName),
new XElement("LastName", p.LastName),new XElement("Email",p.Email),
new XElement("Phone",p.Phone))));
Console.WriteLine(employees);

}


VB.NET

Class Employee
Public FirstName As String
Public LastName As String
Public Email As String
Public Phone As String
End Class


private sub MakeXML()

Dim employees As Employee() = New () {New Employee(), New Employee()}

Dim xBookParticipants As New XElement("Employees", employees.[Select](Function(p) New XElement("Employee", New XElement("FirstName", p.FirstName), New XElement("LastName", p.LastName), New XElement("Email", p.Email), New XElement("Phone", p.Phone))))
Console.WriteLine(employees)
Console.ReadLine()
End Sub

Keep It Watch
Thank you

Wednesday, April 29, 2009

How to write Gridview data to XML file in .NET

Hi,

One my colleague asked me yesterday , how we could save the Grid View Data to XML File, just i think come to this point and share with you.

Just Read data from the Grid View to DataSet, then we need write that dataset ti XML file.

C#:

DataSet ds = new DataSet("TestDataset");
DataTable dt = new DataTable("TestDataTable");
dt.Columns.Add("Id", System.Type.GetType("System.String"));
ds.Tables.Add(dt);
//Add any number columns here
for (int count = 0; count < GridView1.Rows.Count; count++)
{
DataRow dr = dt.NewRow();
dr[0] = GridView1.Rows[count].Cells[0].Text;
//you can add values to all columns in datatable
dt.Rows.Add(dr);
}
ds.Tables[0].WriteXml("d:\\test2.xml");


VB.NET:

Dim ds As New DataSet("TestDataset")
Dim dt As New DataTable("TestDataTable")
dt.Columns.Add("Id", System.Type.[GetType]("System.String"))
ds.Tables.Add(dt)
'Add any number columns here
For count As Integer = 0 To GridView1.Rows.Count - 1
Dim dr As DataRow = dt.NewRow()
dr(0) = GridView1.Rows(count).Cells(0).Text
'you can add values to all columns in datatable
dt.Rows.Add(dr)
Next
ds.Tables(0).WriteXml("d:\test2.xml")


That's it

Thank you
Keep It watch.

How to pass the value when call exe in another application in .NET

Hi Guys,

I seem in forums lot's of the developer asking how to pass the arguments when they call exe within the another application in.NET. is quite easy.

Code

C#:

Process process = new Process();
process.StartInfo = new ProcessStartInfo("abc.exe");
process.StartInfo.Arguments = "folderpath "; // if you need suply multiple arguments you need put space "A B"
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();

VB.NET:

Dim process As New Process()
process.StartInfo = New ProcessStartInfo("abc.exe")
process.StartInfo.Arguments = "folderpath "
' if you need suply multiple arguments you need put space "A B"
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.Start()

Thank you
Keep It Watch

Tuesday, April 28, 2009

Get Version info in Windows mobile

Hi Guys

Last time i look most of guys asking how to get the version information in Windows Mobile with .NET Compact Framework. it pretty easy

private string GetVersionInfo()
{
Version version = Environment.Version;
return version.ToString();
}

Thank you
Keep It Watch

Friday, April 24, 2009

Search a types of file within the Directory with recursive in C#

Hi Guys,

Following code support to search types file with in the Directory including sub directories also.

public class FileSearch
{
ArrayList _extensions;
bool _recursive;
public ArrayList SearchExtensions
{
get { return _extensions; }
}
public bool Recursive
{
get { return _recursive; }
set { _recursive = value; }
}
public FileSearch()
{
_extensions = ArrayList.Synchronized(new ArrayList());
_recursive = true;
}
public FileInfo[] Search(string path)
{
DirectoryInfo root = new DirectoryInfo(path);
ArrayList subFiles = new ArrayList();
foreach (FileInfo file in root.GetFiles())
{
if (_extensions.Contains(file.Extension))
{
subFiles.Add(file);
}
}
if (_recursive)
{
foreach (DirectoryInfo directory in root.GetDirectories())
{
subFiles.AddRange(Search(directory.FullName));
}
}
return (FileInfo[])subFiles.ToArray(typeof(FileInfo));
}
}

How to Use:

FileSearch fileser = new FileSearch();
fileser.Recursive = true;
fileser.SearchExtensions.Add(".rar");
FileInfo[] dirRar = fileser.Search("D:\\test");


Thank you
Keep It Watch

Create A Complete Grid with programmetrically in C#

Hi,

This code snippet to help build windows form grid Under the Windows Presentation Foundation with Winodws Host Object.and also it C# code.

Note:Before add this code you need add Windows Host Object WPF Windows under the any parent control, and also rename to windowsFormsHostDetail.

internal void WindowsFormGridInit()
{
System.Windows.Forms.DataGridViewTextBoxColumn Column2;
System.Windows.Forms.DataGridViewCheckBoxColumn Column3;
System.Windows.Forms.DataGridViewCheckBoxColumn Column4;
System.Windows.Forms.DataGridViewTextBoxColumn Column5;
System.Windows.Forms.DataGridViewCheckBoxColumn Column6;
System.Windows.Forms.DataGridViewTextBoxColumn columnDestValueField;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();

dataGridViewDetail = new System.Windows.Forms.DataGridView();
dataGridViewDetail.AllowUserToAddRows = false;
dataGridViewDetail.DataError += new DataGridViewDataErrorEventHandler(dataGridViewDetail_DataError);
dataGridViewDetail.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridViewDetail_CellValidating);
Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
columnDestValueField = new DataGridViewTextBoxColumn();
Column3 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column4 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
Column6 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column7 = new System.Windows.Forms.DataGridViewComboBoxColumn();

((System.ComponentModel.ISupportInitialize)(dataGridViewDetail)).BeginInit();

//
// dataGridView1
//
dataGridViewCellStyle1.BackColor = System.Drawing.Color.LemonChiffon;
dataGridViewCellStyle1.ForeColor = System.Drawing.Color.Black;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.Color.Beige;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
dataGridViewDetail.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
dataGridViewDetail.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewDetail.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
Column2,
columnDestValueField,
Column3,
Column4,
Column5,
Column6,
Column7

});
dataGridViewDetail.Location = new System.Drawing.Point(40, 46);
dataGridViewDetail.Name = "dataGridView1";
dataGridViewDetail.RowHeadersVisible = false;
dataGridViewDetail.Size = new System.Drawing.Size(655, 150);
dataGridViewDetail.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
dataGridViewDetail.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Raised;
dataGridViewDetail.BackgroundColor = System.Drawing.Color.Gainsboro;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Black;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.Blue;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.White;


dataGridViewDetail.TabIndex = 0;
//
// Column2
//
Column2.HeaderText = "Destination Display Name";
Column2.Name = "Column2";
Column2.DataPropertyName = "DestinationDisplayField";
Column2.ReadOnly = true;
Column2.Width = 200;
Column2.DefaultCellStyle = dataGridViewCellStyle2;
Column2.DisplayIndex = 0;

// Destionation Value Field

columnDestValueField.HeaderText = "Destination Element Name";
columnDestValueField.Width = 150;
columnDestValueField.DefaultCellStyle = dataGridViewCellStyle2;
columnDestValueField.DataPropertyName = "DestionationValueField";
columnDestValueField.ReadOnly = true;
columnDestValueField.Width = 200;
columnDestValueField.Name = "DestValueField";
columnDestValueField.DisplayIndex = 1;
//
// Column3
//
Column3.HeaderText = "Ignore";
Column3.DataPropertyName = "IsIgnore";
Column3.Name = "Column3";
Column3.DisplayIndex = 2;
Column3.Visible = false;
//Column3.DefaultCellStyle = dataGridViewCellStyle2;
//
// Column4
//
Column4.HeaderText = "Custom";
Column4.DataPropertyName = "IsCustom";
Column4.Name = "Column4";
Column4.DisplayIndex = 3;
Column4.Visible = false;
//Column4.DefaultCellStyle = dataGridViewCellStyle2;
//
// Column5
//
Column5.HeaderText = "Values";
Column5.DataPropertyName = "CustomValue";
Column5.Name = "Column5";
Column5.DefaultCellStyle = dataGridViewCellStyle2;
columnDestValueField.DisplayIndex = 4;
Column5.Visible = false;
//
// Column6
//
Column6.HeaderText = "Selection";
Column6.DataPropertyName = "IsFixed";
Column6.Name = "Column6";
Column6.DisplayIndex = 5;
Column6.Visible = false;
//Column6.DefaultCellStyle = dataGridViewCellStyle2;
//
// Column7
//

Column7.HeaderText = "CSV Source Fields";
Column7.DisplayMember = "DisplayValue";
Column7.ValueMember = "ValueMember";
Column7.DefaultCellStyle = dataGridViewCellStyle2;
Column7.DisplayIndex = 6;
Column7.Width = 200;
Column7.Name = "Column7";
((System.ComponentModel.ISupportInitialize)(dataGridViewDetail)).EndInit();
windowsFormsHostDetail.Child = dataGridViewDetail;

}


thank you

Saturday, April 18, 2009

Get Directory Name from the File Path

Code Snippet:

public string GetDirectoryNameFromPath(string fullPath)
{
return Path.GetDirectoryName(fullPath);
}