Hi all,
I have very tricky problem which you u all might have seen in your
daily life when u r creating the folder.Suppose i want to create folder
in D drive. right click and select new folder like this i want to
create n no of folders. Then the series will be
Newfolder,Newfolder(2),Newfolder(3),Newfolder(4),
Newfolder(5),Newfolder(6),Newfolder(7)...After creating if i delete say
Newfolder(3),Newfolder(4) and again if i create Newfolder index will be
Newfolder(3),then one more create index will be Newfolder(4),create one
more folder index will be Newfolder(8).....etc.
This problem i would like to be done in programatic way in c sharp
code behind page. So pls somebody can write the algorithm for this problem. From TextBox Folder Name can be entered and result
will be shown in Treeview in the form of Tree. Ex.Enter parent folder
name as Sumit hit the AddFolder Button result will be in treeview
like...
-Root
-Sumit
Now select Sumit and enter many folder name with name Newfolder...index
will automatically increment then it should look like this...
-Root
-Sumit
Newfolder
Newfolder(2)
Newfolder(3)
Newfolder(4)
And if i want to delete any particular folder,select the folder and hit
the Delete folder button.After deletation if i want to create the
samefolder with same name index shoud follow the rules which i
explained above.Hope i explained my problem.Pls somebody solve this
problem.
Your reply is highly appreciated.
Thanks,
Sumit
Loading
Vimal KandasamyPosted Apr 6, 2009, 3:00 AM
For your Problem, i got a solution.... it works well for me... try it...
[ i didn't use tree view... i check it with two buttons and a textbox.... textbox is used to get name of folder to delete]
1.Create an Array and two counters...and initialize them with zero
like below
int[] ar = new int[100];// contains index of deleted folders
int co = 0; //folder index counter
int arc = 0; // array size counter
2.add this code in the add folder area
int na = co;//na refers name index of array... co refers index counter
if (arc > 0) //check availability of deleted folder index array
{
na = ar[0];//assign first value from array
for (int i = 0; i < arc-1; i++) //remove first value from array
ar[i] = ar[i + 1];
Directory.CreateDirectory("c:\\NewFolder" + na);//create a folder with deleted folder index
arc--;//reduce array size counter
}
else
{
Directory.CreateDirectory("c:\\NewFolder" + na); //create a folder with index counter
co++;//increase index counter
}
3.add this code in delete a folder area
Directory.Delete("c:\\NewFolder" + textBox1.Text); //delete a folder with given index...textbox1.text will be an index such as 0,1,2 etc...
ar[arc++] = int.Parse(textBox1.Text); //add deleted folder index into array...
let me explain....
in this whenever you trying to create a folder it will check out the deleted folder index... if it empty then create a new folder with index counter and increase its value...
if array is not empty then create a folder with arrays first value...and remove that value from array..and decrease array size counter....
whenever you trying to delete a folder..it simply delete the folder and add its index to array and increase arrays counter...
so its start with NewFolder0,NewFolder1,NewFolder2,NewFolder3...etc..
if u delete NewFolder1 and 2...then those indexes will be added into array..now array contains 1 and 2...
if you again create a folder...its name will be NewFolder1 and after that NewFolder2 after that NewFolder4..and goes on...
try this.... it may help you....