随笔 - 41  文章 - 0  评论 - 24 
  2008年3月25日
imageEditImage是一个Image控件,在后台代码中我想给它指定Source的属性。我先如下方式进行:
BitmapImage image = new BitmapImage(new Uri(strImagePath, UriKind.Absolute));
imageEditImage.Source = image;
strImagePath是图片的绝对路径。

在另一处代码中我想把strImagePath指定的图片删掉,操作如下:
if (System.IO.File.Exists(strImagePath))
 {
    System.IO.File.Delete(strImagePath);
 }
但是删除操作报错,愿意是程序在使用图片文件。

解决方法如下:
   BitmapImage image = new BitmapImage();
   m_ImageStream = new FileStream(strImagePath, FileMode.Open);
   image.BeginInit();
   image.StreamSource = m_ImageStream;
   image.EndInit();
   imageEditImage.Source = image;
其中m_ImageStream是一个全局的Stream变量,在删除操作前关闭这个变量就不会再出错了。代码改动如下:
if (m_ImageStream != null)
{
    m_ImageStream.Close();
    m_ImageStream.Dispose();
}

if (System.IO.File.Exists(strImagePath))
 {
    System.IO.File.Delete(strImagePath);
 }
posted @ 2008-03-25 18:38 pdfw 阅读(9588) 评论(2) 编辑

本技巧使用GetFolderPath方法来获取指向由指定枚举标识的系统特殊文件夹的路径。语法格式如下:

public static string GetFolderPath (SpecialFolder folder)

参数folder标识系统特殊文件夹的枚举常数。

如果指定系统的特殊文件夹存在于用户的计算机上,则返回到该文件夹的路径;否则为空字符串(" ")。如果系统未创建文件夹、已删除现有文件夹或者文件夹是不对应物理路径的虚拟目录(例如“我的电脑”),则该文件夹不会实际存在。

主要代码如下:

         MessageBox.Show("我的文档系统路径:" + Environment.GetFolderPath(Environment.SpecialFolder.Personal), "我的档",MessageBoxButtons.OK,MessageBoxIcon.Information);

posted @ 2008-03-25 11:25 pdfw 阅读(3015) 评论(0) 编辑