Se afișează postările cu eticheta C#. Afișați toate postările
Se afișează postările cu eticheta C#. Afișați toate postările

duminică, 29 septembrie 2013

Unable to Access the Properties Window in Visual Studio

I had my part of frustration when I fond I could not get hold of the Properties Window in Visual Studio 2005. No matter whether I tried bringing up the window by accessing the menu entry or using the key combination, I could not see the window even though the current view seemed to be switching focus to a new window.
This had to be something about settings, because when I was running a debugging session, the Properties window would be displayed.
I had two hypotheses, with the help of Spy++. I could see the window listed under Visual Studio 2005 in Spy++ so it had to be somewhere.
Inspecting the properties of the Visual Studio 2005 'Properties' window, I got:
So:
1. The window is either off screen and/or
2. The window is of zero width as shown in the rectangle properties.

I tried locating registry settings to specify the location and/ or dimension of the Properties window, but without success.
My solution was to restore Visual Studio's settings for C# development environment (which I spend most of my time in) and this restored the views of all windows to default and now I can access the Properties window as before. ( see this Microsoft How To for details on how to restore your settings)

The new Window Properties in Spy++ now look like this:





After I wrote this post I found this blog entry suggesting a solution for the situation when the 'Properties' window is off screen.

marți, 11 septembrie 2012

Fast Facts - BinaryReader

This post is about the BinaryReader class and the ReadBytes method return value.

public virtual byte[] ReadBytes( int count )


This method returns an array of count bytes or the number of bytes left to the end of the underlying stream, whichever is less.
When there are no more bytes to read, calling ReadBytes return an empty array of bytes, not a null value. This is useful to know when verifying the outcome of a read operation.
To prove this, there is a quick C# method that you can load in LINQPad:



public static void Main()
{
    const int arrayLength = 20;

    // Create random data to write to the stream. 
    byte[] dataArray = new byte[arrayLength];
    new Random().NextBytes(dataArray);

    BinaryWriter binWriter = new BinaryWriter(new MemoryStream());

    // Write the data to the stream.
    Console.WriteLine("Writing the data.");
    binWriter.Write(dataArray);

    // Create the reader using the stream from the writer.
    BinaryReader binReader = 
        new BinaryReader(binWriter.BaseStream);

    // Set Position to the beginning of the stream.
    binReader.BaseStream.Position = 0;

    // Read and verify the data. 
    byte[] verifyArray = null;
    for( int i = 0; i < 6; i++ )
    {
        // ReadBytes returns an empty array when there are no more bytes to read.
        verifyArray = binReader.ReadBytes(sizeof(uint));
        Console.WriteLine( (verifyArray!=null) ? "Length = " + verifyArray.Length : "Array is null" );
        verifyArray = null;
    }    
}

Running this program will display:

Writing the data.
Length = 4
Length = 4
Length = 4
Length = 4
Length = 4
Length = 0


vineri, 31 august 2012

DllImport default path

When using DllImport for interop in C#, the default path of the library that you want to import is dictated by the operating system. The search order used by Windows OS depends on whether SafeDllSearchMode is enabled or disabled. When the safe mode is enabled, the current directory is placed lower in the search order. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx for complete information on Dinamyc-Link Library Search Order.

Beside the implicit search order described below, one can use the .dll full path with the DllImport attribute.

The search order when safe mode is enabled is:

1.The directory from which the application loaded.

2.The system directory. Use the GetSystemDirectory function to get the path of this directory.

3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.

4.The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.

5.The current directory.

6.The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

And when the safe mode is disabled:

1.The directory from which the application loaded.

2.The current directory.

3.The system directory. Use the GetSystemDirectory function to get the path of this directory.

4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.

5.The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.

6.The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

I have successfully used the directory from which the application is loaded, but it will be interesting to give a try to the other modes. Probably the most efficient is the use the first option most of the time for better performance, just because it's the first place the OS looks for the .dll. Knowing where the OS will look for the .dll is essential and a point to remember.

joi, 31 martie 2011

Serialize an array to XML in .NET Framework

Well, this is not too much of a big deal since with a little knowledge about Serialization you will be able to figure this out quite rapidly. I only thought of posting this here since I had in mind the issue of converting an array of (string) values into an XML format and then have it deserialized at a later time when I would have need the array again. For this purpose I used the XmlSerializer class and the MemoryStream class.
The example code I give below shows how to convert a string array to XML and then how to deserialize it from the XML format into a new array.

public static void SerializeDeserializeArray( )
{
    string[ ] strArr = new string[ ] {
        "C:\\Program Files\\myFile1.txt",
        "C:\\Program Files\\myFile2.txt",
        "C:\\Program Files\\myFile3.txt"
    };

    MemoryStream memStream = new MemoryStream( );
    StreamWriter sw = new StreamWriter( memStream );

    XmlSerializer ser = new XmlSerializer( typeof( string[ ] ) );
    ser.Serialize( sw, strArr );

    StreamReader sr = new StreamReader( memStream );

    memStream.Position = 0;
    string str = sr.ReadToEnd( );

    memStream.Position = 0;
    string[ ] deserArr = (string[ ] )ser.Deserialize( memStream );

    if( deserArr != null )
    {
        foreach( string s in deserArr )
        {
            Console.WriteLine( s );
        }
    }

    Console.WriteLine( );
}

miercuri, 12 ianuarie 2011

How to open a .pdf or .txt file in C#

This is a simple question, with a very simple answer. I've been asking it myself and seeing other people ask it. A solution is to use the System.Diagnostics.Process.Start( ) method.
According to MSDN, using this method is very similar to typing the file's name in the Run dialog box of the Windows start menu.
For instance, if you have a .pdf file that you want to open, just use:
System.Diagnostics.Process.Start( "myfile.pdf" );
You can follow this link for more information: http://msdn.microsoft.com/en-us/library/53ezey2s.aspx.

For a text file, you can use the above method, or if you want to specifically open it with notepad you have to use:
System.Diagnostics.Process.Start( "notepad.exe", "myfile.txt" );