locked
Error : "Access to the path is denied" while copying file one folder to another RRS feed

  • Question

  • Hi,

    I want to copy files one folder to another but I am getting error "Access to the path is denied".

    I am using below code :

    string FromPath = textBox1.Text;
                string ToPath = textBox2.Text;            
                foreach (string file in Directory.GetFiles(FromPath))
                {
                    if (!string.IsNullOrEmpty(file))
                    { 
                        string fileName = Path.GetFileName(file);
                        File.Copy(FromPath, Path.Combine(ToPath, fileName), true); // error
                     
                    }
                }

    Thursday, July 25, 2013 8:10 AM

Answers


  • public void WriteFiles()

    {

       string FromPath = textBox1.Text;

       string ToPath = textBox2.Text;

     

       using (StreamReader streamReader = new StreamReader(FromPath))

       {

            if (streamReader != null)

            {

               using (StreamWriter sw = new StreamWriter(ToPath))

               {

                    if (streamReader.Peek() > -1)

                    {

                          sw.WriteLine(streamReader.ReadToEnd());

                    }

                }

             }

        }

    }

    Small Improvements: You can put more then one new statement/varriable into a using Braket. The check for != null is pointless: Either the new returns an instance or an exception (at wich point the if is never reached). It never returnts null. Hence:

    public void WriteFiles()
    {
       string FromPath = textBox1.Text;
       string ToPath = textBox2.Text;
    
       using (StreamReader input = new StreamReader(FromPath), 
         StreamWriter output = new StreamWriter(ToPath))
       {
          if (input .Peek() > -1)
          {
            output.WriteLine(input.ReadToEnd());
          }
        }
    }


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Marked as answer by Caillen Wednesday, July 31, 2013 11:41 AM
    Thursday, July 25, 2013 1:37 PM
  • Hi Nitin,

    The copy method in the file class needs sufficient access to the directories and  

    hence when the copy operation is performed then system will generate an error as “Access to the path is denied” if the user account does not have sufficient access such as (Administrator and etc…)<//span>

    Please try the below way where we will read the source file to the end and write the same into the destination file using StreamReader and StreamWriter.

    public void WriteFiles()

    {

       string FromPath = textBox1.Text;

       string ToPath = textBox2.Text;

       using (StreamReader streamReader = new StreamReader(FromPath))

       {

            if (streamReader != null)

            {

               using (StreamWriter sw = new StreamWriter(ToPath))

               {

                    if (streamReader.Peek() > -1)

                    {

                          sw.WriteLine(streamReader.ReadToEnd());

                    }

                }

             }

        }

    }


    --------------------------------------------------------------------

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, July 25, 2013 1:26 PM
  • Yes you can transfer over network shares what you could transfer on the smae machine.

    There is naturally a higher Chance to run into exceptions (in addition to both Filesystems, there now might also be problems with the Network).

    Also the speed will be slower (Network is always slower then Disk).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Marked as answer by Caillen Wednesday, July 31, 2013 11:41 AM
    Wednesday, July 31, 2013 6:52 AM

All replies

  • Make sure that you have proper authentication to move files on system.

    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Thursday, July 25, 2013 8:39 AM
  • Hi Nitin,

    The copy method in the file class needs sufficient access to the directories and  

    hence when the copy operation is performed then system will generate an error as “Access to the path is denied” if the user account does not have sufficient access such as (Administrator and etc…)<//span>

    Please try the below way where we will read the source file to the end and write the same into the destination file using StreamReader and StreamWriter.

    public void WriteFiles()

    {

       string FromPath = textBox1.Text;

       string ToPath = textBox2.Text;

       using (StreamReader streamReader = new StreamReader(FromPath))

       {

            if (streamReader != null)

            {

               using (StreamWriter sw = new StreamWriter(ToPath))

               {

                    if (streamReader.Peek() > -1)

                    {

                          sw.WriteLine(streamReader.ReadToEnd());

                    }

                }

             }

        }

    }


    --------------------------------------------------------------------

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, July 25, 2013 1:26 PM

  • public void WriteFiles()

    {

       string FromPath = textBox1.Text;

       string ToPath = textBox2.Text;

     

       using (StreamReader streamReader = new StreamReader(FromPath))

       {

            if (streamReader != null)

            {

               using (StreamWriter sw = new StreamWriter(ToPath))

               {

                    if (streamReader.Peek() > -1)

                    {

                          sw.WriteLine(streamReader.ReadToEnd());

                    }

                }

             }

        }

    }

    Small Improvements: You can put more then one new statement/varriable into a using Braket. The check for != null is pointless: Either the new returns an instance or an exception (at wich point the if is never reached). It never returnts null. Hence:

    public void WriteFiles()
    {
       string FromPath = textBox1.Text;
       string ToPath = textBox2.Text;
    
       using (StreamReader input = new StreamReader(FromPath), 
         StreamWriter output = new StreamWriter(ToPath))
       {
          if (input .Peek() > -1)
          {
            output.WriteLine(input.ReadToEnd());
          }
        }
    }


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Marked as answer by Caillen Wednesday, July 31, 2013 11:41 AM
    Thursday, July 25, 2013 1:37 PM
  • Thanks for the reply.

    Will I be able to copy all (type of) files from one folder (one machine) to another folder (another machine) via using StreamReader and StreamWriter?

    Monday, July 29, 2013 5:53 AM
  • Hi Nitin,

    Yes , you can use the same logic of retriving the all the files from the directory by using Directory.GetFiles(FromPath)) and then use streStreamReader to read the file and StreamWriter to write that file into the new location.

    Wednesday, July 31, 2013 4:15 AM
  • Yes you can transfer over network shares what you could transfer on the smae machine.

    There is naturally a higher Chance to run into exceptions (in addition to both Filesystems, there now might also be problems with the Network).

    Also the speed will be slower (Network is always slower then Disk).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Marked as answer by Caillen Wednesday, July 31, 2013 11:41 AM
    Wednesday, July 31, 2013 6:52 AM
  • Thanks for the reply.

    Above mentioned code is helping me to transfer the files but I want to transfer folders with files from win7 machine/XP machine to window server 2008 machine.

    Example : One folder "A" is contaiing mulitile folders (having files) and files. We want to transfer folder "A" with conainiing all folders and files from machine to another machine via C#.NET. Is it possible ?

    Thursday, August 1, 2013 2:59 PM