0

I have a Visual Studio solution with the following structure:

- root - src - project01 - project02 - config - application.config - database.dacpac 

Now, assuming I am executing a code from the path:

C:\DEV\root\project01\bin\release\project01.exe 

How can I find the first occurrence of the file "application.config" for example?

I know that I can start with this:

public string GetExecutingDirectory(){ { string codeBase = Assembly.GetExecutingAssembly().CodeBase; UriBuilder uri = new UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path); return Path.GetDirectoryName(path); } 

And use it in the following way:

var executingDirectory = GetExecutingDirectory(); 

But now, how can I traverse the Root Tree back and forward until I find the file I am looking for? I tried this code but it doesn't find anything:

var path = Directory .GetFiles(executingDirectory, "**/application.config", System.IO.SearchOption.AllDirectories); 

I need it because I am running Integration Tests from different paths and they need these files which can be in a different location of the solution tree depending on the DEV machine configuration so I need to use the "pattern" because the structure can be different from PC to PC.

1 Answer 1

2

You can do like this:

static string FindConfig() { string appFolder = Path.GetDirectoryName(@"C:\DEV\root\project01\bin\release\project01.exe"); string configPath = null; while (true) { Console.WriteLine(appFolder); string[] files = Directory.GetFiles(appFolder, "*application.config", SearchOption.AllDirectories); foreach (var file in files) { if (file.ToLower().EndsWith(@"\application.config")) { return file; } } appFolder = Path.GetDirectoryName(appFolder); if (appFolder.Length < 4) // "C:\" don't search root drive { break; } } return configPath; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.