Gatsby
Gatsby

Reputation: 23

Obtaining exact path without filename

I'm creating an extension for visual studio 2012 and am having a hard time finding the location of the arbitrary file that the extension is running behind. Does anyone have a good way of doing this through the extension? Maybe with reflection or some other sort of Path method?

Upvotes: 0

Views: 2354

Answers (3)

lastlink
lastlink

Reputation: 1745

I used the path extensions

Works great on https://dotnetfiddle.net/

  • using System.IO;
var path = "/test/test2/test.txt";
Console.WriteLine($"GetFileName {Path.GetFileName(path)}");
Console.WriteLine($"GetFullPath {Path.GetFullPath(path)}");
Console.WriteLine($"GetDirectoryName {Path.GetDirectoryName(path)}");
// correct exact path without filename
Console.WriteLine($"dirPath {path.Substring(0, path.Length - Path.GetFileName(path).Length)}");

/*
Prints:
GetFileName test.txt
GetFullPath /test/test2/test.txt
GetDirectoryName /test/test2
dirPath /test/test2/
*/

Note

  • the path separator has to be native to your machine. E.g. /test/test2/test.txt won't work on windows while \test\test2\test.txt will

Upvotes: 1

Gatsby
Gatsby

Reputation: 23

string executingtitle = _applicationObject.Solution.FullName;
string[] title = executingtitle.Split( ',' );
string filename = title[0];
string filepath = Path.GetFullPath(filename);

One of my co-workers helped me out. Here's the code for future reference.

Upvotes: 0

user2097234
user2097234

Reputation:

You can do this:

Assembly.GetExecutingAssembly().Location

Then you need to look at the the functions in Path to find the directory. I think its one of these:

Path.GetDirectoryName
Path.GetPathRoot

Upvotes: 2

Related Questions