WEB开发网
开发学院软件教学办公软件Word 添加/删除博客信息——Word 2007高级应用(八) 阅读

添加/删除博客信息——Word 2007高级应用(八)

 2008-10-14 20:17:55 来源:WEB开发网   
核心提示: 把工作目录里对应的文件夹名字改为新的名字,现有博客的删除也包含如下两项操作: 在Blogs.xml里删除该博客的对应信息;在工作目录里删除该博客相关的文件夹及其内容,添加/删除博客信息——Word 2007高级应用(八)(2),这些操作将会由BlogsManager类负责: // Code

把工作目录里对应的文件夹名字改为新的名字。

现有博客的删除也包含如下两项操作:

在Blogs.xml里删除该博客的对应信息;

在工作目录里删除该博客相关的文件夹及其内容。

这些操作将会由BlogsManager类负责:

// Code #04
public class BlogsManager
{
  private BlogsManager()
  {
  }
  private static BlogsManager m_Instance = new BlogsManager();
  public static BlogsManager Instance
  {
    get { return m_Instance; }
  }
  public void Initialize()
  {
    if (String.IsNullOrEmpty(WorkingDirectory))
    {
      Properties.Settings.Default.WorkingDirectory = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        "My Blogs"
      );
    }
    if (!Directory.Exists(WorkingDirectory))
    {
      Directory.CreateDirectory(WorkingDirectory);
    }
    string metadataPath = Path.Combine(WorkingDirectory, "Blogs.xml");
    if (!File.Exists(metadataPath))
    {
      XElement blogsMetadata = new XElement(
        "blogs", new XAttribute("defaultBlog", String.Empty)
      );
      blogsMetadata.Save(metadataPath);
    }
  }
  public string WorkingDirectory
  {
    get
    {
      return Properties.Settings.Default.WorkingDirectory;
    }
    set
    {
      if (Properties.Settings.Default.WorkingDirectory != value)
      {
        MoveWorkingDirectoryTo(value);
        Properties.Settings.Default.WorkingDirectory = value;
      }
    }
  }
  public Blog[] Blogs
  {
    get
    {
      XElement blogsMetadata = XElement.Load(
        Path.Combine(WorkingDirectory, "Blogs.xml")
      );
      var blogs = from blog in blogsMetadata.Elements()
            select new Blog
            {
              Name = blog.Attribute("name").Value,
              Url = blog.Attribute("url").Value
            };
      return blogs.ToArray();
    }
  }
  public void Add(Blog blog)
  {
    // Add blog info to Blogs.xml
    string metadataPath = Path.Combine(WorkingDirectory, "Blogs.xml");
    XElement blogsMetadata = XElement.Load(metadataPath);
    blogsMetadata.Add(
      new XElement("blog", new XAttribute("name", blog.Name), new XAttribute("url", blog.Url))
    );
    blogsMetadata.Save(metadataPath);
    // Create directory structure for blog
    string blogPath = Path.Combine(WorkingDirectory, blog.Name);
    string postsPath = Path.Combine(blogPath, "Posts");
    string draftsPath = Path.Combine(blogPath, "Drafts");
    Directory.CreateDirectory(blogPath);
    Directory.CreateDirectory(postsPath);
    Directory.CreateDirectory(draftsPath);
  }
  public void Rename(string oldBlogName, string newBlogName)
  {
    // Modify blog info in Blogs.xml
    string metadataPath = Path.Combine(WorkingDirectory, "Blogs.xml");
    XElement blogsMetadata = XElement.Load(metadataPath);
    XElement blogMetadata = blogsMetadata.Elements().Single(
      blog => blog.Attribute("name").Value == oldBlogName
    );
    blogMetadata.Attribute("name").Value = newBlogName;
    blogsMetadata.Save(metadataPath);
    // Rename blog directory
    string oldBlogPath = Path.Combine(WorkingDirectory, oldBlogName);
    string newBlogPath = Path.Combine(WorkingDirectory, newBlogName);
    Directory.Move(oldBlogName, newBlogName);
  }
  public void Remove(string blogName)
  {
    // Remove blog info from Blogs.xml
    string metadataPath = Path.Combine(WorkingDirectory, "Blogs.xml");
    XElement blogsMetadata = XElement.Load(metadataPath);
    XElement blogMetadata = blogsMetadata.Elements().Single(
      blog => blog.Attribute("name").Value == blogName
    );
    blogMetadata.Remove();
    blogsMetadata.Save(metadataPath);
    // Remove blog directory
    string blogPath = Path.Combine(WorkingDirectory, blogName);
    Directory.Delete(blogPath, true);
  }
  private void MoveWorkingDirectoryTo(string newPath)
  {
    string oldPath = WorkingDirectory;
    foreach (var directory in Directory.GetDirectories(oldPath))
    {
      Directory.Move(
        directory,
        Path.Combine(newPath, Path.GetFileName(directory))
      );
    }
    File.Move(
      Path.Combine(oldPath, "Blogs.xml"),
      Path.Combine(newPath, "Blogs.xml")
    );
  }
}

上一页  1 2 3 4 5  下一页

Tags:添加 删除 博客

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接