当前位置: 首页>>代码示例>>C#>>正文


C# Directory.GetName方法代码示例

本文整理汇总了C#中System.IO.Directory.GetName方法的典型用法代码示例。如果您正苦于以下问题:C# Directory.GetName方法的具体用法?C# Directory.GetName怎么用?C# Directory.GetName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Directory的用法示例。


在下文中一共展示了Directory.GetName方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SyncDirectory

        public async Task SyncDirectory(Directory dir, string parentId)
        {
            // Get directories of parent and find this folder's id in drive (if exists)
            IList<DriveFile> parentChildren = mDMgr.FetchChildrenDirectories(parentId);

            string dirId = "";
            foreach (DriveFile file in parentChildren)
                if (file.Title.Equals(dir.GetName()))
                    dirId = file.Id;

            // Folder does not exist so create it
            if (dirId.Equals(""))
                dirId = mDMgr.CreateDirectory(dir.GetName(), dir.GetName(), parentId).Id;

            // Get files and folders of this directory in drive
            IList<DriveFile> children = mDMgr.FetchChildren(dirId);

            // Start syncing this directory
            // Check the drive for any files and folders that do not exist locally and delete them
            List<DriveFile> foldersToDelete = new List<DriveFile>();
            foreach (DriveFile dFile in children)
            {
                bool existsLocally = false;

                foreach (string file in dir.GetFiles())
                {
                    if (dFile.Title.Equals(Path.GetFileName(file)))
                    {
                        existsLocally = true;
                        break;
                    }
                }

                foreach (Directory subdir in dir.getSubDirectories())
                {
                    if(dFile.Title.Equals(subdir.GetName()))
                    {
                        existsLocally = true;
                        break;
                    }
                }

                // Delete if it doesn't exist
                if (!existsLocally)
                {
                    string type;
                    if (DriveManager.IsDirectory(dFile))
                        type = "directory";
                    else
                        type = "file";

                    Console.WriteLine("Deleting " + type + ": " + dFile.Title);
                    mDMgr.DeleteFile(dFile.Id);
                }
            }

            IList<DriveFile> childrenFiles = children.Where(d => !(DriveManager.IsDirectory(d))).ToList();
            // Upload or update files
            foreach (string file in dir.GetFiles())
            {
                // Get simple filename
                string filename = Path.GetFileName(file);

                // Try to find it on drive
                DriveFile fileInDrive = null;
                foreach (DriveFile dFile in childrenFiles)
                {
                    if (dFile.Title.Equals(filename))
                    {
                        fileInDrive = dFile;
                        break;
                    }
                }

                // If didn't found it, upload it, else update it
                if (fileInDrive != null)
                {
                    // If the local file is modified since the last time it was uploaded on drive, update it.
                    // If not, just leave it as it is. In case of error (eg. the fileInDrive.ModifiedDate is null)
                    // just upload it anyway. Better be safe than sorry :)
                    bool shouldUpload = true;
                    if (fileInDrive.ModifiedDate.HasValue)
                    {
                        DateTime localDate = System.IO.File.GetLastWriteTime(file);
                        DateTime driveDate = (DateTime)fileInDrive.ModifiedDate;

                        if (DateTime.Compare(localDate, driveDate) <= 0)
                            shouldUpload = false;
                    }

                    if (shouldUpload)
                    {
                        mUploader.SetupFile(file, file, dirId);
                        await mUploader.Update();
                    }
                    else
                    {
                        Console.WriteLine("Skipped -- " + file);
                    }
                }
//.........这里部分代码省略.........
开发者ID:TheCrafter,项目名称:GooDPal,代码行数:101,代码来源:DirectorySynchronizer.cs


注:本文中的System.IO.Directory.GetName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。