Solved

FileInfo example throws an error

Posted on 2009-07-07
8
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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