C# - FTP传输访问ACCDE文件

时间:2013-11-18 15:29:51

标签: c# vba ftp access-vba

我编写了一个简单的C#程序,用于将ACCDE文件从FTP服务器传输到客户端的桌面。转移似乎工作正常,但是,当我打开文件并尝试使用该程序时,它给我的消息“请求类型库或向导不是VBA项目”。 当我传输ACCDB源代码时,它似乎工作正常。这是传递函数:

private void DownloadFileFTP(string fileName, string localFilePath, bool isXmlSchema)
    {
        string ftpFilePath = redacted;
        if (isXmlSchema)
        {
            ftpFilePath = ftpFilePath + fileName;
            label3.Text = "Fetching update information...";
        }
        else
        {
            ftpFilePath = ftpFilePath + Properties.Settings.Default.Customer + "/" + fileName;
            label3.Text = "Updating " + fileName;
        }
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpFilePath);
        request.Credentials = new NetworkCredential(redacted, redacted);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.EnableSsl = Properties.Settings.Default.SSL;
        request.UsePassive = Properties.Settings.Default.Passive;
        int bytesRead = 0;
        byte[] buffer = new byte[2048];
        Stream reader = request.GetResponse().GetResponseStream();
        FileStream fs = new FileStream(localFilePath, FileMode.Create);
        while(true)
        {
            bytesRead = reader.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
                break;
            fs.Write(buffer, 0, bytesRead);
        }
        fs.Close();
    }

我想,我的问题是:我在这里做错了吗? ACCDE文件不能与FileStreams一起使用吗?我仍然是.NET的新手,所以任何帮助都会非常感激。

编辑:似乎其中一个参考文献导致了这个问题。

1 个答案:

答案 0 :(得分:0)

您没有关闭流和ftpWebRequest。

试试这个,用你的脚本制作,只需复制/粘贴(修复一些错误,如果它有:)):

    private void DownloadFileFTP(string fileName, string localFilePath, bool isXmlSchema)
{
    FileStream fs = null;

    try
    {
        fileName = Regex.Replace(fileName.ToString(), @"\s.*$", "").Trim();

        string ftpFilePath = redacted;

        if (isXmlSchema)
        {
            ftpFilePath = ftpFilePath + fileName;
            label3.Text = "Fetching update information...";
        }
        else
        {
            ftpFilePath = ftpFilePath + Properties.Settings.Default.Customer + "/" + fileName;
            label3.Text = "Updating " + fileName;
        }

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpFilePath);
        request.Credentials = new NetworkCredential(redacted, redacted);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.EnableSsl = Properties.Settings.Default.SSL;
        request.UsePassive = Properties.Settings.Default.Passive;

        int bytesRead = 0;
        int bufferSize = 2048;
        byte[] buffer = new byte[bufferSize];

        Stream reader = request.GetResponseStream();

        bytesRead = reader.Read(buffer, 0, bufferSize);

        fs = new FileStream(localFilePath, FileMode.Create);

        while (bytesRead > 0)
        {
            fs.Write(buffer, 0, bytesRead);
            bytesRead = reader.Read(buffer, 0, bufferSize);
        }

        reader.Close();
        request.Close();
    }
    catch (Exception ex)
    {
        label3.Text = "Error: " + ex.ToString;
    }
    finally
    {
        if (fs != null)
        {
            fs.Close();
        }
    }
}