我怎么能看远处的文件夹

时间:2014-12-12 10:55:30

标签: c# ftp filesystemwatcher

我可以观看文件夹localy,但我怎么能在距离上做同样的事情?

我使用此代码:

private void Do_Automatic()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = Properties.Settings.Default.Chemin; 
    /* Watch for changes in LastAccess and LastWrite times, and
       the renaming of files or directories. */
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
       | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    // Only watch text files.
    watcher.Filter = Properties.Settings.Default.Type_Fichier;

    // Add event handlers.
    //watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnChanged);          


    // Begin watching.
    watcher.EnableRaisingEvents = true;

    // Wait for the user to quit the program.
    //Console.WriteLine("Press \'q\' to quit the sample.");
    //while (Console.Read() != 'q') ;
}

// Define the event handlers. 
private  void OnChanged(object source, FileSystemEventArgs e)
{                     
    //MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
    var fi = new FileInfo(e.FullPath);
    if (Isaccesible(fi, FileMode.Open, FileAccess.Read)) { DoSaveNPrint(); }

}

bool Isaccesible(FileInfo fi, FileMode Fmode, FileAccess Facces)
{
    bool hasAcces = false;
    while (!hasAcces)
    {
        try
        {
            using (var filestrem = File.Open(fi.FullName, Fmode, Facces))
            { }
            hasAcces = true;
        }
        catch (IOException ex)
        { 
         //absorb exexpteion and try again
            Thread.Sleep(600);
        }
        catch (Exception)
        {

            throw;
        }
    }
    return true;
}

private  void DoSaveNPrint()
{
   //download the file
} 

在距离上意味着,在其他服务器上。

我的服务器 - 互联网 - 其他服务器。

现在,我通过FTP导入文件,如下所示:

 public void Put_FromFTP(string ClientID, string filename)
        {
            try
            {
                FtpWebRequest reqFTP;
                try
                {
                    Get_ListFileFTPExtracted(ClientID);
                    //filePath = <<The full path where the file is to be created.>>, 
                    //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>

                    FileStream outputStream = new FileStream(Path.Combine(filePath, filename), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

                    //Other
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpChemin + "//" + filename));



                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream ftpStream = response.GetResponseStream();
                    long cl = response.ContentLength;
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[bufferSize];

                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                        outputStream.Write(buffer, 0, readCount);
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }

                    ftpStream.Close();
                    outputStream.Close();
                    response.Close();


                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            catch (Exception excThrown)
            {
                throw new Exception("Err_Sauvgarder fichier -->" + filename + excThrown.Message, excThrown);
            }
        }

0 个答案:

没有答案