public FileViewModel(FilePathType filePathType) { _FilePathType = filePathType; IsXMLInvitation = ((_FilePathType == FilePathType.Invitation) || (_FilePathType == FilePathType.Invitation_GDC) || (_FilePathType == FilePathType.Invitation_Owners)); switch (filePathType) { case FilePathType.Source: this.DirPath = FilePathType.Source.DirPath(); BindEventToFSW(); break; case FilePathType.Output: this.DirPath = FilePathType.Output.DirPath(); BindEventToFSW(); break; case FilePathType.Reports: this.DirPath = FilePathType.Reports.DirPath(); BindEventToFSW(); break; case FilePathType.Invitation: this.DirPath = FilePathType.Invitation.DirPath(); BindEventToFSW(); break; case FilePathType.Invitation_GDC: this.DirPath = FilePathType.Invitation_GDC.DirPath(); BindEventToFSW(); break; case FilePathType.Invitation_Owners: this.DirPath = FilePathType.Invitation_Owners.DirPath(); BindEventToFSW(); break; } this.BtnRemoveFileClick = new DelegateCommand<object>(this.OnBtnRemoveFileClick); }
/// <summary> /// 创建文件夹 /// </summary> /// <param name="folderPath"></param> /// <param name="type"></param> public static void CreateFolder(string folderPath, FilePathType type = FilePathType.dataPath) { if (!IsFolderExists(GetFullFilePath(folderPath, type))) { Directory.CreateDirectory(GetFullFilePath(folderPath, type)); Refresh(); } }
private static string GetDefaultErrorMessage(FilePathType filePathType) { return(filePathType switch { FilePathType.File => "The file '{0}' already exists.", FilePathType.Directory => "The directory '{0}' already exists.", _ => "The file path '{0}' already exists." });
/// <summary> /// 删除文件夹 /// </summary> /// <param name="folderPath"></param> /// <param name="type"></param> public static void DeleteFolder(string folderPath, FilePathType type = FilePathType.dataPath) { if (IsFolderExists(GetFullFilePath(folderPath, type))) { Directory.Delete(GetFullFilePath(folderPath, type), true); Refresh(); } }
/// <summary> /// 复制文件 /// </summary> /// <param name="srcFileName"></param> /// <param name="destFileName"></param> public static void CopyFile(string srcFileName, string destFileName, FilePathType type = FilePathType.dataPath) { if (IsFileExists(srcFileName, type) && !srcFileName.Equals(destFileName)) { string folderPath = GetFullFilePath(destFileName, type); CreateFolder(folderPath.Substring(0, folderPath.LastIndexOf('/'))); File.Copy(GetFullFilePath(srcFileName, type), GetFullFilePath(destFileName, type), true); Refresh(); } }
public static string GetAbsoluteFilePath(string filePath, FilePathType filePathType = FilePathType.Relative) { switch (filePathType) { case FilePathType.Absolute: return(filePath); case FilePathType.Relative: return(BaseDirectory + filePath); } return(null); }
/// <summary> /// 写入数据到对应文件 /// </summary> /// <param name="fileName"></param> /// <param name="contents"></param> public static void Write(string fileName, string contents, FilePathType type = FilePathType.dataPath) { string folderPath = GetFullFilePath(fileName, type); CreateFolder(folderPath.Substring(0, folderPath.LastIndexOf('\\'))); using (StreamWriter sw = new StreamWriter(GetFullFilePath(fileName, type))) { sw.Write(contents); sw.Close(); } Refresh(); }
private static string GetDefaultErrorMessage(FilePathType filePathType) { if (filePathType == FilePathType.File) { return("The file '{0}' does not exist."); } if (filePathType == FilePathType.Directory) { return("The directory '{0}' does not exist."); } return("The file path '{0}' does not exist."); }
public static void CreateFile(string fileName, FilePathType type = FilePathType.dataPath) { if (!IsFileExists(fileName, type)) { string folderPath = GetFullFilePath(fileName, type); CreateFolder(folderPath.Substring(0, folderPath.LastIndexOf('/'))); #if UNITY_4 || UNITY_5 using (FileStream stream = File.Create(GetFullFilePath(fileName, type))) { stream.Close(); } #else File.Create(GetFullFilePath(fileName, type)); #endif } }
/// <summary> /// 从对应文件读取数据 /// </summary> /// <param name="fileName"></param> /// <param name="type"></param> /// <returns></returns> public static string Read(string fileName, FilePathType type = FilePathType.dataPath) { if (IsFileExists(fileName, type)) { using (StreamReader streamReader = new StreamReader(GetFullFilePath(fileName, type))) { string data = streamReader.ReadToEnd(); streamReader.Close(); return(data); } //return File.ReadAllText(GetFullFilePath(fileName, type)); } else { return(string.Empty); } }
/// <summary> /// 文件全目录 /// </summary> /// <param name="folderPath"></param> /// <param name="type"></param> /// <returns></returns> public static string GetFullFilePath(string folderPath, FilePathType type = FilePathType.dataPath) { if (type == FilePathType.dataPath) { return(Path.Combine(Application.dataPath, folderPath)); } else if (type == FilePathType.persistentDataPath) { return(Path.Combine(Application.persistentDataPath, folderPath)); } else if (type == FilePathType.streamingAssetsPath) { return(Path.Combine(mStreamingAssetsPath, folderPath)); } else { return(Path.Combine(Application.temporaryCachePath, folderPath)); } }
public static bool IsFileLocked(string filePath, FilePathType filePathType = FilePathType.Relative) { try { var file = new FileInfo(GetAbsoluteFilePath(filePath, filePathType)); using (var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None)) { stream.Close(); } } catch (IOException) { return(true); } catch (Exception ex) { ErrorLogger.GenerateErrorRecord(ex); } return(false); }
/// <summary> /// 复制文件夹 /// </summary> /// <param name="srcFolderPath"></param> /// <param name="destFolderPath"></param> /// <param name="type"></param> public static void CopyFolder(string srcFolderPath, string destFolderPath, FilePathType type = FilePathType.dataPath) { if (!IsFolderExists(srcFolderPath, type)) { DebugHelper.LogInfo(string.Format("文件夹:{0} 不存在", srcFolderPath)); return; } CreateFolder(destFolderPath, type); srcFolderPath = GetFullFilePath(srcFolderPath, type); destFolderPath = GetFullFilePath(destFolderPath, type); // 创建所有的对应目录 foreach (string dirPath in Directory.GetDirectories(srcFolderPath, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(dirPath.Replace(srcFolderPath, destFolderPath)); } // 复制原文件夹下所有内容到目标文件夹,直接覆盖 foreach (string newPath in Directory.GetFiles(srcFolderPath, "*.*", SearchOption.AllDirectories)) { File.Copy(newPath, newPath.Replace(srcFolderPath, destFolderPath), true); } Refresh(); }
/// <summary> /// Initializes an instance of <see cref="FilePathExistsAttributeBase"/>. /// </summary> /// <param name="filePathType">Acceptable file path types</param> public FilePathExistsAttributeBase(FilePathType filePathType) : base(GetDefaultErrorMessage(filePathType)) { _filePathType = filePathType; }
public static bool FileExists(string filePath, FilePathType filePathType = FilePathType.Relative) { return(File.Exists(GetAbsoluteFilePath(filePath, filePathType))); }
/// <summary> /// Returns true if the file is "Null" (doesn't exist) or "Empty" (Length == 0) /// </summary> public static bool IsNullOrEmpty(string filePath, FilePathType filePathType = FilePathType.Relative) { return(!FileExists(filePath, filePathType) || IsFileEmpty(filePath, filePathType)); }
/// <summary> /// 文件是否存在 /// </summary> /// <param name="fileName"></param> /// <param name="type"></param> /// <returns></returns> public static bool IsFileExists(string fileName, FilePathType type = FilePathType.dataPath) { return(File.Exists(GetFullFilePath(fileName, type))); }
/// <summary> /// 检测文件夹是否存在 /// </summary> /// <param name="folderPath"></param> /// <param name="type"></param> /// <returns></returns> private static bool IsFolderExists(string folderPath, FilePathType type = FilePathType.dataPath) { return(Directory.Exists(GetFullFilePath(folderPath, type))); }
/// <summary> /// Initializes an instance of <see cref="FilePathNotExistsAttributeBase"/>. /// </summary> /// <param name="filePathType">Acceptable file path types</param> internal FilePathNotExistsAttributeBase(FilePathType filePathType) : base(GetDefaultErrorMessage(filePathType)) { _filePathType = filePathType; }
public static bool IsFileEmpty(string filePath, FilePathType filePathType = FilePathType.Relative) { return(new FileInfo(GetAbsoluteFilePath(filePath, filePathType)).Length == 0); }