I have a text file. Now I have changed its file type from .txt
to .abc
. My VB.NET program loads the text into textboxes from that file. After changing the file type, however, other apps like NotePad and Word are able to open and read my .abc
file.
Is there any way that only my application will be able to open/read from the file and no other app would be able to do so? What I mean is, suppose I have a PhotoShop document .psd
file, no other app, rather that photoshop itself, can open it. How do I make my file unreadable by other apps?
2
Answers
There is no way to prevent an app that you don’t develop from opening any file. The extensions are just there for helping us humans, and maybe a bit for the computer to know the default app you select for an extension.
Like you said, a
.txt
file can be opened by many many apps. You can open a.txt
file with Notepad, Firefox, VSCode, and many others.Same way, a
.psd
file can be opened by many many apps. You can open that.psd
file with Photoshop, but also Notepad, Firefox, and VSCode, and probably the same apps as above.The difference is which apps can read and understand the file.
In order to make a file not understandable by other apps, you need to make it into a format that cannot recognize, because you planned it “in secret”.
Like Visual Vincent said above, you could encrypt the file in a way, or you can have a binary file, that basically only your app knows know to understand.
Since you dont own the app you want the file to be understood by, then you either have to accept that it can be opened by any app that can open files, or you can try to encrypt the file outside the app, or like zipping it with a password, and then decrypting or unzipping when you want to use it.
Firstly, any file can be read unless it is still open by a particular process or service. Even PhotoShop files can be ‘read’ by NotePad – try it!
So, an attempt at my first answer…
You can try a couple of methods to prevent opening the file, for instance, applying a file lock. As an example, SQL Server
.mdf
files are locked by the SQL Server service. This happens because the files are maintained in an open state, however; your application would have to remain running to keep these files open. Technically, though, the files can still be copied.Another way is to set the hidden attribute for the file. This hides the file from the less savvy users, but it will be displayed if the user show’s hidden files.
And my second answer: You refer to the format of files by saying only PhotoShop can read or write its own files (not true, but I know what you’re saying).
The format of the file must be decided by yourself. You must determine how you are going to store the data that you output from your application. It looks like you have been attempting to write your application data into a text file. Perhaps you should try writing to binary files instead. Binary files, while not encrypted, as suggested by Visual Vincent in the comments to your question, still provide a more tailored approach to storing your data.
Binary files write raw binary data instead of humanised text. For instance, if you write an integer to the file it will appear as a string of four bytes, not your usual 123456789 textual format.
So, you really need to clarify what data you want to write to the file, decide on a set structure to your file (as you also have to be able to read it back in to your application) and then be able to write the information.