簡體   English   中英

通過FTP下載文件

[英]File downloading via FTP

我試圖將.xml文件從ftp服務器下載到我的計算機上,但是由於System.UnauthorizedAccessException: C:\\users\\...\\mypath訪問被拒絕,我總是遇到錯誤。

我該如何清楚地解決問題? 不幸的是,我試圖將目標文件夾更改為可移動硬盤驅動器,但遇到了同樣的異常。 此外,我無法更改只讀狀態的文件夾屬性,如果我犯了錯誤,則無法獲得該屬性。 您能幫您解決問題並加以解決嗎?

控制台說FileStream localFileStream = new FileStream(localFile, FileMode.Create); 代碼部分錯誤。

using System;
using System.IO;
using System.Net;
using System.Security.Permissions;
using System.Security.Authentication;
using System.Security.AccessControl;

namespace GetXMLNodes
{
class Program
{
    static void Main(string[] args)
    {
        try
        {
            ftp ftpClient = new ftp(@"ftp://...ip", "username", "password");
            ftpClient.download(@"3636594381842_2015-04-08T12_08_27.177Z Status.xml", @"C:\Users\HIKMET\Desktop\Proje_XML_to_SCADA\DownloadedXML");
            ftpClient = null;
            Console.WriteLine("download success..!");
            Console.ReadLine();
        }

        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}



class ftp
{
    private string host = null;
    private string user = null;
    private string pass = null;
    private FtpWebRequest ftpRequest = null;
    private FtpWebResponse ftpResponse = null;
    private Stream ftpStream = null;
    private int bufferSize = 2048;

    /* Construct Object */
    public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }

    /* Download File */
    public void download(string remoteFile, string localFile)
    {
        try
        {
            /* Create an FTP Request */
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            /* Log in to the FTP Server with the User Name and Password Provided */
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            /* When in doubt, use these options */
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            /* Specify the Type of FTP Request */
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            /* Establish Return Communication with the FTP Server */
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            /* Get the FTP Server's Response Stream */
            ftpStream = ftpResponse.GetResponseStream();
            /* Open a File Stream to Write the Downloaded File */
            //FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write,localFile);
            //f.AllLocalFiles = FileIOPermissionAccess.Write;
            //f.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, localFile);
            //f.Demand();
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            /* Buffer for the Downloaded Data */
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            /* Resource Cleanup */
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

}

看來localFile是一個目錄:

@"C:\Users\HIKMET\Desktop\Proje_XML_to_SCADA\DownloadedXML"

在目錄路徑上嘗試I / O將引發UnauthorizedAccessException

download()調用中提供文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM