Solved

reuse instance - can't get format correct

Posted on 2006-07-20
4
232 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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