Search code examples
c#filesolutionprojects

Cannot find referenced file after deploy C#


We are trying to create a website verifier for our internal site, to verify that all sites are up. For this, we use seleniums chromedriver.

We have 2 projects for this: 1 main project which executes the code. 1 "shared" project, which is shared between all of our different solution. This project contains data, which is used across multiple solutions.

We have placed the chromedriver in the shared project, and initialize it like this:

        public static IWebDriver InitiateChromeDriver()
    {
        ChromeOptions option = new ChromeOptions();

        option.AddUserProfilePreference("download.default_directory", downloadPath);
        option.AddUserProfilePreference("disable-popup-blocking", "true");

        var path = Path.GetFullPath("Utility");

        Console.WriteLine(path);

        IWebDriver driver = new ChromeDriver(path, option, TimeSpan.FromMinutes(20));

        return driver;
    }

This method is placed in the "Utility" folder, together with the Chromedriver.exe, and can run locally when debugging through Visual Studio. When we deploy it to our production server, it cannot find the path to the chromedriver. The referenced path changes to C:\windows\system32\inetsrv\Utility\chromedriver.exe on our production server.

What is a better approach at referencing the file, and ensuring that the path is correct?


Solution

  • Try the below. Create a folder called drivers and add the chromedriver to it.

                    ChromeOptions options = new ChromeOptions();
                    option.AddUserProfilePreference("disable-popup-blocking", "true");
                    driver = new ChromeDriver(Path.Combine(GetBasePath, @"Drivers\\"), options);
    
    
        public static string GetBasePath
        {
            get
            {
                var basePath =
                    System.IO.Path.GetDirectoryName((System.Reflection.Assembly.GetExecutingAssembly().Location));
                basePath = basePath.Substring(0, basePath.Length - 10);
                return basePath;
            }
        }