Solved

FileInfo example throws an error

Posted on 2009-07-07
8
162 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

12 Experts available now in Live!

Get 1:1 Help Now