How do I determine the current date/time?
Use the static properties in the DateTime class to get the current date (Today property) and also in terms of ticks (Ticks property).
How do I get the install directory for the version of the runtime that is loaded in the current process?
For example, the ‘C:\WINNT\Microsoft.NET\Framework\v1.0.3705’ dir for the 1.0 framework version. You can do so using PInvoke to call this native API method: [C#] [DllImport(‘mscoree.dll’)] // Declaration internal static extern void GetCORSystemDirectory([MarshalAs(UnmanagedType.LPTStr)]System.Text.StringBuilder Buffer, int BufferLength, ref int Length); // Gets the path to the Framework directory. System.Text.StringBuilder sb = new System.Text.StringBuilder(1024); int size; // returned value in size can be ignored GetCORSystemDirectory(sb, sb.Capacity, ref size); [VB.Net] ’ Declaration Private Declare Function GetCORSystemDirectory Lib ‘mscoree.dll’ ( ByVal Buffer As System.Text.StringBuilder, ByVal BufferLength As Integer, ByRef Length As Integer) As Integer ’ Gets the path to the Framework directory. Dim Path As New System.Text.StringBuilder(1024) Dim Size As Integer ’ returned value in Size can be ignored GetCORSystemDirectory(Path, Path.Capacity, Size)
How to get the hyperlink and the hyperlink text dragged from IE in my Control’s drag-drop event?
Compiled from the newsgroup posts by Andy Fish and Brian: You can do this in the DragDrop event handler of your control: [C#] try { //Use e.Data.GetData(‘UniformResourceLocator’) to get the URL System.IO.Stream ioStream= (System.IO.Stream)e.Data.GetData(‘FileGroupDescriptor’); byte[] contents = new Byte[512]; ioStream.Read(contents,0,512); ioStream.Close(); StringBuilder sb = new StringBuilder(); //The magic number 76 is the size of that part of the //FILEGROUPDESCRIPTOR structure before the filename starts – cribbed //from another usenet post. for (int i=76; contents[i] != 0; i++) { sb.Append((char)contents[i]); } if (!sb.ToString(sb.Length-4,4).Equals(‘.url’)) { throw new Exception(‘filename does not end in ’.url’’); } filename = sb.ToString(0,sb.Length-4); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } [VB.Net] Try ’Use e.Data.GetData(‘UniformResourceLocator’) to get the URL System.IO.Stream ioStream= CType(e.Data.GetData(‘FileGroupDescriptor’), System.IO.Stream) Dim contents() As Byte = New Byte(512) {} ioStream.Read(contents,0,512) ioStream.Close() Dim sb As StringBuilder = New StringBuilder() ’The magic number 76 is the size of that part of the ’FILEGROUPDESCRIPTOR structure before the filename starts – cribbed ’from another usenet post. Dim i As Integer For i = 76 To contents(i) 0 Step i + 1 sb.Append(CType(contents(i), Char)) Next If Not sb.ToString(sb.Length-4,4).Equals(‘.url’) Then Throw New Exception(‘filename does not end in ’.url’’) End If filename = sb.ToString(0,sb.Length-4) Catch ex As Exception MessageBox.Show(ex.ToString()) End Try
Is there an Idle event which will get fired from which I could update my menu item’s enabled state?
This used to be a very useful feature in MFC. You can do something similar with the System.Windows.Forms.Application.Idle event which will get fired when the app becomes Idle. Where you could parse through all the menu items and update their states.
How come some of the objects are not initialized during deserialization?
This is possible in this case: Say your object graph contains an object A, which has a reference to the object B. Then while deserializing A, the reference B might not be initialized yet. This is because while deserializing, references are deserialized one at a time and when A is deserialized, B might not have been deserialized, yet. You should follow the workaround as follows: [C#] protected MyCustomConstrucotr(SerializationInfo info, StreamingContext context) { this.cachedRefToB = (B)info.GetValue(‘B’, typeof(B)); // At this point cachedRefToB might not be initialized. } // But when this method gets called, after complete serialization, the cachedRefToB will be initialized void IDeserializationCallback.OnDeserialization(object sender) { // At this point cachedRefToB will be initialized. } [VB.Net] protected MyCustomConstrucotr(ByVal info As SerializationInfo, ByVal context As StreamingContext) As Protected Me.cachedRefToB = CType(info.GetValue(‘B’, Type.GetType(B)), B) ’ At this point cachedRefToB might not be initialized. End Function ’ But when this method gets called, after complete serialization, the cachedRefToB will be initialized ’ Your class should implement IDeserializationCallback Sub OnDeserialization(ByVal sender As Object) as IDeserializationCallback.OnDeserialization ’ At this point cachedRefToB will be initialized. End Sub