-1

I need a way of finding a folder for a portable application which can always be found if it exists (and otherwise it can be created), and which is accessible to the user and the application, such that the files within it can be edited and added to by the user, and read/edited by the application (so, embedded resources do not solve the problem).

My issue is that I don't know where that default folder/directory should be. Should it be in the same folder as the application .exe? Should I be using a SpecialFolder? Or something else entirely?

This is really more a question about standard practices for portable applications in C# and .NET, rather than a question on how to implement this directly (although links to any specific classes/code mentioned would still be very helpful).

My thoughts so far:

  • I prefer the idea of it being placed in the same directory as the .exe, as then if the application folder is moved the important files can still be easily located manually. However, if the user were to move the application to Program Files, or something (which doesn't seem too unreasonable?), I worry that the files may end up requiring Administrator privileges to edit/open, which will break the application.

  • Using a user directory, or similar, would also work, but I don't know whether it's acceptable standard practice for a portable application to be writing to user folders (seems a bit off to me, personally). But I don't know if there's a way to make this more palatable?

Any suggestions would be greatly appreciated.

Gebodal
  • 345
  • 2
  • 12
  • https://stackoverflow.com/questions/3991933/get-path-for-my-exe This is answer, to your question. – Nikolay B. Aug 28 '20 at 17:03
  • @NikolayB. No, I'm afraid it is not. As stated, I am more interested in standard practices, as I'm already well aware of how to find the directories in question, but am unsure which one to use. – Gebodal Aug 28 '20 at 17:25
  • @NikolayB. That has nothing to do with this question. – Etienne de Martel Aug 28 '20 at 18:43
  • use the appropriate [`Environment.SpecialFolder`](https://stackoverflow.com/questions/2501167/). Do not use Program folders or your .exe folder. – Dour High Arch Aug 28 '20 at 19:06
  • @DourHighArch Do you think that's acceptable for a portable application? I know a lot of those locations are "hidden" (and I think Microsoft Office stores template files there), and I only plan on storing very small text files, but I'm worried about cluttering stuff up. – Gebodal Aug 28 '20 at 19:44

3 Answers3

1

In Windows, Program Files is owned by the operating system, not by users and not by application programs. It is a secure location used by the operating system to store third-party data, including binaries. Standard users do not have the ability to write data there.

You must not try to circumvent this to let users or third-party applications to change data there, doing so allows third-parties to install keyloggers, backdoors, new accounts, bitcoin miners, trojans, rootkits, and ransomware into your operating system. Never never do this.

Even if you did this; it won't work. When users uninstall applications, data in Program Files is deleted. If the user or a utility performs a disk repair, data in Program files is deleted and restored from a savepoint. Any data you could store there would be soon deleted. If your application is containerized, your data will be deleted every time the user closed the application.

It is OK to store data in Program Files if it is read-only. If your application needs some kind of permanent lookup data; say a dictionary of words that never changes, store it in Program Files. It will be deleted when the app is deleted, and restored when the app is restored.

If your users want to store data and expect it to be there the next time they run the application, or after they uninstall the app, you have to store it in one of the Windows SpecialFolders. Which one? That depends entirely on who you want to have access to the data, and how they will get it. Is it shared? Do you want to use a file dialog? You haven't explained these things so we can't give you an answer.

None of these folders are hidden; if you want to hide data from the user and other applications you want Isolated Storage.

What do you mean by a “portable application”? Do you mean an application on a USB drive you can plug into any random machine, run your application, store some data, unplug the USB drive, plug it into another machine, run your application again and your data is still there? You are looking for a virtual machine; create an ordinary Windows application, storing data in SpecialFolders. When your application is finished, create a virtual machine on a USB drive and install your application into it. This is how portable disk utilities and web browsers are created. Doing this is a lot of work, and you first have to get your program running normally first.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
  • By "portable application", I meant a program which isn't installed in Program Files, but is rather just unpacked from a ZIP file (or similar) and run directly from the `.exe`. However, I've gone digging in my own file system and found that some programs which fit that description that I use regularly do indeed save application data in the appropriate `SpecialFolder`, and so I am happy to follow their example and your recommendation - thank you for the detailed answer. – Gebodal Aug 29 '20 at 20:35
0

If you're talking about where to put files for a widely distributed application, on Windows, I believe Program Files(x86) would be a good place to use, and if you're talking about Linux distributions, then /etc is normal. A personal realization I had to make in professional development is recognizing that those are places that I normally see as "sacred" or "for real developers" and I AM a real developer.

If this is just a personal application to share with a few people or something to that effect, I think the .exe's location is a perfectly suitable storage location.

Carson
  • 864
  • 1
  • 6
  • 19
  • Each app's Program Files is owned by the OS, not by the user. It is not editable by the user, when you uninstall the app it gets deleted. – Dour High Arch Aug 28 '20 at 19:07
  • It's definitely straddling the boundary between those two, but I am hoping to make it more widely available (honestly, I'm mostly just scared of having to make a proper installer and everything, which is maybe foolish). But, it being a tiny niche/indie piece of software, I was hoping to keep it as a portable exe. I guess I'll start by using the .exe location, and see if it causes any problems... – Gebodal Aug 28 '20 at 19:42
  • Program Files are most definitely editable by the user, and if you are packaging files necessary to run your application along with your software, it is a perfectly viable place to put them @DourHighArch – Carson Sep 01 '20 at 13:51
-2

This is the standard practice for using program directories. All you need to do is take the folder containing the executable file and work with it. For example, create the required number of new folders and place objects in them. Even after the user moves the main directory with the executable file, your program will be able to find the new path and all files contained there without any problems.

(i cant place this as comment, cause SO demands more reputation from me lol)

Nikolay B.
  • 19
  • 6