Home > Enterprise >  Is there way to take the path of file from the main folder of project?
Is there way to take the path of file from the main folder of project?

Time:09-17

i tried to create access to file that loacated in main project folder, but i get error that says the file not found in another folder in project System.IO.FileNotFoundException: Could not find file ,this is thepath which trying to find the file

C:\Users\User\source\repos\AutomationAssignment\AutomationAssignment\bin\Debug\netcoreapp3.1\DataEX.xlsx'

This is the path that i want to use

C:\Users\User\source\repos\AutomationAssignment\AutomationAssignment\DataEX.xlsx'

        static string path = Path.Combine("DataEX.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(File.Open(path, FileMode.Open));
        string url = System.IO.File.ReadAllText("Url.txt");

CodePudding user response:

The spreadsheet should be copied to the build output directory, so it resides in the same folder that your test project DLL is in.

  1. Add the spreadsheet to your test project so it shows up in the Solution Explorer panel in Visual Studio.

    If you can already see the spreadsheet in Solution Explorer, skip this step.

  2. Right-click on the spreadsheet in Solution Explorer, and choose "Properties".

  3. Set the "Build Action" property to "None".

  4. Change the "Copy To Output Directory" to "Copy Always".

    You can set this to "Copy If Newer" but this typically looks at file modification dates. I'm paranoid, so I have it copy the file every time. This is fine for small files.

  5. The spreadsheet should exist in the same folder as the DLL file running your tests. Now change your code:

    var folder = Path.GetDirectoryName(GetType().Assembly.Location);
    var path = Path.Combine(folder, "DataEX.xlsx");
    var workbook = new XSSFWorkbook(File.Open(path, FileMode.Open));
    
  • Related