0

Im trying to store score input as text file (.txt) and use it later. I created the file with File.Create() and I want to add text using StreamWriter but i don't know how to get the path of the file I created with File.Create().

PS: i don't want to specify a path because it will be used by other people not only me so i created .txt file where my console app is being executed

i tried :

File.Create("lHe.txt");
using(StreamWriter sww= File.AppendText("lHe.txt"))            
{
    sww.WriteLine("+112");
}
DerStarkeBaer
  • 669
  • 8
  • 28

1 Answers1

1

Use Directory.GetCurrentDirectory() as below:

string filePath = Path.Combine(Directory.GetCurrentDirectory(), "lHe.txt");
File.Create(filePath);
using(StreamWriter sww= File.AppendText(filePath))            
{
    sww.WriteLine("+112");
}
VillageTech
  • 1,968
  • 8
  • 18
  • 1
    FYI, This will return the current directory from where the process was launched, but not necessarily where the .exe is located. – Rufus L Dec 05 '19 at 03:19
  • I know that, but the question was about directory, where file will be created. File.Create() uses Directory.GetCurrentDirectory() in case that the passed file name doesn't contain path. – VillageTech Dec 05 '19 at 03:25
  • Good point! I realize now the duplicate is probably not correct and will remove it when I can. +1 for you – Rufus L Dec 05 '19 at 05:10