Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a file's path by the name of the file [c#]

First of all I know there are many questions that look like this one but I couldn't really get an answer for my problem.

So in general, I am running a program that needs to get a path to a solution of another visual studio project solution file and I only have the name of the file itself.

For example, my program is running in the background and there is a visual studio project currently being coded, I need to get the path to the solution of this project. The project wasn't compiled yet, it's a new project that was just created. For example MyProject.sln is the file and the directory I am looking for is C:\Users\MyUser\Documents\Visual Studio 2015\Projects\MyProject\MyProject.sln. Of course the project can be anywhere on the computer, like a different drive, or not in the visual studio folder.

I tried using methods such as Path.GetDirectoryName(filename) which returns an empty string, or Path.GetFullPath(path) which returns a wrong path (the one of the program that searches for the path), or Directory.GetDirectories(path,searchPattern) some with the SearchOptions, and then I get authorization errors for SearchOption.AllDirectories on a lot of files.

I am really lost here, I hope someone can help!

like image 234
Ron Bitterman Avatar asked Nov 09 '22 05:11

Ron Bitterman


1 Answers

I don't think you'll be able to get the projects path in visual studio easily.

It would be much easier if your own program was the one to open visual studio and the project - which of course would require some user cooperation

I haven't tried it myself but you'd need to use the devenv command in your own program

Devenv /edit [file1[ file2]]  
like image 198
Sayse Avatar answered Nov 15 '22 06:11

Sayse