Using an enum type with WCF services
I recently started to work with Windows Communication Foundation and one of the first problems I did run into was how to use enum type in WCF.
This is a small example of a data contract and a service contract/interface that makes use of the enum type.
using System; using System.ServiceModel; using System.Runtime.Serialization; namespace Test.DataContracts { [DataContract(Namespace = "Testing", Name = "JobResult")] public enum JobResult { [EnumMember] Passed = 0, [EnumMember] Failed = 1, [EnumMember] Running = 2, [EnumMember] TestFailure = 3, [EnumMember] Queued = 4, [EnumMember] JobNotFound = 5 } }
CSV parsing using OLEDB
For a few days I did run into the problem of importing CSV data with OLEDB. The first thing I tried was creating my own parser, but later I found an easy built in way. I show both methods here.
My own parser is simple based on the split string functionality.
//create the table
DataTable dt = new DataTable("Import");
dt.Columns.Add("col1", typeof(string));
dt.Columns.Add("col2", typeof(string));
dt.Columns.Add("col3", typeof(string));
//read the data
using (StreamReader sr = new StreamReader(filepath))
{
int i = 0;
while (!sr.EndOfStream)
{
//skip the header
while (i < 1)
{
sr.ReadLine();
i++;
}
string[] text = sr.ReadLine().Split(',');
DataRow dr = dt.NewRow();
dr["col1"] = text[0];
dr["col2"] = text[1];
dr["col3"] = text[2];
dt.Rows.Add(dr);
}
}The build in CVS parser uses a connection string and a SQL query to handle the CVS data. The connection string is configured according to:
• Provider: Jet OLEDB
• Data source: The directory that contains the CSV file
• Extended properties: ‘text’ means that we are parsing a text, the HDR property can be set to ‘Yes’ (the first row in the CSV files is header information) or ‘No’ (there is no header row), and setting the FMT property set to ‘Delimited’ essentially says that we will be working with a comma separated value file.
public static DataTable ParseCSV(string filePath)
{
if (!File.Exists(filePath))
{
return null;
}
string fileName = Path.GetFileName(filePath);
string dirName = Path.GetDirectoryName(filePath);
//connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="" + dirName + "\";" + "Extended Properties="text;HDR=No;FMT=Delimited"";
string query = "SELECT * FROM " + fileName;
DataTable dt = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, connString);
try
{
adapter.Fill(dt);
}
catch (InvalidOperationException)
{ }
adapter.Dispose();
return dt;
}UI automation with accessibility object and win 32 API
In my earlier blogs (Fun with UIAutomation and calc, Fun with UIAutomation and calc 2 (event handling)) I have been talking about how to user uiautomation in .NET. Now it time to dig down bellow uiautomation and go directly to the source WIN 32 API. I have tried to translate the first calculator project to use win 32 API and accessibility object instead. The first thing that we need to-do is to create DllImport statement for user32.dll used for handling more or less all windows UI and oleacc.dll used for accessing the accessibility object. You will find my complete solution in the end of the blog.
An example of DllImport is EnumChildWindows that gives use the possibility to get all child windows to a specific window.
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);