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 insidefolderToMove
nor the same. - It must work on .NET 4.0 without additional libraries.
-
\$\begingroup\$ See stackoverflow.com/a/13260186/2224701 \$\endgroup\$Vojtěch Dohnal– Vojtěch Dohnal2016年06月02日 06:53:31 +00:00Commented Jun 2, 2016 at 6:53
1 Answer 1
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);
}
-
2\$\begingroup\$ Another reason to choose the
DirectoryInfo
class over thePath.GetFileName()
method is that if the source path ends with a backslash (\ ), the result of the method will be empty whereas theName
property of theDirectoryInfo
class will still give the correct name of the folder. \$\endgroup\$Abbas– Abbas2016年06月02日 07:42:20 +00:00Commented Jun 2, 2016 at 7:42 -
\$\begingroup\$ Do you really always check strings first for null and then for emptiness? Somehow strange ;-) \$\endgroup\$t3chb0t– t3chb0t2016年07月24日 00:16:13 +00:00Commented Jul 24, 2016 at 0:16
-
\$\begingroup\$ @t3chb0t I do this just to throw different exceptions. \$\endgroup\$Heslacher– Heslacher2016年07月24日 09:26:56 +00:00Commented Jul 24, 2016 at 9:26