source code bean

05 Oct, 2009

Rename a Folder using EPiServer.Web.Hosting.UnifiedDirectory

Posted by: Peter In: ASP.NET|C#|EPiServer

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:

  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;
  9.         int e = -1;
  10.         while (path.IndexOf(oldName, ++e) > -1) ;
  12.         StringBuilder sb = new StringBuilder();
  13.         sb.Append(path.Substring(0, e – 1));
  14.         sb.Append(name);
  15.         sb.Append("/");
  17.         directory.MoveTo(sb.ToString());
  18.     }
  19. }

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

No Responses to "Rename a Folder using EPiServer.Web.Hosting.UnifiedDirectory"

Comment Form



Welcome to source code bean! On this site I post stuff that I encounter in my job and spare time. The content is mostly related to .NET development, but my interest in techonology is very broad, so often you will find posts on totally different subjects!