Read file from FTP to memory in C#

前端 未结 9 1505
长发绾君心
长发绾君心 2020-12-03 05:15

I want to read a file from a FTP server without downloading it to a local file. I wrote a function but it does not work:

private string GetServerVersion()
{
         


        
9条回答
  •  鱼传尺愫
    2020-12-03 06:03

    The code you have above is very simillar to another Stackoverlow example I found and used myself 2 days ago. Provided you set the URI, User and Password correctly, it will download the file and set the contents to FileString. I'm not sure what you mean by wanting to read the file without downloading it. This is not really an option.

    If you want to look at file attributes (I see you mention "version"), you could use the code below to get all the file Name, Data, and Size from the FTP server without downloading the file.

    Call GetFileInfo and pass in the file name (make sure you follow through the code to set the full FTP path, User and Password). This will give you back a FTPFileInfo object with a Name, Date and Size.

      public static FTPFileInfo GetFileInfo(string fileName)
            {
                var dirInfo = WordstockExport.GetFTPDirectoryDetails();
                var list = FTPFileInfo.Load(dirInfo);
                try
                {
                    FTPFileInfo ftpFile = list.SingleOrDefault(f => f.FileName == fileName);
                    return ftpFile;
                }
                catch { }
                return null;
            }
         class FTPFileInfo
            {
                private DateTime _FileDate;
                private long _FileSize;
                private string _FileName;
    
                public DateTime FileDate
                {
                    get { return _FileDate; }
                    set { _FileDate = value; }
                }
                public long FileSize
                {
                    get { return _FileSize; }
                    set { _FileSize = value; }
                }
                public string FileName
                {
                    get { return _FileName; }
                    set { _FileName = value; }
                }
    
                public FTPFileInfo() { }
                public static FTPFileInfo LoadFromLine(string line)
                {
                    FTPFileInfo file = new FTPFileInfo();
    
                    string[] ftpFileInfo = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    file._FileDate = DateTime.Parse(ftpFileInfo[0] + " " + ftpFileInfo[1]);
                    file._FileSize = long.Parse(ftpFileInfo[2]);
                    file._FileName = ftpFileInfo[3];
    
                    return file;
    
                }
                public static List Load(string listDirectoryDetails)
                {
                    List files = new List();
    
                    string[] lines = listDirectoryDetails.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    foreach(var line in lines)
                        files.Add(LoadFromLine(line));
    
                    return files;
                }
    
            }
           private static string GetFTPDirectoryDetails()
                {
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(App.Export_FTPPath);
                    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                    request.Credentials = new NetworkCredential(App.FTP_User, App.FTP_Password);
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream);
                    string fileLines = reader.ReadToEnd();
                    reader.Close();
                    response.Close();
    
                    return fileLines;
                }
    

提交回复
热议问题