We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

FileInfo example throws an error

AstroGuy
AstroGuy asked
on
Medium Priority
205 Views
Last Modified: 2013-11-07
textbook example doesn't work
/* from .NET Framework 2.0 c# 2005 p 72 */
 
using System;
using System.IO;
 
namespace CopyFile72
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo ourFile = new FileInfo(@"c:\boost.ini"); // Note @ attribute beginning of address
            ourFile.CopyTo("@c:\boost.bak");
        }
    }
}

Open in new window

Comment
Watch Question

Top Expert 2013

Commented:
ourFile.CopyTo("@c:\boost.bak");

because in above line you have @ inside of "..."
it should be:
ourFile.CopyTo(@"c:\boost.bak");

Author

Commented:
Even when I make this correction I still get the same error.  I have verified that the original file DOES exist on drive c:.
Top Expert 2013

Commented:
what is the exact error message ?
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
I think sami has this covered, but just to re-iterate, either of these will work:

        FileInfo ourFile = new FileInfo(@"c:\boost.ini");
        ourFile.CopyTo(@"c:\boost.bak",true);

OR

        FileInfo ourFile = new FileInfo(@"c:\boost.ini");
        if (File.Exists(@"c:\boost.bak"))
        {
            FileInfo bakFile = new FileInfo(@"c:\boost.bak");
            bakFile.Delete();
        }
        ourFile.CopyTo(@"c:\boost.bak");


Only way that doesn't work is if boost.ini doesn't exist in that location, or the user (.NET) doesn't have access permissions for it.

Author

Commented:
Thanx dday,
Points already awarded to sami.
AstroGuy

Commented:
points dont seem to be awarded, but they should go to sami :)

Author

Commented:
Thanks for noticing ddayx10
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.