You're right, you can't bake a path into your program that is specific to your computer because then it won't work on the user's computer
Jimi makes the wise point that often programs are installed to C:\Program Files
or similar and it's not automatically possible to write to subfolders in there - you'll have to get into asking the user for permission (Elevation) .. headache
Better to decide what you want the path for:
- If you need a temporary path to e.g. download something to then throw it away you can call
Path.GetTempFilename()
or Path.GetTempPath()
- the former creates a 0 byte file with a random name in the user's temp folder, and returns the path. The latter gives you the path to the temp folder so you can create your own file
- If the file is to have some permanence, such as the user saving his work, you should ask the user for it.
SaveFileDialog
and FolderBrowserDialog
are two things you can drop on your windows form and then call ShowDialog()
on to show the uer a UI where they pick a path. After they OK or Cancel, you check if they OK'd or Cancel and proceed using the dialog's Filename
or SelectedPath
respectively (if they OK'd)
When you're writing your files it's easier not to use FileStream unless you really need to seek in the file etc. Easier to just:
System.IO.File.WriteAllText(path here, contents here)
If you have to write the contents of a string variable to a file