ASP.NET - how to find path or directory or file in remote machine using c#

Asked By anbu n on 29-Sep-11 01:50 AM
  if (System.IO.Directory.Exists(path))
          {
}
----------
this above code works only in the machine where application is hosted i,e server  whereas, if the application used in
any other machine other than hosted server , i could not able to find the path or directory or file.

can any body guide me in how to find the path / directory / file in remote machine using c# in ASP.net.

My aim is , if the user give the path of a folder as input , i must fetch all files & directories inside the path to my folder in web server where the application is hosted .
Riley K replied to anbu n on 29-Sep-11 01:56 AM
Here is one way to check if a file exists on an FTP Server:

public bool ftpFileExist(string fileName)
{
   WebClient wc = new WebClient();
   try
   {
    wc.Credentials = new NetworkCredential("FTP_USERNAME", "FTP_PASSWORD");
    byte[] fData = wc.DownloadData(fileName);
    if (fData.Length > -1)
    {
     return true;
    }
    else
    {
     return false;
    }
   }
   catch (Exception)
   {
    // Debug here?
   }
   return false;
}


And to call that you would do something like:


bool exists = ftpFileExist("ftp://www.yourdomain.co.uk/foo/bar.txt");

Regards
smr replied to anbu n on 29-Sep-11 01:56 AM
hi

Get files from directory

Method Directory.GetFiles returns string array with files names (full paths).

[C#]
using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\my-house.jpg"
Reena Jain replied to anbu n on 29-Sep-11 02:15 AM
Hi,

If you are trying to access a file on Server and you need Absolute Path of that file you can use Server.MapPath method that takes a virtual path as its argument and returns the corresponding physical or absolute path. 
To get physical path of a file located on root of your website
 
string path = Server.MapPath("products.xml");

or

string path = Server.MapPath(".") + "\\products.xml";

To get physical path of a file located in a sub directory such as data of your website.
string path = Server.MapPath("data") + "\\products.xml";

Anoop S replied to anbu n on 29-Sep-11 02:48 AM
Refer this code

IPAddress
[] host;
   
IPAddress[] local;
    bool isLocal
= false;

    host
= Dns.GetHostAddresses(uri.Host);
    local
= Dns.GetHostAddresses(Dns.GetHostName());

   
foreach (IPAddress hostAddress in host)
   
{
       
if (IPAddress.IsLoopback(hostAddress))
       
{
            isLocal
= true;
           
break;
       
}
       
else
       
{
           
foreach (IPAddress localAddress in local)
           
{
               
if (hostAddress.Equals(localAddress))
               
{
                    isLocal
= true;
                   
break;
               
}
           
}

           
if (isLocal)
           
{
               
break;
           
}
       
}
   
}

http://stackoverflow.com/questions/354477/method-to-determine-if-path-string-is-local-or-remote-machine