protected bool AddExistingItem(String file, String type)
        {
            if (!File.Exists(file))
            {
                MessageBox.Show(String.Format("File {0} is not exist!", file), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }
            // 如果当前文件夹不存在,进行文件复制的时候就会出问题;
            if (!Directory.Exists(this.FolderPath))
            {
                Directory.CreateDirectory(this.FolderPath);
            }

            if (String.IsNullOrEmpty(type))
            {
                PcsFileInfo fileInfo = PcsFileInfo.GetPcsFileInfo(file);
                type = fileInfo == null ? String.Empty : fileInfo.type;
            }

            // 如果新旧文件为同一个文件,并且目标文件已经存在,则退出;
            String fileName = Path.GetFileName(file);
            String newFile  = Path.Combine(this.FolderPath, fileName);

            // 如果项目中不存在该文件名
            if (!this.FileNameCanUsed(newFile, type, false))
            {
                // 文件名冲突;
                MessageBox.Show(String.Format("FileName {0} is existing in project!", fileName), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            if (file.Equals(newFile, StringComparison.OrdinalIgnoreCase))
            {
                // 目标文件与newFile为同一个文件,则直接导入;
                return(this.AddExistingItem(FileCreateFactory.CreateFile(newFile, type)));
            }
            else if (File.Exists(newFile))
            {
                // 目标文件与的newFile不为同一个文件,单newFile已经存在;
                MessageBox.Show(String.Format("File {0} is existing in {1}!", newFile, this.FolderPath), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            // 复制文件
            if (FileName.CopyFile(file, newFile))
            {
                return(this.AddExistingItem(FileCreateFactory.CreateFile(newFile, String.Empty)));
            }

            return(false);
        }
Exemple #2
0
        public static ViFileInfo CreateFile(String file, String fileType)
        {
            if (String.IsNullOrEmpty(file))
            {
                return(null);
            }
            if (String.IsNullOrEmpty(fileType))
            {
                PcsFileInfo fileInfo = PcsFileInfo.GetPcsFileInfo(file);
                if (fileInfo == null)
                {
                    return(null);
                }

                fileType = fileInfo.type;
            }
            if (String.IsNullOrEmpty(fileType))
            {
                return(null);
            }

            try
            {
                fileType = fileType.ToUpper();
                if (!DFileTypes.ContainsKey(fileType))
                {
                    return(null);
                }

                Type       type = FileCreateFactory.DFileTypes[fileType];
                ViFileInfo info = Activator.CreateInstance(type, file) as ViFileInfo;
                if (info != null)
                {
                    info.Type = fileType;
                }

                return(info);
            }
            catch (Exception ee)
            {
                Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message);
                Trace.WriteLine("### " + ee.StackTrace);
                return(null);
            }
        }