A group blog from members of the VB team
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) _
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
Return _Description
_Description = value
Public Property Path() As String
Return _Path
_Path = value
Public Property Size() As Long
Return _Size
Set(ByVal value As Long)
_Size = value
Btw, if you bind the result to a DataGridView you can copy&paste from there into excel. Needo :-)
Cheers!
Daniel
Edited April 29, 2009: Included the "Imports" statement and did some cleanup work.
PingBack from http://microsoft-sharepoint.simplynetdev.com/how-to-get-a-list-of-all-files-of-a-directory-subdirectories-included-daniel-walzenbach/
that's not very robust or performant. ms should obsolete GetFiles(..., FileIO.SearchOption.SearchAllSubDirectories)
and give us a proper class that takes advantage of knowledge of the filesystem, the storage hardware and multiple cores and gives simple options to handle links and loops, networks, system directiories. so plinq support and lazy eval. it should also provide access to filesystem inside disk images.
Getting the *.snippet filter into the GetFiles call should improve greatly the performance (or at least I hope).
Of course then, it would make this exemple even more a poor use of Linq. Because, what's the big advantage of Linquing this code against a simple:
Dim files = My.Computer.FileSystem.GetFiles(txtPath.Text, FileIO.SearchOption.SearchAllSubDirectories, "*.snippet")
?
"It gets sorted", you might answer, yeah but it pretty much is already sorted out of the box, and you're not using the fact that filenames are sorted anyway.
Why couldn't you include the dir c:\*.* /s and > to a file?
I had to add
to read the snippets, why is that?
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
Kevin,
sorry for the confusion! I missed to include this line in the code example. I just posted the answer to your question here:
http://blogs.msdn.com/vbteam/archive/2009/04/29/when-using-linq-to-xml-why-don-t-i-get-results-if-i-don-t-import-a-xml-namespace-daniel-walzenbach.aspx
Let me know if this helps!
Thanks!
I have multiple text logs (100) in one folder.
I am looking for code to copy individual txt log contents to copy & paste in a different sheets of a workbook.
Can canyone help me ?
Bhavana
Thanks for the explaination on why the import statement was needed. Also thanks for the code in general!!!
There is an example of how to get registered code snippets at http://msdn.microsoft.com/en-us/library/bb165947(VS.80).aspx. Isn't it working in VS10?