Real World Computing
The missing LINQ
Visual Studio 2008 is certainly a worthwhile download and, although it does seeam to run a bit slower than VS2005, this seems to be because of the more frequent IntelliSense lookups it's doing. Creating an Ajax web application with dynamically updated areas on the page took me just a few clicks with no need to write any code at all - what a change from last year's efforts, where I was having to hand-craft code to get similar effects to work. Its ability to view and build data sources has improved considerably, which means you can do all your developing within Visual Studio without needing to use any separate database or XML design tools, and the enhancements to .NET Framework 3.5 reduce code development time further still. VS2008 has a much-improved design view for web pages that it renders far more accurately, and it will also allow Popfly application development from within its environment. If you want some ready-to-go websites, take a look at the .NET starter kits at www.asp.net/community/projects, which has ready-built projects for CMS, blogging, e-commerce, club sites and all sorts of other examples.
Once you have Visual Studio 2008 installed, create a new website and add a timer to the first page with its delay set to, say, 10,000 milliseconds (ten seconds). Also, add a Label object to the page, which will hold the list of files you're retrieving. From the Timer_Tick event, call a function that will return a string to your label, something like this:
Label1.Text = Main()
where Main() is going to be your function's name. Next, create a Module to hold your functions; otherwise, you'll get an error about this statement not being allowed in this namespace, so add something like:
Public Module ShowFileSystem
End Module
Now, between these Module start and end markers you need to add two functions - the first, which we'll call "GetFiles", isn't too easy to understand, but it grants the LINQ system access to the filesystem (permissions allowing, of course):
Function GetFiles(ByVal root As String) As System.Collections.Generic.IEnumerable(Of System.IO.FileInfo)
Return From file In My.Computer.FileSystem.GetFiles _
(root, FileIO.SearchOption.SearchTopLevelOnly, "*.*" ) _
Select New System.IO.FileInfo(file)
End Function
The second function you'll need is the one that the timer calls every ten seconds, and this will first call your earlier function and then query the filesystem via LINQ to return a list of files found in a certain folder to populate the label object on the web page:
Function Main() As String
' Change the drive\path to point to the folder that you wish to query
Dim root As String = "c:\my documents"
'TAKE a snapshot of the folder content via the GetFiles() function
Dim fileList = GetFiles(root)
Dim Mytext As String
Mytext = ""
' This query will produce the full path for all .txt files
' It orders the list according to the file name.
Dim fileQuery = From file In fileList _
Where file.Extension = ".txt" _
Order By file.Name _
Select file
' Now step through the returned dataset
For Each file In fileQuery
'Add whatever file/ display manipulation code you want'
Mytext = Mytext & (file.Name) & "
"
Next
' Return MyText to the calling routine in the Timer_Tick event
Main = Mytext
