0

I'm starting to play around with FileStream to make a text document. When you do this, you have to clarify a path. Is there a way to create the text document in the folder the EXE file is in?

(I'm asking this because this program is meant to be downloaded, so I think I can't clarify a path specific to my computer)

Thank you!

  • You have different .Net classes that return the path of an Application's executable. What kind of application are you building? Console, Windows Forms, WPF, UWP, ASP-something? Portable? What .Net version? – Jimi Jul 15 '20 at 01:29
  • So what part are you stuck on? Getting the path to the exe or creating the text document? Also may have to be wary of creating test documents in the exe path, you may find that UAT/User permissions may trip you up. What is the purpose of the text documents and would they be better stored as part of the user profile (My Documents or AppData)? – Hursey Jul 15 '20 at 01:30
  • @Hursey I know how to create the text document, I'm just not sure how to save it to the same folder (not the original bin folder) the application is in. The project is Windows Forms and I'm using .NET version 16.6.3 – matthewarthur1 Jul 15 '20 at 02:07
  • There's no such thing as .Net 16.6.3. You're probably referring to the Visual Studio version (2019 v. 16.6.3). Check the .Net version in the Project's Properties (verify whether it's .Net Core 3.x or .Net Framework 4.x.x) – Jimi Jul 15 '20 at 02:14
  • Regardless of which .net, a simple google search will return at least 65 million examples of how to find the executable path, let alone even searching SO https://stackoverflow.com/questions/2593458/executable-directory-where-application-is-running-from – Hursey Jul 15 '20 at 02:43
  • Except, discard everything that suggests `Environment.CurrentDirectory`, `Directory.GetCurrentDirectory()` (everything `Current`: there's no such thing, or, worse, it may *appear* to *work*, until it doesn't. `AppDomain.CurrentDomain.BaseDirectory`, even if it contains a *current* thing, is not bad). Since you're using WinForms, you can rely on `Application.StartupPath`). +++ As already noted, don't be so sure you can write to the executable path. You can usually read from that path, write, not really, when you deploy. Read about the `ProgramData` and `AppData` paths, in a User scope. – Jimi Jul 15 '20 at 02:55
  • Please see [Where is the correct place to store my application specific data?](https://stackoverflow.com/q/10563148/1115360) – Andrew Morton Jul 15 '20 at 08:49

2 Answers2

2

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

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

The best way to create a text file, would be to use CreateText method. It will create a file besides the executable program file. You can go the following way.

Dim sw as StreamWriter = File.CreateText("myfile.txt")
Dim str as String = "Your text"
sw.Write(str)
sw.Flush()
sw.Close()
Hemal
  • 3,682
  • 1
  • 23
  • 54