Helping a Remote User Locate a File on the Server

Throughout this chapter, you have created programs that let users retrieve files from specific folders on a remote server. In each case, the user had to know the folder within which the file resided. The LocateFile web service in Listing 13.6 lets a user search the server’s hard drive for a specific file. If the service locates the file, it will return the pathname of the first occurrence of the file. To create the LocateFile web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name LocateFile. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.6.

Listing 13.6 LocateFile.asmx.vb

start example
Imports System.IO Dim FileLocation As String Sub FindFile(ByVal Dir As String, ByVal File As String)   Dim DirObj As New DirectoryInfo(Dir)   Dim Files As FileInfo() = DirObj.GetFiles("*.*")   Dim Dirs As DirectoryInfo() = DirObj.GetDirectories("*.*")   Dim Filename As FileInfo   For Each Filename In Files     If (UCase(Filename.Name) = File) Then        FileLocation = Dir & "\" & File     End If   Next   If (FileLocation = "") Then     Dim DirectoryName As DirectoryInfo     For Each DirectoryName In Dirs        Try          FindFile(DirectoryName.FullName, File)        Catch E As Exception        End Try     Next   End If End Sub <WebMethod()> Public Function LocateFile(ByVal File As String) _ Ä  As String        FileLocation = ""     FindFile("C:\", UCase(File))     LocateFile = FileLocation End Function
end example

The web service’s LocateFile method calls the FindFile method, passing to the method the first directory the program will search—in this case, C:\. The FindFile method, in turn, will retrieve the files in the specified directory, comparing each file to the filename the user specified. If the FindFile method does not find the file in the current directory, it will then retrieve the subdirectories that reside in the current directory. Then, the method will recursively call itself using each directory until the file is found.

Searching a Server for a Specific File

The C# program in Listing 13.7, SearchRemote.cs, interacts with the LocateFile web service to search the server’s disk for a specific file. When you run the program, it will display a form similar to that shown in Figure 13.7 that prompts the user to specify a filename. After the user types the filename and clicks the Locate File button, the program will call the web service to search for the file.

click to expand
Figure 13.7: Searching a remote server disk for a specific file

To create the SearchRemote.cs program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual C# Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name SearchRemote. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.

  3. Using the Toolbox, drag and drop the button and text boxes, list box, label, and hyperlink label previously shown in Figure 13.6 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/LocateFile/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.7.

Listing 13.7 SearchRemote.cs

start example
private void button1_Click(object sender, System.EventArgs e) {   if (textBox1.Text.Length > 0)    {      try        {          textBox2.Text = "Searching for file";          Form1.ActiveForm.Refresh();          localhost.Service1 WS = new localhost.Service1();          string Location;          Location = WS.LocateFile(textBox1.Text);          if (Location == "")            textBox2.Text = "File not found";          else            textBox2.Text = Location;        }      catch (Exception Ex)        {          textBox2.Text = Ex.Message;        }    }  else    textBox2.Text = "Must specify a file"; }
end example

Warning 

Depending on the number of files on your disk, it is possible that the call to the web service may time out before the web service can locate the file. In Chapter 14, “Improving Web Page Performance,” you will learn how to create asynchronous web services that perform their processing and then call back the client (as opposed to the client waiting for the return value).




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net