C# FTP 文件下载错误,"Access denied to D:\dd folder"

C# FTP File Download Error, "Access denied to D:\dd folder"

我正在编写聊天应用程序并且我有文件传输的表格。我可以将文件上传到服务器,但是当我尝试下载文件时出现错误 "Access denied to D:\dd folder"。我还为所有用户授予了文件夹的完全控制权,并且我的 windows 帐户已获得管理员授权。我还尝试 运行 Visual Studio 作为管理员。下载点击;

           {
        string inputfilepath = txtSavePath.Text;
        string ftphost = "185.86.4.200:21";
        string ftpfilepath = "/" + txtFileN.Text;

        string ftpfullpath = "ftp://" + ftphost + ftpfilepath;

        using (WebClient istek = new WebClient())
        {
            istek.Credentials = new NetworkCredential(txtUser.Text, txtPass.Text);
            byte[] fileData = istek.DownloadData(ftpfullpath);

            using (FileStream file = File.Create(inputfilepath))
            {
                file.Write(fileData, 0, fileData.Length);
                file.Close();
            }
            MessageBox.Show("İndirme Tamamlandı!");
        }

我建议您在打开与 FTP 服务器的连接之前检查该路径是否存在。 在 inputfilepath = txtSavePath.Text;

之后添加此代码段
if(!Directory.Exists(inputfilePath))
{
   MessageBox.Show("FilePath was not valid!");
   return;
}

我认为问题可能出在您试图将文件直接写入文件夹。 txtSavePath.Text 的内容是什么?

要写入文件,路径应如下所示 D:\dd\temp.txt

string inputFilePath= txtSavePath.Text;
if(!Directory.Exists(inputFilePath))
{
   MessageBox.Show("FilePath was not valid!");
   return;
}
var fileNameAndPath = Path.Combine(inputFilePath, "test.txt");

...
  using (FileStream file = File.Create(fileNameAndPath))
            {
                file.Write(fileData, 0, fileData.Length);
                file.Close();
            }

将 "test.txt" 替换为您的文件名。

我在上面的评论中看到了你的GUI。另一种解决方案可能是使用 SaveFile 对话框

    var safeFileDialog = new SaveFileDialog();
    safeFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    safeFileDialog.Title = "Save your FTP File";
    safeFileDialog.ShowDialog();

    // If the file name is not an empty string open it for saving.
    if(safeFileDialog.FileName != "")
    {
    // in your case write safeFileDialog.FileName to the TextBox and use this value for saving the ftp file
    }

用户有一个漂亮且熟悉的保存对话框,并且您有一个经过验证的文件路径。双赢局面;)