Suppose I am working on Welcome.cs file. In the constructor I want to print "Welcome". However, If I put the same code in HelloWorld.cs, it should print HellowWorld. How do I do that?
To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null.
Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function. Syntax: public static void Copy (string sourceFileName, string destFileName);
if I've understood correctly, try this.GetType().Name
How to print
[current file name - method name - line number] ?
private static void Log(string text,
[CallerFilePath] string file = "",
[CallerMemberName] string member = "",
[CallerLineNumber] int line = 0)
{
Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}
Test:
Log("Test");
Output:
Program.cs_Main(11): Test
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