C# 7, .Net 4.7.2
In a simple but big .net project I have a quite normal structure.
How can I get the structural path to MyClass.cs from code?
The result should be MyProject/SomeOtherFolder/MyNamespace.MyClass
(I'm just looking for the string, I don't need a reference or anything fancy)
Clarification I'm working in a regular Console project, no AddIn. The project is quite big, so it would be really helpful to be able to do this by code and not manually. I want to do this at runtime.
Additional information The typeof(MyClass).FullName does NOT fit the structural path. The namespaces differ from the project structure.
Additional information II I'm searching for a method that takes the type or name or some other information of MyClass and returns MyProject/SomeOtherFolder/MyNamespace.MyClass.
If you want the path of a source file, you can declare a utility function with a string parameter marked with the System.Runtime.CompilerServices.CallerFilePath attribute, and have that function return the value of that parameter. This will give you the full path to the source file of the caller.
So, that would be as follows:
public static class Utilities
{
public static string GetSourceFilePath( [CallerFilePath] string sourceFileName = "" )
{
return sourceFileName;
}
}
You call it simply like this:
string myPath = Utilities.GetSourceFilePath(); // no parameter!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With