April, 2009

  • <dw:daniel_walzenbach runat="server" />

    How to get a list of all files of a directory (subdirectories included)?

    • 4 Comments

    I needed to create a list of all Code Snippets we ship in Visual Studio the other day containing their title, description and path on disc and size. As you might know, Code Snippets are stored in multiple directories below "%ProgramFiles%\Microsoft Visual Studio 10.0" so I had to traverse all the subdirectories of the aforementioned path to find all snippets.

    Fortunately – linq to the rescue – this came down to a few lines of code :-)

    Dim query = From file In My.Computer.FileSystem.GetFiles("C:\Program Files\Microsoft Visual Studio 10.0", FileIO.SearchOption.SearchAllSubDirectories) _

                Where file.EndsWith(".snippet") _

                Order By file

    This statement gives you a List of Strings (or, to be a bit more precise, a System.Linq.IOrderedEnumerable(Of String)) which you can walk over to do all kinds of crazy things.

    Here is what I did to solve the problem I described above:

    Imports <xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> 

     

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim query = From file In My.Computer.FileSystem.GetFiles(txtPath.Text, FileIO.SearchOption.SearchAllSubDirectories) _

                        Where file.EndsWith(".snippet") _

                        Order By file

            Dim snippets As New List(Of Snippet)

            Dim snippetDocument As XElement

            Dim snippet As Snippet

            For Each item In query

                snippetDocument = XElement.Load(item)

                If snippetDocument...<Title>.Value IsNot Nothing Then

                    snippet = New Snippet With {.Title = snippetDocument...<Title>.Value.ToString _

                                                , .Description = snippetDocument...<Description>.Value.ToString _

                                                , .Path = item _

                                                , .Size = New System.IO.FileInfo(item).Length}

                    snippets.Add(snippet)

                End If

            Next

            DataGridViewSnippets.AutoGenerateColumns = True

            DataGridViewSnippets.DataSource = snippets

        End Sub

    End Class

     

    Public Class Snippet

        Private _Title As String

        Private _Description As String

        Private _Path As String

        Private _Size As Long

        Public Property Title() As String

            Get

                Return _Title

            End Get

            Set(ByVal value As String)

                _Title = value

            End Set

        End Property

        Public Property Description() As String

            Get

                Return _Description

            End Get

            Set(ByVal value As String)

                _Description = value

            End Set

        End Property

        Public Property Path() As String

            Get

                Return _Path

            End Get

            Set(ByVal value As String)

                _Path = value

            End Set

        End Property

        Public Property Size() As Long

            Get

                Return _Size

            End Get

            Set(ByVal value As Long)

                _Size = value

            End Set

        End Property

    End Class

    Btw, if you bind the result to a DataGridView you can copy&paste from there into excel. Needo :-)

    Cheers!

       Daniel

    P.S. If you'd like to understand why you need the Imports <xmlns="http... at the beginning of the code check the follow-up article I wrote.

    P.P.S. You can download the source code from the MSDN Code Gallery.

    Edited April 29, 2009: Included the "Imports" statement and did some cleanup work.

  • <dw:daniel_walzenbach runat="server" />

    Virtual Earth Silverlight Map Control CTP available :-)

    • 3 Comments

    The Virtual Earth Silverlight™ Map Control, enables a new level of performance without document object model (DOM) constraints. The release allows users to enhance their mapping and data visualization applications with powerful extensions, including high-fidelity video, animation, and vector graphics. The use of Silverlight also provides a development environment working with managed code, which allows for development, unit testing and debugging with Visual Studio 2008.

    Integration with Detailed Virtual Earth Features

    • Build and deploy road, aerial, and hybrid map views, with Silverlight in the .NET framework.
    • Engage with the interactive map view control with mouse and keyboard panning and zooming, set the map view programmatically and create new perspectives with integrated world wrap.
    • Build and overlay multiple robust and animated maps, and clusters of maps, quickly and more easily, and design custom images and more easily build complex code through Visual Studio.
    • Add shapes such as polygons and polylines to enrich maps and add custom tile layers using MapCruncher.

    Seamless Development and Rich Toolkit

    • Use a single toolkit for building next generation user experiences, incorporating video, smooth zooming, and more—all using low bandwidth.
    • Easily integrate Silverlight-based applications with existing Web applications, without any ripping and replacing of content. With Silverlight, developers can easily incorporate services and data from anywhere, using Virtual Earth Web Services or AJAX to deliver stunning data-driven experiences.
    • Extend your reach with built-in support for multiple browsers, including Internet Explorer 8 and Firefox 3.
    • Embed rich geovisualization capabilities into your existing Silverlight applications or create new rich internet applications (RIAs).

    Rich, Interactive Silverlight Applications

    • End users can now experience Virtual Earth applications that feature the “deep zoom” capabilities of Silverlight, including fly-through and scaling views of landmarks, properties, and more. The availability of fluid video imagery can create immersive, highly engaging end-user experiences that bring location-based information to life.
    • Developers can provide a new navigation paradigm for multimedia collections by overlaying them as points on a map, creating halo and fade-in effects, dragging and dropping images, and drawing on maps.
    • Designers can extend applications by embedding brand-enhancing multimedia, advertising, and more into the map.

    To begin developing with Virtual Earth Silverlight Control CTP, visit Microsoft Connect: http://connect.microsoft.com/silverlightmapcontrolctp

     

    I got the bits a while ago and have to say that I’m more than pleased ! Unfortunately back than they were still under NDA as we intended to disclose them at MIX so you can imagine my excitement that I’m finally allowed to share the goodness with you :-) (and that I can release a Silverlight version of Where is Daniel soon :-) if I find some time…)

     

    Cheers!

        Daniel

  • <dw:daniel_walzenbach runat="server" />

    When using LINQ to XML why don’t I get results if I don’t import a XML namespace?

    • 2 Comments

    Hi,

    I posted a bit of code the other day which I used to get a list of all Code Snippets we ship in Visual Studio. In a nutshell, I used XElement.Load to create a new XML document from a filename from which I then read elements from (There are way to many ”from” in this sentence ;-) ). Unfortunately, I forgot to import the XML Namespace in the code I posted which caused my code not to return any results (but instead provided me with a brilliant opportunity to write another post. hehe :-) ).

    Let’s have a look at the code which is causing trouble and a Code Snippets to understand what’s going on (you can find the complete code including the Snippet class in the aforementioned post):

    Dim query = _

        From file In My.Computer.FileSystem.GetFiles( _

            "C:\Program Files\Microsoft Visual Studio 10.0", _

            FileIO.SearchOption.SearchAllSubDirectories) _

        Where file.EndsWith(".snippet") _

        Order By file

     

    Dim snippets As New List(Of Snippet)

    Dim snippetDocument As XElement

    Dim snippet As Snippet

     

    For Each item In query

        snippetDocument = XElement.Load(item)

        If snippetDocument...<Title>.Value IsNot Nothing Then

     

            snippet = New Snippet With {.Title = snippetDocument...<Title>.Value.ToString _

                                        , .Description = snippetDocument...<Description>.Value.ToString _

                                        , .Path = item _

                                        , .Size = New System.IO.FileInfo(item).Length}

            snippets.Add(snippet)

        End If

    Next

      Code Snippet

    <?xml version="1.0" encoding="utf-8"?>

    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

      <CodeSnippet Format="1.0.0">

        <Header>

          <Title>Activate a Running Application by Name</Title>

          <Author>Microsoft Corporation</Author>

          <Description>Activates a running application using the name of the application.</Description>

          <Shortcut>appActNa</Shortcut>

        </Header>

        <Snippet>

          <Imports>

            <Import>

              <Namespace>Microsoft.VisualBasic</Namespace>

            </Import>

          </Imports>

          <Declarations>

            <Literal>

              <ID>applicationName</ID>

              <Type>String</Type>

              <ToolTip>Replace with the name of the application. This is often the title of the application window.</ToolTip>

              <Default>"Untitled - Notepad"</Default>

            </Literal>

          </Declarations>

          <Code Language="VB" Kind="method body"><![CDATA[AppActivate($applicationName$)]]></Code>

        </Snippet>

      </CodeSnippet>

    </CodeSnippets>

    As you can see there is a <Title> element in the the Code Snippet to which we are referring to in the code above… or are we?

    Let’s dig a bit deeper, open the compiled program in .NET Reflector and disassemble it (if you don’t have .NET Reflector you have to stop reading NOW and get it!! Seriously, this tool is a life-safer and I’ve learned soooo much using it!).

    If you focus on the parts I highlighted in red in the above picture you can see that <Title> consists out of the XML name and the XML namespace (check out XName..::.Get Method (String, String)). Since I didn’t import the namespace in my program an empty namespace got used resulting in the qualified name Title which doesn’t fit the namespace of the Code Snippet {http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet}Title, hence my program didn’t return any results.

    To solve this problem I added the following line of code to my program

    Imports <xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

    and – finally – all is good :-)

    Hopefully this solved the mystery of the missing results ;-)

    Best!

        Daniel

  • <dw:daniel_walzenbach runat="server" />

    How to manage your parent’s/friend’s computer (The destiny of every computer geek)?

    • 2 Comments

    Ever since I remember I was in charge of my father’s computer (Well, to be honest, there was a time when I tried to install Skool Daze – a Commodore 64 game – on his brand new XT where he was ahead in the game but this quickly changed ;-) ).

    Taking care of my father’s computer was an easy talk while we lived together in the same house but got significantly more challenging when I moved out, especially now that I’m in the US and my parents are in Germany. Since “remote controlling” my father on the phone wasn’t as easy as I hoped it to be (without going into details, I guess you know what I’m talking about…) I tried a couple of different programs including “Remote Assistance”, vnc, msn messenger and others over the time which all had different constraints. Without complaining to much none of them truly satisfied me. Fortunately though a friend introduced me to TeamViewer while we were struggling to setup his development environment and I’m using it ever since. Like Paint.NET, .NET Reflector, Fiddler, Process Explorer, FileZilla, … this is a “must have” tool for everybody doing IT-related work and getting regular requests from family and friends about “why the printer isn’t working anymore” or similar issues (I should probably write a “Must have tools” post).

    To me it came down to a couple of things I liked about TeamViewer:

    • Didn’t have to worry worry about firewalls, IP addresses, NAT. It just worked
    • Free for private use
    • Can be run without being installed
    • Can be installed to run as a service on your parent’s computer which allows you to connect to this computer as long as it’s online

    If you’re intrigued and have the same need (otherwise, what are you doing here ;-) ) give it a try and share your thoughts.

    Happy administration!

       Daniel

  • <dw:daniel_walzenbach runat="server" />

    Two free e-books available till April 22th to celebrate MS Press’s 25th b-day

    • 1 Comments

    2009 is the 25th anniversary of Microsoft Press! To celebrate their b-bay they’re giving away two free e-books, namely

    clip_image002 clip_image003

    Get them as soon as the offer lasts :-)

    Daniel

Page 1 of 1 (5 items)