【问题标题】:How to go one step above in a folder path by using AppDomain.CurrentDomain.BaseDirectory in c#如何在 c# 中使用 AppDomain.CurrentDomain.BaseDirectory 在文件夹路径中前进一步
【发布时间】:2021-02-11 01:34:17
【问题描述】:

我正在使用AppDomain.CurrentDomain.BaseDirectory,我想倒退一步,但不知道怎么做?下面是例子,

代码:

string path = AppDomain.CurrentDomain.BaseDirectory;

结果:

"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin\\Debug\\"

预期结果:

"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin"

【问题讨论】:

  • 如果您已经在C: 会发生什么?
  • @O.R.Mapper 你能详细说明一下吗?
  • 如果AppDomain.CurrentDomain.BaseDirectory 是"C:\\" 会发生什么?

标签: c#


【解决方案1】:

您可以使用以下内容来获取给定目录的父目录:

        string dirName = AppDomain.CurrentDomain.BaseDirectory; // Starting Dir
        FileInfo fileInfo = new FileInfo(dirName);
        DirectoryInfo parentDir = fileInfo.Directory.Parent;
        string parentDirName = parentDir.FullName; // Parent of Starting Dir

【讨论】:

  • 谢谢。我正要发布我刚刚发现的类似答案。我的代码:String licenseKeyPath = AppDomain.CurrentDomain.BaseDirectory; DirectoryInfo baseDirInfo = new DirectoryInfo(licenseKeyPath); String oneStepBackwardInPath = baseDirInfo.Parent.FullName; 谢谢。
【解决方案2】:

使用以下sn-p:

string path = (new FileInfo(AppDomain.CurrentDomain.BaseDirectory)).Directory.Parent.FullName;

【讨论】:

    【解决方案3】:

    Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName

    【讨论】:

      猜你喜欢
      • 2015-05-14
      • 2010-12-31
      • 2019-06-19
      • 2015-03-03
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多