0

I am writing chat application and I have form for file transfer. I can upload file to server but when ı tried download file ı am getting error "Access denied to D:\dd folder". I also gave the folder full control for all users and my windows account is administrator authorized. I also tried running Visual Studio as an administrator. Download click;

           {
        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ı!");
        }
  • is it windows application or web? Does `d:\dd` folder exist? – oleksa Apr 16 '20 at 10:19
  • Windows application and I am trying to write users machine. – Berkant Demir Apr 16 '20 at 11:26
  • So where are you getting the "Access denied to" exception? What is the value of `inputfilepath`? – Martin Prikryl Apr 16 '20 at 11:29
  • I want to get the file from the server and save it to the folder on my computer. However, I get an error when accessing the "D:\dd" path I have specified and there is "D:\dd" in the inputefilepath. – Berkant Demir Apr 16 '20 at 11:35
  • You already know from the answer by @user8606929 that it must be a path to a file, not a path to a directory. So `D:\dd\filename`. + You still didn't answer, what does throw the exception. I would assume it is the `File.Create`, right? – Martin Prikryl Apr 16 '20 at 11:41
  • Yes, I am getting exception in the File.Create line. Also I tried what @user8606929 say but not worked. – Berkant Demir Apr 16 '20 at 11:50
  • Look the picture please. https://hizliresim.com/YZbkCj – Berkant Demir Apr 16 '20 at 12:02
  • So if you do `File.Create(@"D:\dd\deneme.txt")` (string literal – `inputfilepath`), what exact exception do you get? – Martin Prikryl Apr 16 '20 at 12:06
  • @MartinPrikryl thank you so much. I received an error because I tried the file control command of user8606929 . I deleted his code and it worked. I writed file name like D:\dd\filename and worked so thanks. Have a good day. – Berkant Demir Apr 16 '20 at 12:21
  • Good. Also note that your download code is rather inefficient (you unnecessarily keep whole file in memory). For a correct code, see [Upload and download a binary file to/from FTP server in C#/.NET](https://stackoverflow.com/q/44606028/850848). – Martin Prikryl Apr 16 '20 at 12:29
  • Yes I know. You made me so happy because I've been dealing with this for 2 days. :) :) – Berkant Demir Apr 16 '20 at 12:37

2 Answers2

0

I would recommend that you check if the path exists before opening the connection to the FTP server. Add this snippet after inputfilepath = txtSavePath.Text;

if(!Directory.Exists(inputfilePath))
{
   MessageBox.Show("FilePath was not valid!");
   return;
}

I think the problem might be that you are trying to write a file directly into a folder. What is the content of txtSavePath.Text ?

To write a file the path should look like this 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();
            }

Replace "test.txt" with your Filename.

user8606929
  • 226
  • 1
  • 12
  • I am writing file name to txtSavePath.Text. Example: deneme.txt. I tried what you said but still not working. I added your file exists control and I changed it to "D: \ dd \ deneme.txt", but it still didn't. – Berkant Demir Apr 16 '20 at 11:20
0

I have seen your GUI in the comment above. Another solution could be to use the SaveFile dialog

    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
    }

The user has a nice and familiar save dialog and you have a validated file path. Win win situation;)

user8606929
  • 226
  • 1
  • 12