Rename a Folder using EPiServer.Web.Hosting.UnifiedDirectory

While reading the EPiServer 5 SDK documentation, i found this:

Rename a Folder

There is no Rename method on the EPiServer.Web.Hosting.UnifiedDirectory class. To rename a folder you need to call the MoveTo method as follows:

  1.  
  2. protected void RenameFolder(string path, string oldName, string name)
  3. {
  4.     if (IsFolder(path))
  5.     {
  6.         UnifiedDirectory directory =
  7.         System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(path) as UnifiedDirectory;
  8.  
  9.         int e = -1;
  10.         while (path.IndexOf(oldName, ++e) > -1) ;
  11.  
  12.         StringBuilder sb = new StringBuilder();
  13.         sb.Append(path.Substring(0, e – 1));
  14.         sb.Append(name);
  15.         sb.Append("/");
  16.  
  17.         directory.MoveTo(sb.ToString());
  18.     }
  19. }
  20.  

What a convenient way of renaming a folder :) Good thing that you don’t have to do it too often.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>