Solved

reuse instance - can't get format correct

Posted on 2006-07-20
4
228 Views
Last Modified: 2010-04-16
This is likely a silly question but I can't seem to figure out the format....

I am creating a FileInfo instance that I want to reuse in a loop...changing the file that I'm looking for information on.  I did not want to create a new instance for every file.  This is what I have so far.....I don't have my loop created but don't worry about that.... I'll get to that... the problem is the line:  file = FileInfo(strBakFileName);   Basically, I'm trying to reuse instance of file and set it to a new file name and then do a file.Exists on the new file name.  I've tried file = (FileInfo)strBakFileName and that does work....

                FileInfo file = new FileInfo(strCommissionsPath);
                               
                if (file.Exists)
                {
                    string strBakFileName = strCommissionsPath;

                    strBakFileName = strBakFileName.Insert(strBakFileName.IndexOf(".csv"), intCounter.ToString());
                    strBakFileName = strBakFileName.Replace(".csv", ".bak");
                    file = FileInfo(strBakFileName);

                    if (file.Exists)
                    {
                        intCounter = intCounter + 1;
                    }

Thanks so much for your help!
                }
0
Comment
Question by:babybird
  • 2
4 Comments
 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 100 total points
ID: 17148145
Sorry, but you can't reuse the same FileInfo instance for different files.
0
 
LVL 5

Accepted Solution

by:
paulb1989 earned 400 total points
ID: 17148148
why not scrap the use of FileInfo to check if the file exists completely and use File.Exists?
                               
                if (File.Exists(strCommissionsPath))
                {
                    string strBakFileName = strCommissionsPath;

                    strBakFileName = strBakFileName.Insert(strBakFileName.IndexOf(".csv"), intCounter.ToString());
                    strBakFileName = strBakFileName.Replace(".csv", ".bak");

                    if (File.Exists(strBakFileName)
                    {
                        intCounter = intCounter + 1;
                    }
                }

Make sure that you have using System.IO in your code.
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17148150
The only way to specify which file the FileInfo is associated with is via the constructor, so you can't "reload" the object after it is created.
0
 

Author Comment

by:babybird
ID: 17148465
I didn't know that was an option...

thanks to all for your help!!!
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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

13 Experts available now in Live!

Get 1:1 Help Now