Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello everyone
I am developing a new application and I want to design a new private file which my only application can write and read it (as word file(*.docx), only word program can read it, excel word can't read it)
I don't know how to start (theory,example,...)
please give me how to start
thank you very much!
Posted

You can't "write a file that nobody else can read" - if you can read it, anybody can.
But it is possible to create file that nobody else can understand. It's considered a fairly poor idea these days - even Microsoft use pretty much standard data formats these days - but it's not too difficult.

There are a couple of ways to do this: the most obvious is to use the Cryptography namespace to encrypt and decrypt your files into memory. This should help: http://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledes(v=vs.110).aspx[^] - it gives an example using file streams, but you can use memory streams for teh unencrypted data just as easily.
 
Share this answer
 
Not even Word and Excel write custom binary files anymore. DOCX and XLSX are just XML (embedded in a ZIP file) and anything can read and write them. It's now an open standard.

Your app can read and write XML if that makes sense to you, you can give it any extension you want. Otherwise you can use a binary file that no one is likely to read or write, but that's soooo last century.
 
Share this answer
 
v2
You most likely want to create a new file format, private to your application. That means, as already suggested, that your application understands it while other applications ('standard programs') don't.
If you mean instead, literally, that other applications should be not able open it, then you are out of luck: cannot do that without the help of dedicated hardware.
 
Share this answer
 
CPallini's solution suits your needs most (although the other comments are valid too).

It's kinda hard to answer in detail without more info about the data you want to store in the file, but I'll try to provide a few pointers to help.

(Sorry about the awful formatting, I'm still learning what works here)
1. Decide if you can leverage an existing format.
   XML or text-based formats are the obvious candidates - in both cases support is 
   good from the standard libraries. 
   
   If XML, check the XML namespaces on MSDN to see how to load an XML file. 
   If text, simply open the file and read into a string.
   
   Otherwise, you'll have to design a binary file format.

2. Examine the structure of the data you want to store. Look at the structure of 
   the data and design a file format.

   If XML, write an example of the file using a text editor. Ideally create an XML 
   Schema describing the file - but if you're new to XML that can be a tall task.
   
   For both text and binary files, think of the structure of the data. Most files 
   have a header section that indicates (by offset) where the remaining data in the 
   file is.
   
   Commonly other parts of the file are record-like (the same type of item repeated
   a number of times) or free-form (like free-form text) in which case you may need 
   to decide how to terminate the section, or store the length up front. 

3. To read and write the file, you'll need to use the .NET stream input/output 
   facilities. MSDN has some useful information on this under Common I/O tasks, 
   linked below.


The Wikipedia entry of file formats (Wiki[^]) may offer some useful pointers).

MSDN Common I/O Tasks[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900