Solved

FileInfo example throws an error

Posted on 2009-07-07
8
165 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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