|
Is there a way to return the full path name of the file containing the code?
suppose I have the following class in the file c:\myproject\CA.cs
public static class A
{
public static void Show()
{
string myFile = ....
Console.WriteLine(myFile);
}
}
modified 20-Nov-12 17:08pm.
|
|
|
|
|
The only possible reason I could see for you doing so, is if you're working on some sort of dynamic plugin system and you're going to compile/interpret/load the file at runtime - in which case you need a system to register where you're going to look for the code/plugin/etc
Once your code is compiled into an exe or a dll, you can find the path to the executable module or where it was loaded from, but not to a single piece of code - What would be the point, hmm, if a compiled module couldnt be shipped to a seperate machine/location, but that compiled module still had to retain a reference to the source code ?? yuck ??
Maybe Ive mis-interpreted what you're asking
|
|
|
|
|
You can certainly get the fully qualified name.
There might be some method in the Assembly which loaded the class which might provide a path name but I doubt it would be a fully qualified one. (I don't feel like looking at the docs at the current time.)
Your version control system might have 'variables' (or macros, etc - the name is always different) that allows it to insert the path into the source code when you check it in. If so you could create string with that. Of course you would need to do it on every class.
|
|
|
|
|
The best I can think of is the stack trace, but it's probably not worth the trouble.
Remember that a class can be partial or inherit members from base classes.
If you really want to, you could add an Attribute to the class, but you'd have to weigh the costs and benefits of added maintenance.
It still comes down to why you want the information.
|
|
|
|
|
The reason it that the class has a method that should be able to copy its source file whatever it is.
An alternative solution i found:
1) create each file containing one only class so that the file is the class name
2) implementing a method (in a base class that will be overrides), which get the text class name,
and build a name of file (classname +".cs"). Then starting from root directory of project, it will search for the file name in the sub directories..
a workaround.. but it should work
|
|
|
|
|
You still haven't really explained why. Why do you want to copy the file?
And what happens if the app is installed on another system or otherwise separate from the source?
Include the file name in a config file?
Could you include the source as a resource in the app?
Or in an install package?
|
|
|
|
|
it is an utility I use only during the developing, only to copy the file containing the specific class to a specific directory. I don have to distribute it.
This is way: I don ' want to look for the class and do copy and paste of the file manually
|
|
|
|
|
Sounds like a lot of hard work for no particular benefit. You can probably do this more easily within Visual Studio during your build processing.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
If you're using the functionality of this class again and again, why not put it into a class library and reference that instead? That way you don't have to copy/paste.
|
|
|
|