안녕하세요.
C#책 잘 읽었습니다.
1. 목적:윈도우OS에서C#으로 리눅스FTP로 업로드 목적
2. 목적지Linux FTP폴더경로: /home/LinuxFolder/
3. 질문(모르는부분): 아래 코드에서 
string uploadUrl = "ftp://32.785.25.32/home/LinuxFolder/LinuxFile.txt";
부분에서 리눅스 /home/LinuxFolder 에 업로드 해야 되는데...
string uploadUrl = "ftp://32.785.25.32/home/LinuxFolder/TotalAmount.txt" 해야 되는지요?
4. 질문(모르는부분) 아래 코드에서 
   // 리눅스 디렉토리에 ftp://32.785.25.32/home/LinuxFolder 폴더가 없으면 
   string DestinationFolderPath = "ftp://32.785.25.32/home/LinuxFolder";
   DirectoryInfo di = new DirectoryInfo(DestinationFolderPath);
   if (di.Exists == false)
   {
        di.Create(); // 리눅스 디렉토리에 폴더를 생성
   }
하면 되는지요
public void FtpUpload()
{
   // 리눅스 ftp://32.785.25.32/home/LinuxFolder 폴더가 없으면 
   string DestinationFolderPath = "ftp://32.785.25.32/home/LinuxFolder";
   DirectoryInfo di = new DirectoryInfo(DestinationFolderPath);
   if (di.Exists == false)
   {
        di.Create(); // 리눅스 디렉토리에 폴더를 생성
   }
    // 리눅스FTP에 home/LinuxFolder 디렉토리
    string uploadUrl = "ftp://32.785.25.32/home/LinuxFolder/LinuxFile.txt";
    FtpWebRequest request = (FtpWebRequest) WebRequest.Create(uploadUrl);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    
    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential("username", "password");
    request.Proxy = null;
    request.KeepAlive = true;
    request.UseBinary = true;
    request.Method = WebRequestMethods.Ftp.UploadFile;
    // Copy the contents of the file to the request stream.
    StreamReader sourceStream = new StreamReader(@"D:\Sapmle applications\TotalAmount.txt");
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    
    request.ContentLength = fileContents.Length;
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();
    
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
}
        
        
                    
                    
                    
                    
                    
    
                    
                    
                    
                    
                    
                
                    [최초 등록일: ]
                    [최종 수정일: 8/1/2024]