【问题标题】:C# overwrite file with empty file when trying to upload (ftp)C# 尝试上传时用空文件覆盖文件(ftp)
【发布时间】:2014-10-23 12:02:17
【问题描述】:

我正在尝试通过 ftp 上传文本文件,当我运行程序时,它会清空我的文件然后上传。 我不知道为什么...可能会覆盖或其他...这是我的代码:

这是在主类中

        /* Create Object Instance */
        ftp ftpClient = new ftp(@"ftp://sportcaffe.me", "sport***", "*****");

        /* Upload a File */
        ftpClient.upload("public_html/test.txt", @"C:\Users\Lazar\Desktop\test.txt");

这里是ftp类功能代码:

/* Upload File */
    public void upload(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.UploadFile;
            /* Establish Return Communication with the FTP Server */
            ftpStream = ftpRequest.GetRequestStream();
            /* Open a File Stream to Read the File for Upload */
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            /* Buffer for the Downloaded Data */
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            /* Resource Cleanup */
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        return;
    }

没有错误和警告...

首先我在我的文件中写一些东西并保存,然后当我运行程序时,我的文件是空的,并且那个 empy 文件已经上传了......

我使用了这个代码http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class

【问题讨论】:

    标签: c# ftp


    【解决方案1】:
    FileStream localFileStream = new FileStream(localFile, FileMode.Create);
    

    我希望我不需要把它写得太清楚,但上面的行被告知创建一个文件。引用文档:“指定操作系统应该创建一个新文件。如果文件已经存在,它将被覆盖。”

    http://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx 有一个您可以在此处使用的选项列表。 FileMode.Open 应该可以满足您的需求(它会打开文件,如果文件不存在则抛出异常)。

    【讨论】:

      【解决方案2】:

      解决了

      FileStream localFileStream = new FileStream(localFile, FileMode.Create); 将 FileMode.Create 替换为 FileMode.Open

      【讨论】:

      • 你为什么要发布答案?这正是现有答案所建议的,您应该接受那个。
      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 2011-08-27
      • 2015-02-16
      • 2023-03-23
      相关资源
      最近更新 更多