當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。