상속: MonoBehaviour
예제 #1
0
        /// <summary>Creates all directories and subdirectories in the path for this FolderObject unless they already exist.
        /// if Folder.Option
        /// </summary>
        /// <returns>An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists.</returns>
        /// <exception cref="T:System.IO.IOException">The directory specified is a file.-or-The network name is not known.</exception>
        /// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission. </exception>
        /// <exception cref="T:System.ArgumentException"> </exception>
        /// <exception cref="T:System.ArgumentNullException"> </exception>
        /// <exception cref="T:System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters. </exception>
        /// <exception cref="T:System.IO.DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive). </exception>
        /// <exception cref="T:System.NotSupportedException"></exception>
        public void Create(FolderOption folderOption)
        {
            var exist = Exist;

            switch (folderOption)
            {
            case FolderOption.OverwriteFilesIfExist:
                DirectoryInfo = Directory.CreateDirectory(FullName);
                break;

            case FolderOption.DoNothingIfExist:
                DirectoryInfo = Directory.CreateDirectory(FullName);
                break;

            case FolderOption.DoNothingIfFileExist:
                DirectoryInfo = Directory.CreateDirectory(FullName);
                break;

            case FolderOption.DeleteThenWrite:
                if (exist)
                {
                    DirectoryInfo.Delete(true);
                    DirectoryInfo = Directory.CreateDirectory(FullName);
                }
                else
                {
                    DirectoryInfo = Directory.CreateDirectory(FullName);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(folderOption), folderOption, null);
            }
        }
예제 #2
0
        /// <summary>
        /// Copies to.
        /// </summary>
        /// <param name="location">The location.</param>
        /// <param name="folderOption"></param>
        /// <exception cref="Exception">
        /// </exception>
        public void CopyTo(string location, FolderOption folderOption)
        {
            var destination = new DirectoryInfo(Path.Combine(location, Name));

            if (folderOption == FolderOption.DoNothingIfExist && destination.Exists)
            {
                return;
            }
            CopyLogic(DirectoryInfo, destination, folderOption);
        }
예제 #3
0
        /// <summary>
        /// Copies to.
        /// </summary>
        /// <param name="location">The location.</param>
        /// <param name="folderOption"></param>
        /// <exception cref="Exception">
        /// </exception>
        public void CopyContentsTo(string location, FolderOption folderOption)
        {
            var target = new DirectoryInfo(location);

            if (folderOption == FolderOption.DoNothingIfExist && target.Exists)
            {
                return;
            }
            CopyLogic(DirectoryInfo, target, folderOption);
        }
        public void Test_Create_DoesCreateFolder([Values] FolderOption folderOption)
        {
            // Arrange

            var originalFolder = NewTestSubFolder;

            originalFolder.Create(folderOption);

            Assert.True(originalFolder.Exist);
        }
예제 #5
0
        private void CopyLogic(DirectoryInfo source, DirectoryInfo target, FolderOption folderOption)
        {
            //if (string.Equals(source.FullName, target.FullName, StringComparison.CurrentCultureIgnoreCase))
            //{
            //	return;
            //}

            // Check if the target directory exists, if not, create it.
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }

            // Copy each file into it's new directory.
            foreach (var fi in source.GetFiles())
            {
                //Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
                var destinationFile = new FileObject(Path.Combine(target.ToString(), fi.Name));
                var sourceFile      = new FileObject(fi);
                switch (folderOption)
                {
                case FolderOption.OverwriteFilesIfExist:
                    sourceFile.CopyTo(destinationFile.FullName, FileOption.Overwrite);
                    break;

                case FolderOption.DoNothingIfExist:                         // WE SHOULD NEVER GET THIS FAR TO BEGIN WITH
                    sourceFile.CopyTo(destinationFile.FullName, FileOption.Overwrite);
                    break;

                case FolderOption.DeleteThenWrite:
                    if (destinationFile.Exist)
                    {
                        destinationFile.DeleteFile(false);
                    }
                    sourceFile.CopyTo(destinationFile.FullName, FileOption.Overwrite);
                    break;

                case FolderOption.DoNothingIfFileExist:
                    sourceFile.CopyTo(destinationFile.FullName, FileOption.DoNothingIfExist);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(folderOption), folderOption, null);
                }
            }

            // Copy each subDirectory using recursion.
            foreach (var diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                CopyLogic(diSourceSubDir, nextTargetSubDir, folderOption);
            }
        }
예제 #6
0
        /// <summary>
        /// 获取对应模块的文件夹
        /// </summary>
        /// <param name="folderOption">根文件夹类型</param>
        /// <param name="subOption">子文件夹类型</param>
        /// <returns>返回对应模块的文件夹</returns>
        public static async Task <StorageFolder> getFolder(FolderOption folderOption, SubOption subOption)
        {
            StorageFolder folder = null;

            switch (folderOption)
            {
            case FolderOption.Motion:
                folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(MOTION, CreationCollisionOption.OpenIfExists);

                break;

            case FolderOption.Im:
                folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(IM, CreationCollisionOption.OpenIfExists);

                break;

            default:
                return(null);
            }
            StorageFolder sub = null;

            switch (subOption)
            {
            case SubOption.HeadImg:
                sub = await folder.CreateFolderAsync(HEADIMG, CreationCollisionOption.OpenIfExists);

                break;

            case SubOption.Emoji:
                sub = await folder.CreateFolderAsync(EMOJI, CreationCollisionOption.OpenIfExists);

                break;

            case SubOption.Bubble:
                sub = await folder.CreateFolderAsync(BUBBLE, CreationCollisionOption.OpenIfExists);

                break;

            case SubOption.db:
                sub = await folder.CreateFolderAsync(DB, CreationCollisionOption.OpenIfExists);

                break;

            default:
                return(null);
            }
            return(sub);
        }
예제 #7
0
        private void MoveLogic(string location, FolderOption folderOption, bool includeFolder)
        {
            if (!Exist)
            {
                return;
            }

            switch (folderOption)
            {
            case FolderOption.OverwriteFilesIfExist:
                CopyLogic(DirectoryInfo, new DirectoryInfo(location), folderOption);
                Delete(true, false);
                break;

            case FolderOption.DoNothingIfExist:                     // WE SHOULD NEVER GET THIS FAR TO BEGIN WITH
                if (Directory.Exists(location) != true)
                {
                    Directory.CreateDirectory(location);
                    Directory.Delete(location);
                    Directory.Move(FullName, location);
                }
                break;

            case FolderOption.DeleteThenWrite:
                new FolderObject(location).Delete(true, true);
                Directory.CreateDirectory(location);
                Directory.Delete(location);
                Directory.Move(FullName, location);
                break;

            case FolderOption.DoNothingIfFileExist:
                if (includeFolder)
                {
                    CopyLogic(DirectoryInfo, new DirectoryInfo(location), folderOption);
                }
                else
                {
                    CopyContentsTo(location, folderOption);
                }
                Delete(true, false);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(folderOption), folderOption, null);
            }
        }
예제 #8
0
        public static IEnumerable <FileInfo> EachFile(
            IEnumerable <string> paths,
            FolderOption folder = FolderOption.Ignore)
        {
            foreach (var path in paths)
            {
                // フォルダが存在する場合
                if (Directory.Exists(path))
                {
                    switch (folder)
                    {
                    case FolderOption.SearchFilesShallow:
                        foreach (var file in Directory.EnumerateFiles(
                                     path, "*.*",
                                     SearchOption.TopDirectoryOnly))
                        {
                            yield return(new FileInfo(file));
                        }
                        break;

                    case FolderOption.SearchFilesDeep:
                        foreach (var file in Directory.EnumerateFiles(
                                     path, "*.*",
                                     SearchOption.AllDirectories))
                        {
                            yield return(new FileInfo(file));
                        }
                        break;

                    case FolderOption.Ignore:
                    default:
                        continue;
                    }
                }

                // ファイルが存在する場合
                if (File.Exists(path))
                {
                    yield return(new FileInfo(path));
                }
            }
        }
        public void Test_MoveContent_WhenDestinationDoesntExist_DoesntIncludeFolderJustItsContent([Values] FolderOption folderOption)
        {
            var originalFolder    = NewTestSubFolder;
            var destinationFolder = NewTestSubFolder;

            originalFolder.AddFile("file1.txt", string.Empty, FileOption.Overwrite);
            originalFolder.AddFile("file2.txt", string.Empty, FileOption.Overwrite);
            originalFolder.AddFile("file3.txt", string.Empty, FileOption.Overwrite);

            originalFolder.CopyContentsTo(destinationFolder.FullName, folderOption);


            destinationFolder.RefreshObject();
            originalFolder.RefreshObject();

            var destinationFoldersRecursive = destinationFolder.DirectoryInfo.GetDirectories("*", SearchOption.AllDirectories);
            var destinationFilesRecursive   = destinationFolder.DirectoryInfo.GetFiles("*", SearchOption.AllDirectories);

            var ogFoldersRecursive = destinationFolder.DirectoryInfo.GetDirectories("*", SearchOption.AllDirectories);
            var ogFilesRecursive   = destinationFolder.DirectoryInfo.GetFiles("*", SearchOption.AllDirectories);


            Assert.IsEmpty(destinationFoldersRecursive);
            Assert.IsNull(destinationFoldersRecursive.FirstOrDefault(f => f.Name.Equals(originalFolder.Name)));
            Assert.AreEqual(3, destinationFilesRecursive.Count());

            Assert.IsEmpty(ogFoldersRecursive);
            Assert.IsNull(ogFoldersRecursive.FirstOrDefault(f => f.Name.Equals(originalFolder.Name)));
            Assert.AreEqual(3, ogFilesRecursive.Count());


            // Assert.AreEqual(3, destinationFolder.Files.Count);
        }
        public void Test_MoveTo_WhenDestinationDoesntExist_IncludeEntireFolderNotJustContentInFolder([Values] FolderOption folderOption)
        {
            var originalFolder    = NewTestSubFolder;
            var destinationFolder = NewTestSubFolder;

            originalFolder.AddFile("file1.txt", string.Empty, FileOption.Overwrite);
            originalFolder.AddFile("file2.txt", string.Empty, FileOption.Overwrite);
            originalFolder.AddFile("file3.txt", string.Empty, FileOption.Overwrite);

            originalFolder.MoveTo(destinationFolder.FullName, folderOption);


            destinationFolder.RefreshObject();
            originalFolder.RefreshObject();

            var ogSubfolders          = originalFolder.GetDirectories("*", SearchOption.AllDirectories);
            var destinationSubFolders = destinationFolder.GetDirectories("*", SearchOption.AllDirectories);

            var ogFiles          = originalFolder.GetFiles("*", SearchOption.AllDirectories);
            var destinationFiles = destinationFolder.GetFiles("*", SearchOption.AllDirectories);


            Assert.IsEmpty(ogFiles);
            Assert.IsEmpty(ogSubfolders);

            Assert.AreEqual(3, destinationFiles.Count());
            Assert.AreEqual(1, destinationSubFolders.Count());

            Assert.IsFalse(originalFolder.Exist);
        }
예제 #11
0
        public void Test_CopyTo_WhenDestinationDoesntExist_IncludeEntireFolderNotJustContentInFolder([Values] FolderOption folderOption)
        {
            var originalFolder    = NewTestSubFolder;
            var destinationFolder = NewTestSubFolder;

            originalFolder.AddFile("file1.txt", string.Empty, FileOption.Overwrite);
            originalFolder.AddFile("file2.txt", string.Empty, FileOption.Overwrite);
            originalFolder.AddFile("file3.txt", string.Empty, FileOption.Overwrite);

            originalFolder.CopyTo(destinationFolder.FullName, folderOption);


            destinationFolder.RefreshObject();
            originalFolder.RefreshObject();

            var subFolders = destinationFolder.DirectoryInfo.GetDirectories("*", SearchOption.AllDirectories);

            Assert.IsNotEmpty(subFolders);
            Assert.IsNotNull(subFolders.FirstOrDefault(f => f.Name.Equals(originalFolder.Name)));
            Assert.AreEqual(3, destinationFolder.DirectoryInfo.GetFiles("*", SearchOption.AllDirectories).Count());


            Assert.IsEmpty(originalFolder.DirectoryInfo.GetDirectories("*", SearchOption.AllDirectories));
            Assert.AreEqual(3, originalFolder.DirectoryInfo.GetFiles("*", SearchOption.AllDirectories).Count());
        }
예제 #12
0
 /// <summary>
 /// Moves to.
 /// </summary>
 /// <param name="location">The location.</param>
 /// <param name="folderOption"></param>
 /// <exception cref="Exception">
 /// </exception>
 public void MoveContentsTo(string location, FolderOption folderOption)
 {
     MoveLogic(location, folderOption, false);
 }
예제 #13
0
 /// <summary>
 /// Moves to.
 /// </summary>
 /// <param name="location">The location.</param>
 /// <param name="folderOption"></param>
 /// <exception cref="Exception">
 /// </exception>
 public void MoveTo(string location, FolderOption folderOption)
 {
     MoveLogic(Path.Combine(location, Name), folderOption, true);
 }