?
Solved

FileInfo example throws an error

Posted on 2009-07-07
8
Medium Priority
?
173 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 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