Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 11th, 2006, 01:13 AM
Registered User
 
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default hei,anyone know how to check a remote file exists

hei,i try to check whether a remote file exists using C#,but using the following statement,it doesn't work:
 if (System.IO.File.Exists("http://10.0.0.1/Advertisement/adv_001.swf")),
but if i try to check the local file:
 if (System.IO.File.Exists("D:\Documents and Settings\Administrator\desktop\DEducation\DEducati on\obj\Debug\DEducation.exe")), it works well.anyone know how to check it?thanks in advance!
    peter


 
Old November 11th, 2006, 11:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

There may be a simpler way to do this, and I don’t have a remote machine to run this against (I’m using IIS locally), but what occurred to me was creating a server object running on the remote machine, and using a client proxy object to invoke System.IO.File.Exists on the remote object over an HTTP Channel.

To do that I built a solution with three projects – a console app (Client), a class library (ServerSideDLL) containting just a RemoteFileChecker class exposed via remoting, and a Web project (RemotingHost) running on IIS that gives ServerSideDLL a remote process to run in.

1. Create a blank solution, name it whatever you like.
2. Add a class library project named ServerSideDLL.
3. Add a class, and name it RemoteFileChecker. Here’s the code:

Code:
using System;

namespace ServerSideDLL
{
    public class RemoteFileChecker : MarshalByRefObject
    { 
        public string GetServerName() {
            return System.Environment.MachineName;
        }

        public int GetProcessID(){
            return System.Diagnostics.Process.GetCurrentProcess().Id;
        }

        public bool FileExists(string fileName){
            return System.IO.File.Exists(fileName);
        }
    }
}
4. Add an Empty Web project to the solution and name it RemotingHost.
5. Add a reference to the ServerSideDLL library project.
6. Add a Web.config file, and configure remoting via a <system.runtime.remoting> element, like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <service>
                <wellknown mode="SingleCall"
                           objectUri="ServerSideDLL.Rem"
                           type="ServerSideDLL.RemoteFileChecker, ServerSideDLL"/>
            </service>
        </application>
    </system.runtime.remoting>


  <system.web>

  â€¦

7. Build the solution. The remoting server should be ready to use. Test it by typing the following in a browser to view RemotingHost's WSDL specification.

http://localhost/remotinghost/serversidedll.rem?wsdl

8. Add a Console Application to the solution and name it Client.
9. Add a reference to the ServerSideDLL library project.
10. Here is the client code:

Code:
using System;

using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

namespace Client
{
    class Program
    {
        static void Main()
        {
            Hashtable properties = new Hashtable();
            properties.Add("name", "HttpBinary");
            BinaryClientFormatterSinkProvider formatter = 
                new BinaryClientFormatterSinkProvider();

            HttpChannel channel = 
                new HttpChannel(properties, formatter, null);

            RemotingConfiguration.RegisterWellKnownClientType(
                typeof(ServerSideDLL.RemoteFileChecker),
                "http://localhost/remotinghost/serversidedll.rem");

            System.Text.StringBuilder output = new System.Text.StringBuilder();
            ServerSideDLL.RemoteFileChecker remoteFileChecker = new ServerSideDLL.RemoteFileChecker();

            // Identify remote process the ServerSideDLL assembly is running in.
            output.AppendFormat("ServerSideDLL values\n");
            output.AppendFormat("Machine: {0}\n", remoteFileChecker.GetServerName());
            output.AppendFormat("Process: {0}\n", remoteFileChecker.GetProcessID());

            // Identify the local process the Client assembly is running in.
            output.AppendFormat("\nClient values\n");
            output.AppendFormat("Machine: {0}\n", System.Environment.MachineName);
            output.AppendFormat("Process: {0}\n", 
                System.Diagnostics.Process.GetCurrentProcess().Id);

            // Use remote object to verify files existence.
            output.AppendFormat("\nFile located by remote object: {0}", 
                remoteFileChecker.FileExists(@"\Inetpub\wwwroot\RemotingHost\bin\TheFile.txt"));

            Console.WriteLine(output.ToString(), "Client");
            Console.Read();
        }
    }
}
I just stuck a text file named “TheFile” in RemotingHost’s bin directory.

11. Set the Client project as the Solutions Startup Project.
12. Here’s my output:

Code:
ServerSideDLL values
Machine: ELIOT
Process: 2772

Client values
Machine: ELIOT
Process: 3744

File located by remote object: True
As I mentioned, I’m not actually accessing a remote physical machine, just communicating across process boundaries. But since that’s basically what “accessing a remote physical machine” entails, I suspect this would work. If this is completely wrong-headed, someome feel free to scold me.

And a bow to Rockford Lhotka for his wonderful explanations of the remoting subsystem.


HTH,

Bob




 
Old November 21st, 2006, 03:54 AM
Registered User
 
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

to bob:
  thank u so much

 
Old November 28th, 2006, 02:55 AM
Registered User
 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
i am using remoting to upload files on server.
my remote object is hosted on IIS.
i want to create the uploaded file following path from the root directory on the server \content\uploadedFiles
pls help me.






Similar Threads
Thread Thread Starter Forum Replies Last Post
document() - check if a file exists N.Todd XSLT 2 September 19th, 2007 05:50 AM
Check if a file exists on network darrenb Access VBA 2 March 27th, 2007 07:15 PM
Determining if a Remote File Exists [email protected] VB How-To 1 January 5th, 2007 11:56 PM
check first to see if the file exists crmpicco Classic ASP Professional 2 December 1st, 2005 12:34 PM
checking file exists on remote server pete_m Classic ASP Basics 11 August 2nd, 2004 02:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.