?
Solved

reuse instance - can't get format correct

Posted on 2006-07-20
4
Medium Priority
?
240 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
[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
  • 2
4 Comments
 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 400 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 1600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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