Connection Strings for Desktop Search in Application

Okay, I have been fighting with this for about two days now.  I believe I mentioned previously that there isn’t much docuemntation on the new desktop seach for Windows Server 2008.  it turns out that what I thought to be true was in fact wrong.  I thought, and please correct me if I am wrong, that the windows search feature in Windows Server 2008 is the same as the windows search feature in Vista.  Boy I was wrong on that one.  I was trying to search for something that wasn’t there.  I just needed a different connection string is all. Here is what I used for Vista:

 

Dim connString As String = “Provider=MSIDXS.1;Integrated Security .=””

 

It works fine.  I thought it worked fine in server 2008 also.  I was trying to find a whole plethora of solutions that could fix my problem.  What I needed to change was that line of code above to:

 

Dim connString As String = “Provider=Search.CollatorDSO;Extended Properties=’Application=Windows’”

 

Go figure! The error I was getting was System.Data.OleDb.OleDbException: Service is not running.

 

That told me that the windows indexing or windows search was not running. I didn’t figure that the connection string would be wrong.  Oh and there is no catalog created when you index files on server 2008.  You have to reference them this way:

 

SELECT filename blah blah FROM systemindex..scope() WHERE SCOPE=‘file:C:\inetpub\cgweb\appname\repository’

 

You also have to not include the directory folders like this:

 

AND system.itemtype NOT LIKE ‘%Directory%’

 

I guess that can be chalked up as lessons learned.  Don’t look for the obvious, look of the unobvious. Hope this helps you.  Have fun coding and as always, if there are any questions or suggestions, they are welcome.  Thank you.

File Upload Using ASP.NET VB

I noticed or I should say it was brought to my attention that although I show how to work with files, I never discussed uploading files.  Well here it is.

 

In its simplest form, uploading a file using asp.net VB is quite simple. The following 2 links show how it is done:

 

http://msdn.microsoft.com/en-us/library/aa478971.aspx

http://support.microsoft.com/kb/323245

 

This link covers size limitations and how to circumvent it and also other things like multiple file uploads, controlling file types, and other issues.

 

On another note, if you want to see the code right here and now, just see below (this is how I do it):

 

In the display page we have the following:

 

<font face=”verdana”>Select File to Upload:</font>

<input id=”uploadedFile” type=”file” accept=”" runat=”server” />

<input type=”button” id=”upload” value=”Upload” onserverclick=”Page_Load” runat=”server” />

 

In the code behind we do this:

Make sure you import:

Imports System.IO 

Then button_click() you have:

If Not (uploadedFile.PostedFile Is Nothing) Then

Try

Dim postedFile As HttpPostedFile = uploadedFile.PostedFile

Dim filename As String = Path.GetFileName(postedFile.FileName)

Dim fileExt As String = Path.GetExtension(postedFile.FileName)

Dim contentType As String = postedFile.ContentType

Dim contentLength As Integer = postedFile.ContentLength

Dim qryString As String = Request.QueryString(“dir”)

If fileuploaded = True Then

message.Text = “The file was renamed and uploaded successfully.”

postedFile.SaveAs(currentDir & directorySeparatorChar.ToString() & filename)

End If

Catch ex As Exception

message.Text = “Failed uploading file. Please use feedback form.”

End Try

End If

 

The qryString variable would be the directory you are saving the files to.

This is how I did my qryString;

‘Find out the server name

 Dim srvrnm As String = Server.MachineName

‘Set up root directory. Doesn’t matter if live of dev, will know by above statement

Dim currentDir As String = “\\” & srvrnm & “\folder\folder”

 

To rename the files you would add the following code after the postedFile.saveAs above:

File.Move(currentDir & directorySeparatorChar.ToString() & filename, currentDir & directorySeparatorChar.ToString() & FilereName & fileExt)

 

A couple of last things, the ASPNET user needs to have permissions to write to the folder you are uploading to. Also, if you upload a file that is already there, it will not overwrite, it will throw an error. You have to try catch that error. You also have to add enctype=”multipart/form-data” to the form tag to allow file to be uploaded.

 

To allow for larger files than 4Mg you need to add the following code to the web.config file located in the application you are working on (add it inside the <system.web> node):

<httpRuntime

executionTimeout=”90″

maxRequestLength=”4096″ ß This would be changed to indicate size of file in Kb

useFullyQualifiedRedirectUrl=”false”

minFreeThreads=”8″

minLocalRequestFreeThreads=”4″

appRequestQueueLimit=”100″ />

 

That should work for you. If there are any issues or questions, post them and I will do my best to get back to you with an answer or a solution. Thank you.

Searching for Files Using ASP.NET VB

      One of the problems I had finding is searching directories for files via user input. Right now I am using a text field to allow users to enter search criteria. I may go to drop downs. The reason I may do this is because the files I am uploading are all being renamed as per the directory structure. (i.e. test.txt would be renamed to asp_ dept_station_1 with the directory structure being c:/asp/dept/station/). Then I just tack on a consecutive number at the end. But I digress. What I have found to be a good way of searching all files in the folders is to loop though all directories and add the files to an array() of type fileinfo. After that, I then loop the array and add the found files to a datalist. I have found this to work pretty well. I am now working on getting the search to allow for more than one word. That is why I may just go to dropdowns. Make sense? Anyway, the code is below for what I am doing currently. I will re-post if I decide to change it. If there are any improvements that can be made, please let me know. Thank you. Now that I think about it, I am using the same code as the post: Recursively Browse Directories Using ASP.NET VB I only added an ‘if’ loop to check for search criteria in filename:

If fi.Name.Contains(searchCriteria.ToUpper) Then
filelink = " Server.UrlEncode(dirname) + directorySeparatorChar + Server.UrlEncode(fi.Name) + "&action=download>" + fi.Name + "
"
row = myfiledt.NewRow()
row("filename") = filelink
row("creationtime") = fi.CreationTime
myfiledt.Rows.Add(row)
End If
position += 1 …

Recursively Browse Directories Using ASP.NET VB

I have come across an issue.  I have solved it part of the way.  I needed to display the contents of a directory for the user when they click on a link (a folder).  That wasn’t too much of a problem.  Now I must also display the ‘most recent files’ in a list, but instead of listing all files all the time, I want to be able to just list the 10 most recent files.  There is my problem.  I have searched the Internet. I have found a way to go through the directories.  I have found a way to display the files in the order I need by using a datagrid and a third function.   I just need to limit how many files are displayed.  There are several functions that will accomplish this.  I will share them with you so they are all in one place.  First we have the originating function call:

Public Sub GetFiles(ByVal path As String)
        If File.Exists(path) Then
            ' This path is a file
            ProcessFile(path, path)
        ElseIf Directory.Exists(path) Then
            ' This path is a directory
            ProcessDirectory(path)
        End If
    End Sub

This in turn calls the following 2 functions:

Public Sub ProcessDirectory(ByVal targetDirectory As String)
Dim dirInfo As New DirectoryInfo(targetDirectory)
Dim newfiles() As FileInfo
'need to make this global so file are added in each loop
newfiles = dirInfo.GetFiles()
Dim f As FileInfo
' Process the list of files found in the directory.
'Array.Sort(newfiles, New CompareFileInfoDESC)
For Each f In newfiles
ProcessFile(f.Name, f.DirectoryName)
Next
' Recurse into subdirectories of this directory.
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
For Each subdirectory As String In subdirectoryEntries
ProcessDirectory(subdirectory)
Next
End Sub

AND

Public Sub ProcessFile(ByVal path As String, ByVal dirname As String)
Dim fi As New FileInfo(dirname + "\" + path)
Dim filelink As String
If position <= 1 Then
Dim myDataColumn As DataColumn
myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "Filename"
myfiledt.Columns.Add(myDataColumn)
myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "Creationtime"
myfiledt.Columns.Add(myDataColumn)
End If
Dim row As DataRow
If Not dirname.EndsWith("_vti_cnf") Then
filelink = "" + fi.Name + "
"
row("filename") = filelink
row("creationtime") = fi.CreationTime
myfiledt.Rows.Add(row)
position += 1
End If
datagrid1.DataSource = AlphabeticSort(myfiledt, 1)
datagrid1.DataBind()
End Sub

The last function that I use is the sort function for the datatable. I need to sort the files by creationdate. Here is that code:


Private Function AlphabeticSort(ByVal dtTable As DataTable, ByVal sortOrder As Integer) As DataTable
Dim columnKey As String = "creationtime"
Dim sortDirection As String = ""
Dim sortFormat As String = "{0} {1}"
Select Case sortOrder
Case 0
sortDirection = "ASC"
Case 1
sortDirection = "DESC"
Case Else
sortDirection = "ASC"
End Select
dtTable.DefaultView.Sort = String.Format(sortFormat, columnKey, sortDirection)
Return dtTable.DefaultView.Table
End Function

When I add all these together I get almost what I need. Like I said, it lists all files. I need just the first 10 files to be listed. Somehow I need to add that functionality into the AlphabeticSort function to return the records I need. I thougt I would share with you what I have so far as it may help you. Any feedback is appreciated. Thank you.

Oh I almost forgot, you have to include the following lines of code above the
Public Sub Page_load.... or nothing will work:

Imports System.IO
Public Sub Page_load.....
Dim position As Integer = 1 ' for Public Sub ProcessFile ()
Dim myfiledt As DataTable = New DataTable()
Dim int As Integer = 0

Using ASP.NET VB to get all files of directory and subdirectory

Imports System.IO

Public Sub GetFiles(ByVal path As String)

        If File.Exists(path) Then

            ‘ This path is a file

            ProcessFile(path)

        ElseIf Directory.Exists(path) Then

            ‘ This path is a directory

            ProcessDirectory(path)

        End If

    End Sub

    ‘ Process all files in the directory passed in, recurse on any directories

    ‘ that are found, and process the files they contain.

    Public Sub ProcessDirectory(ByVal targetDirectory As String)

        ‘ Process the list of files found in the directory.

        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)

        For Each fileName As String In fileEntries

            ProcessFile(fileName)

        Next

        ‘ Recurse into subdirectories of this directory.

        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)

        For Each subdirectory As String In subdirectoryEntries

            ProcessDirectory(subdirectory)

        Next

    End Sub

    ‘ Insert logic for processing found files here.

    Public Sub ProcessFile(ByVal path As String)

        Dim fi As New FileInfo(path)

        Response.Write(“File Number “ + position.ToString() + “. Path: “ + path + ” <br />”)

        position += 1

End Sub

 

To use the methods just call the following method:

GetFiles(“C:\Test\”)