Reputation: 271
I have a link which im reading into stream reader - @"C:\Users\James\Desktop\tweets.txt"
I have the text file in the same folder as the source code and .exe
How would i change '@"C:\Users\James\Desktop\tweets.txt"' to a relative file path?
Upvotes: 0
Views: 1198
Reputation: 299
You can use the Application.StartupPath property:
using System.Windows.Forms;
...
string myApplicationPath = Application.StartupPath;
Upvotes: 0
Reputation: 30127
You can get the current assembly/exe path by using Assembly.GetExecutingAssembly().Location
. Once you get that you can easily append the text file name in the path to access your text file
You can also get the path of folder which contains your exe by using System.IO.Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location)
As mentioned by Boo you can do
Path.Combine(System.IO.Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location), "Text File Name");
Upvotes: 1
Reputation: 69392
Do you mean relative path? You could try this:
Uri srFile = new Uri(@"C:\Users\James\Desktop\tweets.txt");
Uri projFile= new Uri(@"C:\MyprojectDirectory\Project\...");
Uri relativePath = projFile.MakeRelativeUri(srFile);
Upvotes: 1