Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

reuse instance - can't get format correct

Posted on 2006-07-20
4
Medium Priority
?
243 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

Industry Leaders: 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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
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

609 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