Solved

FileInfo example throws an error

Posted on 2009-07-07
8
163 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

0
Comment
Question by:AstroGuy
  • 3
  • 3
  • 2
8 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24794720
ourFile.CopyTo("@c:\boost.bak");

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

Author Comment

by:AstroGuy
ID: 24796273
Even when I make this correction I still get the same error.  I have verified that the original file DOES exist on drive c:.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24796327
what is the exact error message ?
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24796380
Also say if boost.bak already Exists in given location you may want to overrite it then you should use overload method like below:
 ourFile.CopyTo(@"c:\boost.bak",true);

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 20

Expert Comment

by:ddayx10
ID: 24811451
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.
0
 

Author Comment

by:AstroGuy
ID: 24812594
Thanx dday,
Points already awarded to sami.
AstroGuy
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24819137
points dont seem to be awarded, but they should go to sami :)
0
 

Author Comment

by:AstroGuy
ID: 24819317
Thanks for noticing ddayx10
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now