To find the path of a file in C#, you can use the Path
class from the System.IO
namespace. Here is an example of how to get the path of a file called "example.txt" in the root directory of the C drive:
main.cs102 chars5 lines
The Path.Combine
method combines the two strings and creates a new file path. The @"C:\"
is used to avoid having to escape the backslash character, as it is a special character in C#.
Another way to achieve the same result is to use the Directory.GetCurrentDirectory()
method to get the current working directory and then combine it with the file name:
main.cs171 chars6 lines
This will get the current directory of the running application and combine it with the file name to create the file path. The Console.WriteLine
method is used to display the file path in the console window.
gistlibby LogSnag