【问题标题】:How to retrieve the path where the Project folder exists in C#?如何在 C# 中检索 Project 文件夹所在的路径?
【发布时间】:2014-01-04 10:01:05
【问题描述】:
public static void ReadingExcelSheetData()
{
    Console.WriteLine("GetCurrentDirectory Returns :" + System.IO.Directory.GetCurrentDirectory());
    Console.ReadLine();
    controller = new XlsReader(System.IO.Directory.GetCurrentDirectory() + "\\TestCode\\Config\\controller.xls");
    testData = new XlsReader(System.IO.Directory.GetCurrentDirectory() + "\\TestCode\\Config\\testData.xls");
}

public static void  Main()
{
        ReadingExcelSheetData();
        Console.ReadLine();
}

这里 System.IO.Directory.GetCurrentDirectory() 返回 E:\seleniumwith C#\myprojectsC#\Pluralsite\TestCode\TestCode\bin\Debug。

但我们的控制器表位于 E:\selenium 和 C#\myprojectsC#\Pluralsite\TestCode\TestCode\Config 位置。

我只想要 E:\seleniumwith C#\myprojectsC#\Pluralsite\TestCode\TestCode 的路径。 如何得到那个。谁能帮帮我。

谢谢, Nilanjan.

【问题讨论】:

标签: c# selenium


【解决方案1】:

Directory.GetParent(Assembly.GetExecutingAssembly().Location) 会将完整路径返回到您的 {fullpaht}\debug\bin。请记住 bin 文件夹可能有也可能没有您的文件,具体取决于您是否将属性“复制到输出目录”设置为“如果较新则复制”或“始终复制”。如果你还没有,你可以尝试类似的东西

Directory.GetParent(Assembly.GetExecutingAssembly().Location).Parent.Parent 直到它将您带到所需的文件夹位置,然后您可以附加 @"\config\controller.xls"。

using System.IO;
using System.Reflection;

【讨论】:

    【解决方案2】:
    controller = new XlsReader(@"E:\seleniumwith C#\myprojectsC#\Pluralsite\TestCode\TestCode\Config\controller.xls");
    testData = new XlsReader(@"E:\seleniumwith C#\myprojectsC#\Pluralsite\TestCode\TestCode\Config\testData.xls");
    

    【讨论】:

      【解决方案3】:

      Application.StartupPath 属性有助于查找应用程序启动路径。
      对于您的 SO 问题,以下代码将起作用。

      string exeFolder = (System.IO.Path.GetDirectoryName(Application.StartupPath)).Substring(0, (System.IO.Path.GetDirectoryName(Application.StartupPath)).Length - 3);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-27
        • 1970-01-01
        相关资源
        最近更新 更多