Posts

Showing posts with the label dotnet

Microsoft.NET Framework directory locations

Since there are directories all over the place for the Microsoft.NET Framework, I have decided to compile a list of some of the key ones used. Compilers, GACUtil, ASP.NET Register IIS/SQL, Assemblies etc. .NET 1.0: C:\Windows\Microsoft.NET\Framework\v1.0.3705 .NET 1.1: C:\Windows\Microsoft.NET\Framework\v1.1.4322 .NET 2.0: C:\Windows\Microsoft.NET\Framework\v2.0.50727 After 2.0, more directories were added, the build number was also omitted from the directory name. Some key utilities (for Global Assembly Cache (GAC), ASP.NET, Code Access Security Policy Tool (caspol.exe)) weren't part of future versions, so, as a result, .NET 2.0 is required by them. .NET 3.0 uses .NET 2.0 compiler. New directories: C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation C:\Windows\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation (only SQL scripts in here) C:\Windows\Microsoft.NET\Framework\v3.0\WPF (Windows Presentation Foundation) C:\Program Files\Reference A...

Easy database querying with dOOdads (C#, .NET)

Been some time since I posted about MyGeneration and dOOdads. Still using it regularly, even though it is a few years old now. A few sites I do still use .NET 1.1, so it is still in use. A brief (re)introduction - MyGeneration is a tool that can generate code for you, through the use of templates (you can create your own, and there is a template library ). It comes bundled with the dOOdads data abstraction library, which allows you to update and query your database without knowing the intricacies of interacting with it (i.e. .NET classes to use, SQL to write etc). Both MyGeneration and dOOdads support multiple databases (SQL Server, MySQL, Oracle etc) and regeneration of code, changing the connection string is often all that is needed to move to another system. It was freeware initially, but is now hosted on SourceForge and no longer actively maintained by the main developers. A basic example of a query would be: Employees e = new Employees(); e.Where.EmployeeID.Value = 1; if(e.Q...

Link: HTML Agility Pack (.NET)

The HTML Agility Pack is an HTML parser for .NET, supporting XPATH and XSLT parsing. Basically the HTML equivalent of XmlDocument . Example use (from site): HtmlDocument doc = new HtmlDocument(); doc.Load("file.htm"); foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]") { HtmlAttribute att = link["href"]; att.Value = FixLink(att); } doc.Save("file.htm");

Link: A list of .NET Cheat Sheets

.NET Cheat Sheets includes ".NET Format String Quick Reference", "ASP.NET 2.0 Page Life Cycle & Common Events", "Visual Studio 2005 Built-in Code Snippets (C#)" as well as links to several more.

Sorting Files by Name, Date, FileSize etc (C#)

It is simple to get a list of files in .NET, you simply do: string folder = "c:\\windows\\"; string[] files = Directory.GetFiles(folder); This will get all the files in C:\Windows (does not include subdirectories). However, they are sorted by name (A-Z) and there aren't any options in the GetFiles method to return them in any other order. Luckily, the results are returned as an array and they can be sorted with a custom comparer. I have created a FileComparer class that can be used to sort the files (works in both .NET 1.1 and 2.0): public class FileComparer : IComparer { public enum CompareBy { Name /* a-z */, LastWriteTime /* oldest to newest */, CreationTime /* oldest to newest */, LastAccessTime /* oldest to newest */, FileSize /* smallest first */ } // default comparison int _CompareBy = (int)CompareBy.Name; public FileComparer() { } public FileComparer(CompareBy compareBy) { _CompareBy = (int)compareBy; } int IComparer.Compare( obj...

FileHelpers - a library for worked with CSV files (.NET)

Best described in the words on the FileHelpers website : The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams. It is a library that can be used to work with flat text files (e.g. Comma Separated Value (CSV) or Tab Separated Values (TSV) etc). Available for commercial and non-commercial use (under the LGPL). They also have a blog . There is also a CSV parser on CodeProject if you can't use a library licensed under the LGPL: A Fast CSV Reader . Not as powerful as FileHelpers but still good nonetheless.

Sending emails with C#

I'm sure many may already know how to do this, but for those that don't, sending an email using .NET is very easy and can be used in ASP.NET as well as Windows Forms or Console applications (assuming you have CDOSYS installed). This example also shows how to send an email to multiple recipients (requires you to import the namespace System.Collections ). .NET 1.0 / 1.1 (import the namespace System.Web.Mail ) MailMessage m; SmtpMail.SmtpServer = "smtpservername"; ArrayList recipients = new ArrayList(); recipients.Add("fred@bloggs.com"); recipients.Add("jane@doe.com"); for (int i = 0;i < recipients.Count;i++) { m = new MailMessage(); m.To = recipients[i].ToString(); m.From = "me@mysite.com"; m.BodyFormat = MailFormat.Html; m.Subject = "Subject of the email"; m.Body = String.Format( "<p style='font: 12px Arial'>Sending email to {0}</p>", recipients[i] ); SmtpMail.Send(m); } .NET 2.0+ ...

Get remote page contents (ASPX C#)

A simple function for getting the contents of another page in your code behind private string GetHTML(string url) { WebRequest request = WebRequest.Create(url); // use logged in user credentials request.Credentials = CredentialCache.DefaultCredentials; try { // get the response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // set the content length int contentLength = (int)response.ContentLength; // get the stream Stream responseStream = response.GetResponseStream(); // Pipes the stream to a higher level stream reader with the required encoding format. StreamReader readStream = new StreamReader(responseStream, Response.ContentEncoding); // create a char array char[] data = new char[contentLength]; // load from the stream into the char array readStream.Read(data, 0, contentLength); // return the data as a string return new String(data); } catch(Exception ex) { return string.Empty; } return string.Empty; }

Mono Migration Analyzer (MoMA)

Mono Migration Analyzer (MoMA) is an application (requiring .NET 2.0 or Mono 1.2) that you can use to analyse your assemblies (.exe, .dll) to see if there are any incompatibilities with Mono. While it can't analyse web pages (.aspx, .ascx, .ashx etc), it can be used on any assemblies compiled for use within your website that are stored in the bin folder under your site root. Once done, you can submit a report (that only contains methods you call that are not implemented) which will help the Mono team prioritise their work (as it reflects real world applications).

iTextSharp: Generate a PDF file containing a table (ASP.NET/C#)

Contining on from iTextSharp: Generating a Basic PDF file (ASP.NET/C#) , here is a demo of how to generate a PDF document with a table in it. TablePDF.ashx <%@ WebHandler Language="C#" Class="MyNamespace.TablePDF" %> using System; using System.IO; using System.Web; using iTextSharp.text; using iTextSharp.text.pdf; namespace MyNamespace { public class TablePDF: IHttpHandler { public bool IsReusable { get { return true; } } /// <summary> /// Font used for table headers /// </summary> private Font TableHeaderFont { get { return new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD); } } public void ProcessRequest(HttpContext ctx) { // make sure it is sent as a PDF ctx.Response.ContentType="application/pdf"; // make sure it is downloaded rather than viewed in the browser window ctx.Response.AddHeader("Content-disposition", "attachment; filename=TablePDF.pd...

iTextSharp: Generating a Basic PDF file (ASP.NET/C#)

iTextSharp is free library for .NET that allows you to create PDF documents. It can be used to dynamically create PDF's which can be streamed to the user. As there is no HTML being sent to the user, a WebHandler (ashx file) is a more appropriate way to generate your PDF than an aspx page. Download itextsharp-3.0.10-dll.zip (latest version as of 08 Feb 2006) and save the dll in the archive to your websites bin directory. Here is a basic sample of creating a PDF (more complex samples may follow in future posts). BasicPDF.ashx <%@ WebHandler Language="C#" Class="MyNamespace.BasicPDF" %> using System; using System.IO; using System.Web; using iTextSharp.text; using iTextSharp.text.pdf; namespace MyNamespace { public class BasicPDF: IHttpHandler { public bool IsReusable { get { return true; } } /// <summary> /// Font used for any hyperlinks added to the PDF /// </summary> private Font LinkFont { get ...

PDF Database Report (MyGeneration)

PDF Database Report is a template for the MyGeneration code generator. It uses iTextSharp to generate the document. The report includes tables, foreign keys, indexes and views. Details for each column (name, data type, is nullable etc) is shown for each table and view selected. Tags: Web Developer Blog , MyGeneration , iTextSharp