首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

为啥会出现“系统找不到指定文件”

2013-04-07 
为什么会出现“系统找不到指定文件”using Systemusing System.Collections.Genericusing System.Linqusi

为什么会出现“系统找不到指定文件”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Cinch;
using System.Windows.Forms;
using System.Windows.Threading;

namespace PIC
{
    public  class VM:ViewModelBase
    {
         /// <summary>
        /// 路径
        /// </summary>
        public string FolderPath { get; private set; }
        //public IList<string> imageList{get;set;}
        public string ImagePath { get; set; }
        private int index = 0;
        //private string path = System.Environment.CurrentDirectory;
        DispatcherTimer timer = new DispatcherTimer();
        /// <summary>
        /// 图片文件名
        /// </summary>
        public IList<string> ImageFileList { get; private set; }

        public SimpleCommand ChangeFolderCommand { get; private set; }
        public SimpleCommand PreviousImageCommand { get; private set; }
        public SimpleCommand NextImageCommand { get; private set; }
        public SimpleCommand ClockWiseCommand { get; private set; }
        public SimpleCommand CounterClockWiseCommand { get; private set; }
        public SimpleCommand StartSlideShowCommand { get; private set; }
        public SimpleCommand StopSlideShowCommand { get; private set; }
        public SimpleCommand DeleteImageCommand { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="folderPath"></param>
        public VM(string folderPath)
        {
            this.ImageFileList = new List<string>();
            ChangeFolderPath(folderPath);
            
            this.ChangeFolderCommand = new SimpleCommand
            {
                ExecuteDelegate=x=>ChangeFolder()


            };
            this.PreviousImageCommand = new SimpleCommand
            {
                ExecuteDelegate = x => PreviousImage()
            };
            this.NextImageCommand = new SimpleCommand
            {
                ExecuteDelegate = x => NextImage()
            };
            this.StopSlideShowCommand = new SimpleCommand
            {
                ExecuteDelegate =x=>StopSlideShow()
            };
            this.DeleteImageCommand = new SimpleCommand
            {
                ExecuteDelegate=x=>DeleteImage()
            };
            timer.Interval =  TimeSpan.FromMilliseconds(1000);
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (index >= ImageFileList.Count)
            {
                index = 0;
            }
            ImagePath = ImageFileList[index];
            index++;
        }
        void StopSlideShow()
        {
            timer.Stop();
        }
        void ChangeFolder()
        {
            FolderBrowserDialog dialog=new FolderBrowserDialog();
            if(dialog.ShowDialog()==DialogResult.OK)
            {
                ChangeFolderPath(ImagePath);
                ImageFileList.Clear();
                //


            }
        }
        void PreviousImage()
        {
            index--;
            if (index < 0)
            {
                MessageBox.Show("已是第一张");
                return;
            }
            ImagePath=ImageFileList[index];
        }
        void NextImage()
        {
            index--;
            if (index >ImageFileList.Count-1)
            {
                MessageBox.Show("已是最后一张");
                return;
            }
            ImagePath = ImageFileList[index];
        }
        void StartSlideShow()
        {
            timer.Start();
        }
        void DeleteImage()
        {
            if (ImagePath != null)
            // if (_deletingImg != null)
            {

                if (MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo) ==
                    System.Windows.Forms.DialogResult.Yes)
                {
                    try
                    {
                        //this.pictureBox.Image.Dispose();
                        //this.pictureBox.Image = null;
                        DeleteImage(ImagePath);


                    }


                    catch (Exception ex)
                    {
                        MessageBox.Show("删除失败", ex.ToString(), MessageBoxButtons.OK);

                        //throw;
                    }
                    finally
                    {
                        //this.LoadImage();
                    }
                }
            }
            else
            {
                MessageBox.Show("选择图片");
            }
        }
        /// <summary>
        /// 验证路径合法性
        /// </summary>
        /// <param name="folderPath"></param>
        void ValidateFilePath(string folderPath)
        {
            if (Directory.Exists(folderPath) == false)
            {
                throw new Exception("错误");
                
            }
            return;
        }

        /// <summary>
        /// 更改文件路径
        /// </summary>
        /// <param name="folderPath"></param>
        public void ChangeFolderPath(string folderPath)
        {
            ValidateFilePath(folderPath);
            this.FolderPath = folderPath;
            ReadImages();
        }

        /// <summary>


        /// 读取文件资源
        /// </summary>
        void ReadImages()
        {
            //清空原有的文件路径
            //遍历新文件夹路径下的文件
            //填充图片文件名
            // ImageFiles
            string[] imagePath;
            ImageFileList.Clear();
            try
            {
                imagePath = Directory.GetFiles(FolderPath, "*.jpg");
                for (int i = 0; i < imagePath.Length; i++)
                {
                    ImageFileList.Add(imagePath[i]);
                }
            }
            catch (Exception e)
            {
                
                throw new Exception("错误", e);
            }
        }
        void DeleteImage(string imageFileName)
        {
            if (File.Exists(imageFileName) == true)
            {
                try
                {
                    File.Delete(imageFileName);
                    ImageFileList.Remove(imageFileName);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }

        }

    }
}



写了这个个ViewModel,然后我用Nunit测试,我就在测试类里写了句
VM v = new VM(System.Environment.CurrentDirectory);

用Nunit来Debug一到vm的构造就报“未能加载文件或程序集“Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。”怎么搞
[解决办法]
引用:
你是不是引用了一个framework2.0的dll?

如果是的话在config文件里面加
<startup useLegacyV2RuntimeActivationPolicy="true">
  </startup>
试试看。

热点排行