For a Windows (desktop) application, do:
Path.Combine(Application.StartupPath, "Files", "test.txt")
The application path is accessible in Application.StartupPath
, and you can use Path.Combine
to combine parts of a path together.
For ASP.net, you need Server.MapPath
instead of Application.StartupPath
:
Path.Combine(Server.MapPath("~", "Files", "test.txt")
Of course since this is all hardcoded, you should also be able to simply do
Server.MapPath("~/Files/test.txt")
Depending on your needs, you may need to use .
instead of ~
. See also Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?.