1
\$\begingroup\$

In C# I wrote a method to move a folder into another existing folder:

public void MoveFolder(string folderToMove, string destination)
{
 String destinationFolder = Path.Combine(
 destination, Path.GetFileName(folderToMove));
 Directory.Move(folderToMove, destinationFolder);
}

Usage:

MoveFolder(@"D:\myfolder\mysubfolder", @"C:\");

It works, but I am sure there is a more readable and shorter way to do that? In particular, I feel bad using GetFileName.

  • No tricky things to worry about: The folders exist, and destination is not inside folderToMove nor the same.
  • It must work on .NET 4.0 without additional libraries.
asked Jun 2, 2016 at 6:29
\$\endgroup\$
1

1 Answer 1

5
\$\begingroup\$

This method is public but you don't do any validation about the passed in parameters which is a bad idea because your code is giving implementationdetails to a user of that method which he/she doesn't need to know. Assume folderToMove == null then PathCombine() will throw an ArgumentNullException which is the correct exception, but this is telling a user that you are using System.Path.Combine() which he/she doesn't need to know.

You should better do the validation yourself and throw the expected exception.


Instead of Path.GetFileName() you should use a DirectoryInfo object and read its Name property which you could extract to a separate method to construct the destination foldername like so

 public void MoveFolder(string folderToMove, string destination)
 {
 if (folderToMove == null)
 {
 throw new ArgumentNullException("folderToMove");
 }
 if (destination == null)
 {
 throw new ArgumentNullException("destination");
 }
 if (string.IsNullOrEmpty(folderToMove))
 {
 throw new ArgumentException("The parameter may not be empty", "folderToMove");
 }
 if (string.IsNullOrEmpty(destination))
 {
 throw new ArgumentException("The parameter may not be empty", "destination");
 }
 String destinationFolder = CreateDestinationFolderName(folderToMove, destination);
 Directory.Move(folderToMove, destinationFolder);
 }
 private string CreateDestinationFolderName(string folderToMove, string destination)
 {
 var directoryInfo = new DirectoryInfo(folderToMove);
 return Path.Combine(destination, directoryInfo.Name);
 }
answered Jun 2, 2016 at 7:01
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Another reason to choose the DirectoryInfo class over the Path.GetFileName() method is that if the source path ends with a backslash (\ ), the result of the method will be empty whereas the Name property of the DirectoryInfo class will still give the correct name of the folder. \$\endgroup\$ Commented Jun 2, 2016 at 7:42
  • \$\begingroup\$ Do you really always check strings first for null and then for emptiness? Somehow strange ;-) \$\endgroup\$ Commented Jul 24, 2016 at 0:16
  • \$\begingroup\$ @t3chb0t I do this just to throw different exceptions. \$\endgroup\$ Commented Jul 24, 2016 at 9:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.