Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Summary
Great tips for .NET developers.
Description

Determine Mime Type for Server Side Files

Joe had a problem. He had a restricted folder from which he was allowing authorized downloading through a response.binarywrite. It is fairly straight forward stuff. But, he didn't want to write a big switch statement based on the various file extensions in order to determine the file type for the Response.ContentType = "xx" statement.

5 and a half hours later he answered his own question:

System.Runtime.InteropServices.DllImport("urlmon.dll", 
   CharSet = System.Runtime.InteropServices.CharSet.Unicode, 
   ExactSpelling = true, SetLastError = false)] 
static extern int FindMimeFromData(IntPtr pBC, 
   [System.Runtime.InteropServices.MarshalAs(
      System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pwzUrl,
   [System.Runtime.InteropServices.MarshalAs(
      System.Runtime.InteropServices.UnmanagedType.LPArray, 
      ArraySubType = System.Runtime.InteropServices.UnmanagedType.I1, SizeParamIndex = 3)] 
   byte[] pBuffer, int cbSize,
   [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] 
   string pwzMimeProposed, int dwMimeFlags, out IntPtr ppwzMimeOut, int dwReserved);    
    
    /// <summary>
    /// Ensures that file exists and retrieves the content type 
    /// </summary>
    /// <param name="strfileName"></param>
    /// <returns>Returns the Content MimeType </returns>
    public static string getMimeFromFile(string strFileName)
    {
        IntPtr mimeout;
        if (!System.IO.File.Exists(strFileName))
            throw new System.IO.FileNotFoundException(strFileName + " not found");

        int MaxContent = (int)new System.IO.FileInfo(strFileName).Length;
        if (MaxContent > 4096) MaxContent = 4096;
        System.IO.FileStream fs = System.IO.File.OpenRead(strFileName);


        byte[] buf = new byte[MaxContent];
        fs.Read(buf, 0, MaxContent);
        fs.Close();
        int result = FindMimeFromData(IntPtr.Zero, strFileName, buf, 
           MaxContent, null, 0, out mimeout, 0);

        if (result != 0)
            throw System.Runtime.InteropServices.Marshal.GetExceptionForHR(result);

        string mime = System.Runtime.InteropServices.Marshal.PtrToStringUni(mimeout);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(mimeout);
        return mime;
    }
    //rename crystal.jpg to .gif to test functionality!
    //string getImg = Environment.GetEnvironmentVariable("windir") 
    //   + "\\Web\\WallPaper\\Crystal.gif";
    //string mime = getMimeFromFile(getImg);

from a solution provided by Joe Johnston in Message #1167048

Getting Database Schema
There are several different ways to get schema information from the database. Both of the following methods return a DataTable containing information for every column in the table you are interested in.

1) If you're using .NET 1.1, you can use the .GetSchemaTable() method of the DataReader class (either SqlDataReader or OleDbDataReader), as follows:

SqlCommand sc = new SqlCommand("select * from personnel", new SqlConnection(MyConnectionString));
sc.Connection.Open();
SqlDataReader drSql = sc.ExecuteReader(CommandBehavior.SchemaOnly);
DataTable test = drSql.GetSchemaTable();
sc.Connection.Close();

2) If you're using .NET 2.0, I think it's easier to use the new .GetSchema() method of the Connection class (SqlConnection or OleDbConnection), as follows:
SqlConnection connection = new SqlConnection(MyConnectionString);
connection.Open();
DataTable test = connection.GetSchema("Columns", new string[] { null, null, "personnel" });
connection.Close();

By using the connection.GetSchema() method with no parameters, one can use the resulting DataTable to see what other kinds of schema information is available. Play with it!!

from two solutions, provided by Çetin Basöz and Richard Hamm in Messages #1213367 and #1210023

How To Not Lose Controls on a Web Page
When you dynamically add controls to your web page in the code behind, it must be done in the Page_Init(), otherwise when the Page does a PostBack, they will not be re-added. By the time the Page_Load() fires, POST back assignment (as well as Viewstate) have already occurred.

from a solution provided by Rick Strahl in Message #1123792

Changing Property Values When Loading a Form/Control
How do you change things like the Title (this.Text) of a Form dynamically? A common mistake is to include it in the constructor. If it's put before the InitializeComponent() you'll have a problem.

You can't set anything that is set in the property sheet in the constructor. The constructor will be overridden by anything in InitalizeComponent(). If you move that code after the InitializeComponent() it should work.

Ideally you shouldn't do anything dynamic in the constructor at all - use Form_Load for those sort of things as it ensures that the form has properly initialized and all values have been set.

from a solution provided by Rick Strahl in Message #1233163

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers