0

I have this code that downloads file from ftp server and then it creates that file in path that is set.

  string inputfilepath = @"C:\Users\me\Documents\Test";
        string ftphost = "ftp_server";
        string ftpfilepath = @"/TestDOTNET/text.TXT";

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

        using (WebClient request = new WebClient())
        {
            request.Proxy = null;
            request.Credentials = new NetworkCredential("user", "pass");
            byte[] fileData = request.DownloadData(ftpfullpath);

            File.SetAttributes(inputfilepath, FileAttributes.Normal);  

            using (FileStream file = File.Create(inputfilepath)) // this is the line where I get the exception
            {
                file.Write(fileData, 0, fileData.Length);
                file.Close();
            }
            MessageBox.Show("Download Complete");

        }

I tried to create app.manifest and set requestedExcetuion level to requireAdministrator but still got no change.

Thank you for your time

8
  • 1
    I think your "inputfilepath" is a directory which is obviously read-only. Commented Dec 10, 2013 at 13:58
  • @Abhineet you are so right! I unet in the properties of the folder read-only but when I close and reopen the properties window it is still read-only. How can I unset that? Commented Dec 10, 2013 at 14:01
  • Programmatically or manually? Commented Dec 10, 2013 at 14:03
  • I think it doesn't matter, but programmatically it would be better. Commented Dec 10, 2013 at 14:04
  • var di = new DirectoryInfo("inputfilepath "); di.Attributes &= ~FileAttributes.ReadOnly; Commented Dec 10, 2013 at 14:05

3 Answers 3

2

Are you sure the user running the app has write-access to the file system?

Sign up to request clarification or add additional context in comments.

1 Comment

I think this should be mentioned as comment but. In the folder I can create, edit, copy whatever I want. I think I got all rights to write to this folder.
2

You should check that the effective user running the app - which is usually a different user (eg. NETWORK SERVICE user) from yourself - has the appropriate rights.

  • Check on IIS application pool settings which is the user running the app
  • Assign appropriate rights to such user on your target folder

Comments

1

You should first test for the Directory Path if it exists or not, if yes, then negate the read-only attribute for that directory. If not, then create directory and then create the test.txt to write in it. Ex::

string inputdirpath = @"C:\Users\me\Documents\Test";
string inputfilepath = inputdirpath + "\text.TXT";

 // Downloading Stuff
 if(Directory.Exists(inputdirpath )) 
 {
    var di = new DirectoryInfo("inputfilepath "); 
    di.Attributes &= ~FileAttributes.ReadOnly;

    using (FileStream file = File.Create(inputfilepath)) 
            {
                file.Write(fileData, 0, fileData.Length);
                file.Close();
            }
            MessageBox.Show("Download Complete");
 }
else
{
  // Create Directory
  // Set Attributes
  // Create file
  // Write data
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.