使用Chilkat上传文件时出现问题

时间:2013-02-14 11:25:53

标签: c# .net sftp chilkat

我正在尝试使用sftp将文件上传到服务器。我已下载并安装了Chilkat,我正在下载文件,没有任何问题。但是当我尝试将文件上传到服务器时,我没有收到任何错误声明上传文件。当我检查响应消息时,它会显示“文件上传成功1”,其中一个是 true 但是这些文件没有上传到服务器。

这是我的代码:

 public void UploadAndMoveFile()
        {
            bool success = false;
            string path = @"\\geodis\";
            string archive = @"\\Archive\";
            string[] files = Directory.GetFiles(path);
            if (files.Count() == 0)
            {
//no files
            }

            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                string fileSource = path + fileName;
                string fileDestination = archive + fileName;
                string handle;
                string ftp = @"\IN\"+fileName;
                handle = sftp.OpenFile(ftp, "writeOnly", "createTruncate");
                if (handle == null)
                {
                    Console.WriteLine(sftp.LastErrorText);
                    return;
                }
                success = sftp.UploadFile(handle, fileSource);
                if (success == true)
                {
                    AppendLogFile("Uploading File Succeeded", "Uploade File", fileName);
                    System.IO.File.Move(fileSource, fileDestination);
                    AppendLogFile("Moving File Succeeded", "Moving File", fileName);
                }
                else
                {
                   // no files
                }
            }
        }

任何人都可以帮我找出我做错了吗?

1 个答案:

答案 0 :(得分:1)

发现问题,在上传方法中我有处理变量而不是ftp变量。

这是解决方案:

public void UploadAndMoveFile()
        {
            bool success = false;
            string path = @"\\geodis\";
            string archive = @"\\Archive\";
            string[] files = Directory.GetFiles(path);
            if (files.Count() == 0)
            {
//no files
            }

            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                string fileSource = path + fileName;
                string fileDestination = archive + fileName;
                string handle;
                string ftp = @"\IN\"+fileName;
                handle = sftp.OpenFile(ftp, "writeOnly", "createTruncate");
                if (handle == null)
                {
                    Console.WriteLine(sftp.LastErrorText);
                    return;
                }
                success = sftp.UploadFile(ftp, fileSource);
                if (success == true)
                {
                    AppendLogFile("Uploading File Succeeded", "Uploade File", fileName);
                    System.IO.File.Move(fileSource, fileDestination);
                    AppendLogFile("Moving File Succeeded", "Moving File", fileName);
                }
                else
                {
                   // no files
                }
            }
        }