コード例 #1
0
        public void OpenFile(string fileName)
        {
            FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));

            Debug.Assert(fileUtilityService.IsValidFileName(fileName));

            // test, if file fileName exists
            if (!fileName.StartsWith("http://"))
            {
                // test, if an untitled file should be opened
                if (!Path.IsPathRooted(fileName))
                {
                    foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection)
                    {
                        if (content.IsUntitled && content.UntitledName == fileName)
                        {
                            content.SelectView();
                            return;
                        }
                    }
                }
                else if (!fileUtilityService.TestFileExists(fileName))
                {
                    return;
                }
            }
            //检查当前文件有没有在视图中打开过.
            foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection)
            {
                // WINDOWS DEPENDENCY : ToUpper()
                if (content.ContentName != null &&
                    content.ContentName.ToUpper() == fileName.ToUpper())
                {
                    //content.SelectView();
                    MessageBox.Show("该文件已经打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            ViewTypeService viewTypeService = (ViewTypeService)ServiceManager.Services.GetService(typeof(ViewTypeService));

            IFileService fileService = (IFileService)ServiceManager.Services.GetService(typeof(IFileService));
            IViewType    viewType    = viewTypeService.GetViewTypePerFileName(fileName);

            if (viewType != null)
            {
                LoadFileWrapper            fileWrapper = new LoadFileWrapper(viewType);
                NamedFileOperationDelegate nameFileOperationDelegate = new NamedFileOperationDelegate(fileWrapper.Invoke);
                FileOperationResult        result = fileUtilityService.ObservedLoad(nameFileOperationDelegate, fileName);
                if (result == FileOperationResult.OK)                  //如果当前文档打开成功,则把当前打开的文档添加到最近打开的列表中.
                {
                    fileService.RecentOpenMemeto.AddLastFile(fileName);
                }
            }
            else
            {
                throw new ApplicationException("Can't open " + fileName + ", no display codon found.");
            }
        }
コード例 #2
0
		public void OpenFile(string fileName)
		{
			FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));

			Debug.Assert(fileUtilityService.IsValidFileName(fileName));
				
			// test, if file fileName exists
			if (!fileName.StartsWith("http://")) {
				// test, if an untitled file should be opened
				if (!Path.IsPathRooted(fileName)) { 
					foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) {
						if (content.IsUntitled && content.UntitledName == fileName) {
							content.SelectView();
							return;
						}
					}
				} else if (!fileUtilityService.TestFileExists(fileName)) {
					return;
				}
			}
			//检查当前文件有没有在视图中打开过.
			foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) {
				// WINDOWS DEPENDENCY : ToUpper()
				if (content.ContentName != null && 
				    content.ContentName.ToUpper() == fileName.ToUpper()) {
					//content.SelectView();
					MessageBox.Show("该文件已经打开!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
					return;
				}
			}
			
			ViewTypeService viewTypeService = (ViewTypeService)ServiceManager.Services.GetService(typeof(ViewTypeService));
			
			IFileService fileService = (IFileService)ServiceManager.Services.GetService(typeof(IFileService));
			IViewType viewType = viewTypeService.GetViewTypePerFileName(fileName);
			
			if (viewType != null) {
				LoadFileWrapper fileWrapper = new LoadFileWrapper(viewType);
				NamedFileOperationDelegate nameFileOperationDelegate = new NamedFileOperationDelegate(fileWrapper.Invoke);
				FileOperationResult result = fileUtilityService.ObservedLoad(nameFileOperationDelegate,fileName);
				if (result == FileOperationResult.OK) {//如果当前文档打开成功,则把当前打开的文档添加到最近打开的列表中.
					fileService.RecentOpenMemeto.AddLastFile(fileName);
				}
			} else {
				throw new ApplicationException("Can't open " + fileName + ", no display codon found.");
			}
		}