ABOUT ME

Today
Yesterday
Total
  • 최신 파일 하나만 다운로드 해줘
    프로그램(C# etc.) 2023. 1. 12. 10:45

    Q1. 다음 코드 참조해서 최신 파일 하나만 다운로드 해줘

           public static ArrayList DownloadLast(string strPathDownload)
            {
                ArrayList aFileNames = GetDirNFileList(strPathDownload);
    
                if (null == aFileNames)
                    return null;
    
                for (int i = 0; i < aFileNames.Count; ++i)
                {
                    string strFileName = aFileNames[i].ToString();
    
                    FtpWebResponse resp = GetRequest(strPathDownload + "/" + strFileName, WebRequestMethods.Ftp.DownloadFile, true);
    
                    if (null == resp)
                    {
                        Global.LOG("Download, resp is null!");
                    }
                    else
                    {
                        Stream stream = resp.GetResponseStream();
    
                        Global.CreateDirectory(Global.strUpdataLocalPath);
    
                        Global.LOG("Download, " + Global.strUpdataLocalPath + "\\" + strFileName);
    
                        using (Stream fileStream = File.Create(Global.strUpdataLocalPath + "\\" + strFileName))
                        {
                            stream.CopyTo(fileStream);
                        }
                    }
                }
    
                return aFileNames;
            }

     

    A.

    다음과 같이 변경하여 Download Last 기능을 수정하여 마지막 파일만 다운로드할 수 있습니다:

    1. GetDirNfileList 함수에서 디렉터리를 사용할 수 있습니다.GetFiles(strPathDownload) - 디렉터리에 있는 모든 파일을 가져오고 Array를 사용하여 정렬합니다.마지막으로 수정한 날짜를 기준으로 정렬합니다.

        string[] files = Directory.GetFiles(strPathDownload);
        Array.Sort(files, new FileLastWriteTimeComparer());

    2. 그런 다음 파일을 반복하여 배열의 마지막 요소를 가져와 해당 요소만 다운로드하고 루프를 해제하고 반환할 수 있습니다.

        string strFileName = files[files.Length - 1];
        FtpWebResponse resp = GetRequest(strPathDownload + "/" + strFileName, WebRequestMethods.Ftp.DownloadFile, true);
        // rest of the code remains same

    3. 파일의 마지막 쓰기 시간을 비교하는 FileLastWriteTimeComparer 클래스도 추가되었습니다.

    public class FileLastWriteTimeComparer : IComparer
    {
        public int Compare(Object x, Object y)
        {
            FileInfo file1 = new FileInfo(x.ToString());
            FileInfo file2 = new FileInfo(y.ToString());
            return DateTime.Compare(file1.LastWriteTime, file2.LastWriteTime);
        }
    }

    4. 위의 코드 변경 사항을 사용하면 지정된 폴더의 마지막 파일만 다운로드하고 파일 이름을 반환하여 최신 파일만 다운로드할 수 있습니다.

Designed by Tistory.