Reputation: 1217
This is a Console C# Based Program
I want to Loop the Creation of a Directory Creation. I'm Using the Following Method to Duplicate a Directory (Please Ignore Using a Do While For Now... I just want the Device Numbering to be Correct and at the right Place). For now lets say we run the app each time manually. I want the Naming and Numbering of the List as Follows on each execution it should build a list as follows...
C:\\Some Location\\Devices
Within the Location...
And so forth... In other words i need some sort of loop to replace the naming of the device with the next index
This is the Method in Use...
True - Default for Copying the Sub directories as well and not just the intended directory
DirectoryCopy(path, @"C:\\Custom Location to duplicate to", true);
Method Implementation :
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
if (Directory.Exists(sourceDirName))
{
cancontinue = true;
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
//Make an Array of Directories found on The Device
DirectoryInfo[] dirs = dir.GetDirectories();
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
else
{
//Provide a Device Listing. Here is Where I am Stuck
for (int i = 1; i < 11; i++)
{
if (Directory.Exists(destDirName))
{
destDirName = destDirName + "\\Device\\ " + i.ToString();
}
else
{
break;
}
}
Directory.CreateDirectory(destDirName);
}
What follows is the Loop to Copy the Sub directories in the same manner. The Current Solution Provides the Following Output...
C:\Custom Location\Devices\Device 1\Contents
And within the Device 1 the Second Device will copy and within that the next and so on.
I want to Customise the Path it Copies to, to the Machine Name (For publishing Purposes)
//This Gets the Name Perfectly + The Added Desired Path Below
string MachineName = System.Environment.MachineName;
string DesiredPath = "\\Desktop\\Program\\";
DirectoryCopy(path, @"C:\Users\" + MachineName + DesiredPath, true);
Problem Here is that i get an Access Denied Error??? Why is that? Is there a work around?
Upvotes: 0
Views: 488
Reputation: 1217
Ok so i figured out what my problem was. Fixed it. 2 Things were wrong...
Secondly using the Directory Info I created an array to check 1st what my destination path held (What devices i already had Device 1 to infinity) then using a counter just added a new device enabling a device listing directory
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
else
{
DirectoryInfo dircheck = new DirectoryInfo(destDirName);
DirectoryInfo[] dirscheck = dircheck.GetDirectories();
//Start at Device 1
int count = 1;
Item presents each file in the Destination directory
foreach (var item in dirscheck)
{
//If the FileName (Lets say Device 1) contained the count which is 1 then increment... Do so until you reach the last index of a device.
if (item.Name.Contains(count.ToString()))
{
count++;
}
}
//Give the Destination directory the name of the last index of the count + 1
destDirName = holdoriginal + "Device " + count + "\\";
Directory.CreateDirectory(destDirName);
}
Upvotes: 0
Reputation: 368
Question 1:
The for loop shouldn't overwrite destDirName variable:
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
if (Directory.Exists(sourceDirName))
{
cancontinue = true;
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
//Make an Array of Directories found on The Device
DirectoryInfo[] dirs = dir.GetDirectories();
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
//Provide a Device Listing. Here is Where I am Stuck
for (int i = 1; i < 11; i++)
{
string tmp = destDirName + "\\Device\\ " + i.ToString();
if ( ! Directory.Exists(tmp))
{
Directory.CreateDirectory(tmp);
// !!!
// here apply your function to copy
// from sourceDirName to directory in tmp variable
break;
}
}
}
}
Question 2
I bet that you don't have rights to create directory in c:\Users\ Try to execute this as Administrator
Upvotes: 1