Solved

C# keep value of variable

Posted on 2010-11-27
1
397 Views
Last Modified: 2012-05-10
Are there any method to keep value of  "i" in the code below so that the name of file that change will be continue

public void CopyFiles(string sourceDir, string targetDir)

        {

            string[] sourceFiles = Directory.GetFiles(sourceDir);

            for (int i = 0; i < sourceFiles.Length; i++)

            {

                string sourceFile = sourceFiles[i];

                string targetFile = targetDir + "\\" + "1" + (i + 10).ToString().PadLeft(3, '0') + "1" + ".mp3";

                File.Copy(sourceFile, targetFile, true);

            }

        }

Open in new window

0
Comment
Question by:teera
1 Comment
 
LVL 4

Accepted Solution

by:
Maverick543 earned 500 total points
ID: 34224906
You can make use of a global variable.

Assign to 0 before starting the whole copying. Then add it to the line
string targetFile = targetDir + "\\" + "1" + (i + 10 + globalVarI).ToString().PadLeft(3, '0') + "1" + ".mp3";
               
And add the value of i to it at the end of the loop.

See the code below for a full solution

Regards
Marco
int globalI = 0;



public void CopyFiles(string sourceDir, string targetDir)

        {

            string[] sourceFiles = Directory.GetFiles(sourceDir);

            for (int i = 0; i < sourceFiles.Length; i++)

            {

                string sourceFile = sourceFiles[i];

                string targetFile = targetDir + "\\" + "1" + (i + 10 + globalI).ToString().PadLeft(3, '0') + "1" + ".mp3";

                File.Copy(sourceFile, targetFile, true);

            }

            globalI += sourceFiles.Length;

        }

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

10 Experts available now in Live!

Get 1:1 Help Now