Copying all the checkout files - c#

I am using Team foundation system and I have requirement in which I want to copy all the checkout files to a local folder along with the same folder structure using C#. How can I do that?

I don't know what you mean by the "checkout files", but if you want to copy a directory you have to:
Recursively enumerate all of the files and folders in the top-level directory.
For each item that you enumerate, either create the folder in the destination directory, or copy the source file to the destination directory hierarchy.
The following will enumerate all of the files and folders in a directory:
static void FullDirList(DirectoryInfo dir, string searchPattern)
{
Console.WriteLine("Directory {0}", dir.FullName);
// list the files
foreach (FileInfo f in dir.GetFiles(searchPattern))
{
Console.WriteLine("File {0}", f.FullName);
}
// process each directory
foreach (DirectoryInfo d in dir.GetDirectories())
{
FullDirList(d, searchPattern);
}
}
If you call that with FullDirList("C:\MyProject\", *.*), it will enumerate all of the files.
To create destination folders or copy files, change the calls to Console.WriteLine so that they do the appropriate thing. All you should have to change in the destination file or folder names is the root folder name (that is, if you're copying from C:\MyProject\ to C:\MyProjectCopy\, then the destination files are just f.FullName with the C:\MyProject\ replaced by C:\MyProjectCopy).

Related

C# Is there a faster way to speed up copying a directory and files inside?

I'm using below code to make a copy a folder available on a network. This folder has, subfolders and files with total of 455 files and 13 folders and of size 409 MB.
My method is recursively calling itself to create a copy of sub folders and files in it. Overall, this method is taking more than 10 minutes to finish the task and I'm looking to speed up the process.
So far, I've went through different posts but did not find any better solution. Is there a better way to achieve my task or any improvements to my code for a faster execution?
void CopyDirectoryAndFiles(string sourceDirectory, string destinationDirectory, bool recursive)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDirectory);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found:dir.FullName}");
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDirectory);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDirectory, file.Name);
file.CopyTo(targetFilePath);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDirectory= Path.Combine(destinationDirectory,subDir.Name);
CopyDirectoryAndFiles(subDir.FullName, newDestinationDirectory, true);
}
}
}
Thanks for your help.
I don't think there's a way to increase performance in a drastic way, but I can suggest a few things to try:
replace foreach w/ Parallel.ForEach to copy data in several streams;
you can use an external tool (e.g. xcopy), which is optimized for the task, and call this tool from your C# code. xcopy can copy folders recursively if you specify the /e flag.

How to get specific type of files from a specific directory in a given path in an array in c#?

How do I get all *.txt files from a folder path to an array where I only want to get all the *.txt files from that specificly belong to a sub-directory called TXT.
I have tried
string[] txtFiles = Directory.GetFiles(#"D:\MyFiles", "*.txt");
But the above code gets all *.txt files in the given directory instead of those that are in the sub-folder named TXT?
The folders inside MyFiles look like below:
152-10-11
30-124-12
....
Where each of the folders 152-10-11 has sub-folders like 152\10\11\TXT
You can search for a specific directory using (assuming that you only have one directory that starts with "TXT"):
string[] txtDir = Directory.GetDirectories(#"D:\", "TXT*");
You could then use that result to get the files:
string[] txtFiles = Directory.GetFiles(txtDir[0], "*.txt");

c# directory tree search

I have to do a directory search and map it to the ui as a tree structure. I will explain my problem with the following structure
Root directory has --> [dir1] [dir2] [dir3].....[nth dir] (Note there will be only directories and no files)
[dir1] has --> [dir1a] [dir1b] [dir1c]....[nth dir1] (Note there will be only directories and no files)
[dir1a] has --> [1dir] [2dir] [3dir] (Not there will be only directories and no files)
[1dir] has --> some files and directories
so what I need here is the name of the directories till the directory that has files in it
In the above case that would be dir1/dir1a/1dir
I want to scan all the directories that only has directories in it and no files. I need the full path to the directories that has files in it.
I have tried the directory.enumeratedirectories and directoryinfo in a foreach but i think i might be going on the wrong path.
Any ideas how can I scan all the directories and get path to the directory that has files in it.
public void FindDirectoriesWithFiles(List<string> paths, DirectoryInfo workingDir)
{
// if this directory has files in it, add its path to the list.
if (workingDir.GetFiles().Length > 0)
{
paths.Add(workingDir.FullName);
}
else
{
// Else, this directory has no files, so iterate through its children.
foreach (var childDir in workingDir.GetDirectories())
{
FindDirectoriesWithFiles(paths, childDir);
}
}
}
Invoke the method above as follows:
var paths = new List<string>();
FindDirectoriesWithFiles(paths, new DirectoryInfo(#"C:\"));
// 'paths' now contains the folders you're looking for.
Note that this solution stops searching subfolders of folders that contain files. For example, if C:\Dir1\Dir1a\Dir1b has files in it, but it also has subfolders in it (e.g. C:\Dir1\Dir1a\Dir1b\Dir1c), those subfolders (Dir1c in this example) would not be searched.

Deleting directories inside directories

I know that if you want to delete a directory you have to delete all of it's files first.
However if you want to delete a directory which contains empty sub-directories, do you have to delete those sub-directories first? or can you just go ahead and delete the main directory?
Directory.Delete set the recurse flag to true, should do the job, no need to empty them first.
Directory.Delete(path, true);
I have just noticed that your tag refers to IsolatedStorage, in which case you will need to enumerate all the files and folders and delete as you go.
How to: Delete Files and Directories in Isolated Storage
You can try to delete recursively:
var path = Path.GetFullPath(#"C:\Temp\DeleteMe");
Directory.Delete(path,true); // true for recursive
This should delete everything including files if you have the proper permissions.
Why check if it is empty or not when you are going to delete it anyway.
You can use the Directory.Delete(yourpath,true) method only if you are sure that there isn't any readonly file in the directory. else it will throw an exception. Instead you can use your own recursive method like this which will first mark the file as normal before deleting it.
public static void DeleteDirectory(string target_dir)
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}

Unable to Delete Folder Containing Invisible Files

Using Visual Studio 2010 C#. I'm attempting to delete a folder in C:/Windows/MyFolderA where MyFolderA is a folder placed there by my software - Not Microsoft's.
I've used this code to delete the contents of the folder and the folder itself:
foreach (FileInfo tempfi in listOfMSIInstallers)
{
//Delete all Files
DirectoryInfo localDirectoryInfo = new DirectoryInfo(targetDirectory);
FileInfo[] listOfMSIInstallers = localDirectoryInfo.GetFiles("*",SearchOption.AllDirectories);
File.SetAttributes(tempfi.FullName, File.GetAttributes(tempfi.FullName) & ~FileAttributes.ReadOnly); //Remove Read-Only
File.Delete(tempfi.FullName); //Delete File
string parentFolderPath = "C:/Windows/MyFolderA"; //Example string for StackOverflow
//Remove ReadOnly attribute and delete folder
var di = new DirectoryInfo(parentFolderPath);
di.Attributes &= ~FileAttributes.ReadOnly;
Directory.Delete(parentFolderPath);
}
If I attempt to delete the folder I get an exception
"System.IO.IOException: The directory is not empty".
Showing invisible files on my GUI I do not see any. Looking at the folder with a command prompt there appears to be 2 directories: 1 named . and the second named .. (not too familiar with command prompt dir so I don't know if they're temp names or if those are the actual directory names) both at 0 files and 0 bytes.
Debugging through looking at the FileInfo[] object, it doesn't grab the invisible files found from command prompt.
Any ideas how I can delete the files/directory?
Try
Directory.Delete(parentFolderPath, true);

Categories