Solved

C# keep value of variable

Posted on 2010-11-27
1
398 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

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

Suggested Solutions

Title # Comments Views Activity
Why, and when, to use Windows Workflow 1 31
Get String split 5 46
Definitions and default visual studio colors 5 58
Code works but it's slow 23 26
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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 demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 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

18 Experts available now in Live!

Get 1:1 Help Now