C# Directory.GetParent的代码示例

通过代码示例来学习C# Directory.GetParent方法

通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。


Directory.GetParent是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的Directory.GetParent() 已经帮大家高亮显示了,大家可以重点学习Directory.GetParent() 方法的写法,从而快速掌握该方法的应用。

Directory.GetParent的代码示例1 - TestLongPaths()

    using System.IO;

        // Unzip src/SauronEyeTests/testfiles.zip first
        [TestMethod]
        public void TestLongPaths() {
            var LongDirectories = new List {
                Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"\testfiles\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ienflaflieflanfeifnalnfae\dfiwnfwnfwnfownefinewnf.txt",
            };
            var Regex = new SauronEye.RegexSearch(new List { "pass" } );
            var Keywords = new List { "pass" };
            var ContentSearcher = new SauronEye.ContentsSearcher(LongDirectories, Keywords, Regex, 1024);
            ContentSearcher.Search();

            var currentConsoleOut = Console.Out;

            var outputPath = @"ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ininfafasf ienflaflieflanfeifnalnfae\ienflaflieflanfeifnalnfae\dfiwnfwnfwnfownefinewnf.txt";
            var outputMatch = @"this is pass";
            using (var consoleOutput = new ConsoleOutput())
            {
                ContentSearcher.Search();
                Assert.IsTrue(consoleOutput.GetOuput().Contains(outputPath));
                Assert.IsTrue(consoleOutput.GetOuput().Contains(outputMatch));
            }

            Assert.AreEqual(currentConsoleOut, Console.Out);

        }
    

开发者ID:vivami,项目名称:SauronEye,代码行数:27,代码来源:UnitTests.cs

在TestLongPaths()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

Directory.GetParent的代码示例2 - OnUpButtonClicked()

    using System.IO;

		private void OnUpButtonClicked()
		{
#if !UNITY_EDITOR && UNITY_ANDROID
			if( FileBrowserHelpers.ShouldUseSAF )
			{
				string parentPath = FileBrowserHelpers.GetDirectoryName( m_currentPath );
				if( !string.IsNullOrEmpty( parentPath ) && ( FileBrowserHelpers.ShouldUseSAFForPath( parentPath ) || FileBrowserHelpers.DirectoryExists( parentPath ) ) ) // DirectoryExists: Directory may not be accessible on Android 10+, this function checks that
					CurrentPath = parentPath;
			}
			else
#endif
			{
				try // When "C:/" or "C:" is typed instead of "C:\", an exception is thrown
				{
					DirectoryInfo parentPath = Directory.GetParent( m_currentPath );
					if( parentPath != null )
						CurrentPath = parentPath.FullName;
				}
				catch
				{
				}
			}
		}
    

开发者ID:yasirkula,项目名称:UnitySimpleFileBrowser,代码行数:25,代码来源:FileBrowser.cs

在OnUpButtonClicked()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

Directory.GetParent的代码示例3 - MatchIncludeInParentPath()

    using System.IO;

        private string MatchIncludeInParentPath(string filePath, string initialDirectory, IncludeType includeMatchType)
        {
            string matchPath = Path.Combine(initialDirectory, filePath);
            bool matchPathExists = Util.FileExists(matchPath);

            if (matchPathExists && includeMatchType == IncludeType.NearestMatchInParentPath)
            {
                return matchPath;
            }

            // backtrace one level in the path
            string matchResult = null;
            DirectoryInfo info = Directory.GetParent(initialDirectory);
            if (info != null)
            {
                string parentPath = info.FullName;

                if (!parentPath.Equals(initialDirectory))
                {
                    matchResult = MatchIncludeInParentPath(filePath, parentPath, includeMatchType);
                }
            }

            if (matchPathExists && matchResult == null)
                return matchPath;

            return matchResult;
        }
    

开发者ID:ubisoft,项目名称:Sharpmake,代码行数:30,代码来源:AttributeParsers.cs

在MatchIncludeInParentPath()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

Directory.GetParent的代码示例4 - PrepareSourceRootFolders()

    using System.IO;

        private void PrepareSourceRootFolders(Project project, Project.Configuration configuration)
        {
            List folders = new List();

            // Add subfolders instead of SourceRootPath to make the project's hierarchy looks like VS
            foreach (var folder in Directory.GetDirectories(project.SourceRootPath))
                folders.Add(folder);

            foreach (var folder in project.AdditionalSourceRootPaths)
            {
                // suppose 'workspaceFolder/projectA/tmp/autogen' in AdditionalSourceRootPaths
                // SourceRootPath=workspaceFolder/projectA/src
                // then 'workspaceFolder/projectA/tmp' is expected to be added into 'folders'.
                // 'workspaceFolder/projectA/tmp/autogen' will be added later as child of ProjectFolder(workspaceFolder/projectA/tmp) in AddInFileSystem() if there's any file under 'workspaceFolder/projectA/tmp/autogen'
                string candidateFolder = GetLongestCommonPath(folder, project.SourceRootPath);
                if (candidateFolder.Equals(string.Empty))
                {
                    candidateFolder = folder;
                }
                if (folders.Contains(candidateFolder))
                    continue;
                folders.Add(candidateFolder);
            }

            // blob path
            foreach (var conf in project.Configurations)
            {
                if (!conf.IsBlobbed)
                    continue;
                string folder = GetLongestCommonPath(conf.BlobPath, project.SourceRootPath);
                if (folder.Equals(string.Empty))
                {
                    folder = conf.BlobPath;
                }
                if (folders.Contains(folder))
                    continue;
                folders.Add(folder);
            }

            string projectPath = Directory.GetParent(configuration.ProjectFullFileNameWithExtension).FullName;
            // add source root folders
            foreach (string folder in folders)
            {
                AddOrGetFolderInFileSystem(folder, projectPath);
            }
        }
    

开发者ID:ubisoft,项目名称:Sharpmake,代码行数:48,代码来源:XCodeProj.cs

在PrepareSourceRootFolders()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

Directory.GetParent的代码示例5 - PreStartCleanUpAfterPortabilityUpdate()

    using System.IO;

        ///
        ///This method should be run at first before all methods during start up and should be run before determining which data location
        ///will be used for Flow Launcher.
        ///
        public void PreStartCleanUpAfterPortabilityUpdate()
        {
            // Specify here so this method does not rely on other environment variables to initialise
            var portableDataDir = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(), "UserData");
            var roamingDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FlowLauncher");

            // Get full path to the .dead files for each case
            var portableDataDeleteFilePath = Path.Combine(portableDataDir, DataLocation.DeletionIndicatorFile);
            var roamingDataDeleteFilePath = Path.Combine(roamingDataDir, DataLocation.DeletionIndicatorFile);

            // If the data folder in %appdata% is marked for deletion,
            // delete it and prompt the user to pick the portable data location
            if (File.Exists(roamingDataDeleteFilePath))
            {
                FilesFolders.RemoveFolderIfExists(roamingDataDir);

                if (MessageBox.Show("Flow Launcher has detected you enabled portable mode, " +
                                    "would you like to move it to a different location?", string.Empty,
                                    MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    FilesFolders.OpenPath(Constant.RootDirectory);

                    Environment.Exit(0);
                }
            }
            // Otherwise, if the portable data folder is marked for deletion,
            // delete it and notify the user about it.
            else if (File.Exists(portableDataDeleteFilePath))
            {
                FilesFolders.RemoveFolderIfExists(portableDataDir);

                MessageBox.Show("Flow Launcher has detected you disabled portable mode, " +
                                    "the relevant shortcuts and uninstaller entry have been created");
            }
        }
    

开发者ID:Flow-Launcher,项目名称:Flow.Launcher,代码行数:41,代码来源:Portable.cs

在PreStartCleanUpAfterPortabilityUpdate()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

Directory.GetParent的代码示例6 - CopyNewImageToUserDataDirectoryIfRequired()

    using System.IO;

        public void CopyNewImageToUserDataDirectoryIfRequired(
            SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage)
        {
            var destinationFileNameFullPath = Path.Combine(Main.CustomImagesDirectory, Path.GetFileName(fullpathToSelectedImage));

            var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();

            if (parentDirectorySelectedImg != Main.CustomImagesDirectory && parentDirectorySelectedImg != Main.DefaultImagesDirectory)
            {
                try
                {
                    File.Copy(fullpathToSelectedImage, destinationFileNameFullPath);
                }
                catch (Exception)
                {
#if DEBUG
                    throw;
#else
                MessageBox.Show(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath));
                UpdateIconAttributes(selectedSearchSource, fullPathToOriginalImage);
#endif
                }
            }
        }
    

开发者ID:Flow-Launcher,项目名称:Flow.Launcher,代码行数:26,代码来源:SearchSourceViewModel.cs

在CopyNewImageToUserDataDirectoryIfRequired()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

Directory.GetParent的代码示例7 - Win32Program()

    using System.IO;

        private static Win32 Win32Program(string path)
        {
            try
            {
                var p = new Win32
                {
                    Name = Path.GetFileNameWithoutExtension(path),
                    IcoPath = path,
                    FullPath = path,
                    UniqueIdentifier = path,
                    ParentDirectory = Directory.GetParent(path).FullName,
                    Description = string.Empty,
                    Valid = true,
                    Enabled = true
                };
                return p;
            }
            catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
            {
                ProgramLogger.LogException($"|Win32|Win32Program|{path}" +
                                           $"|Permission denied when trying to load the program from {path}", e);

                return Default;
            }
#if !DEBUG
            catch (Exception e)
            {
                ProgramLogger.LogException($"|Win32|Win32Program|{path}" +
                                                "|An unexpected error occurred in the calling method Win32Program", e);

                return Default;
            }
#endif
        }
    

开发者ID:Flow-Launcher,项目名称:Flow.Launcher,代码行数:36,代码来源:Win32.cs

在Win32Program()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

Directory.GetParent的代码示例8 - DetectSharepoint()

    using System.IO;

		private static Task> DetectSharepoint()
		{
			using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
			if (oneDriveAccountsKey is null)
			{
				return Task.FromResult>(null);
			}

			var sharepointAccounts = new List();
			foreach (var account in oneDriveAccountsKey.GetSubKeyNames())
			{
				var accountKey = oneDriveAccountsKey.OpenSubKey(account);
				if (accountKey is null)
					continue;

				var userFolderToExcludeFromResults = (string)accountKey.GetValue("UserFolder", "");

				var sharePointParentFolders = new List();
				using (var mountPointsKey = accountKey.OpenSubKey("ScopeIdToMountPointPathCache"))
				{
					if (mountPointsKey is null)
					{
						continue;
					}

					var valueNames = mountPointsKey.GetValueNames();
					foreach (var valueName in valueNames)
					{
						var directory = (string?)mountPointsKey.GetValue(valueName, null);
						if (directory != null && !string.Equals(directory, userFolderToExcludeFromResults, StringComparison.OrdinalIgnoreCase))
						{
							var parentFolder = Directory.GetParent(directory);
							if (parentFolder != null)
								sharePointParentFolders.Add(parentFolder);
						}
					}
				}

				sharePointParentFolders.Sort((left, right) => left.FullName.CompareTo(right.FullName));

				foreach (var sharePointParentFolder in sharePointParentFolders)
				{
					string name = $"SharePoint - {sharePointParentFolder.Name}";
					if (!sharepointAccounts.Any(acc => string.Equals(acc.Name, name, StringComparison.OrdinalIgnoreCase)))
					{
						sharepointAccounts.Add(new CloudProvider(CloudProviders.OneDriveCommercial)
						{
							Name = name,
							SyncFolder = sharePointParentFolder.FullName,
						});
					}
				}
			}

			return Task.FromResult>(sharepointAccounts);
		}
    

开发者ID:files-community,项目名称:Files,代码行数:58,代码来源:CloudDrivesDetector.cs

在DetectSharepoint()方法中,Directory的代码示例类中的GetParent的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

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