Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi, guys!
Sorry to ask you this, but, after researching the topic, I still haven't gotten anywhere. And I'm new at Visual Studio C++/CLI, too!.
I'm using two types of data files in my application:
a) arrays (to be used in comboboxes as menu items), and
b) structures (to hold the data users will be accessing).

I want to save them all to disk, so they'll be ready for the program when it starts, instead of creating them everytime.

I'd be grateful if you helped me with this, specially on how to:

  1. Find out if the file already exists
  2. Save the file to disk, after creating it
  3. Open it for use within the application.

Thanks in advance.
Updated 26-Feb-12 9:47am
Sergey Alexandrovich Kryukov 26-Feb-12 14:43pm    
The language is called C++/CLI. Please change the tag and the name of the language in the text.
Sergey Alexandrovich Kryukov 26-Feb-12 14:47pm    
OK, I did it for you because I needed other improvements.

1 solution

Here are the answers:

  1. Use System.IO.File, please see:[^].
  2. There is no such concept. The concept is: you open file/stream and write to it. The file is finally saved (flashed) on disk when you close it. You can also explicitly flash. If a class implements the interface IDisposable, you can just call it's Dispose method, which is the case if you use the class System.IO.StreamWriter (most recommended for write-only in text form):[^].
  3. By default, all files are opened for exclusive access. You cannot open it again until it is closed. This is the best option for most cases.

If you want to store your classes and structure, consider using serialization instead of explicit use of files.

The best method of serialization is using Data Contract. This approach is the most robust, non-intrusive and easiest to implement. You can store and load (restore) any arbitrary object graph in XML or JSON, even if the graph is not a tree (contains circular reference paths).

Please see:[^].

For more detail, please see my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

Share this answer
Espen Harlinn 26-Feb-12 17:33pm    
Will surely help :)
Sergey Alexandrovich Kryukov 26-Feb-12 19:27pm    
I think so. Thank you, Espen.
Wilmer Lameda 28-Feb-12 4:12am    
THANK YOU. I'm trying to get the syntax right. Will let you know when I solve it.

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