-3

Are there any guides for saving data from a C# application to its own external project file such as embedding images, etc. to be loaded later? Thanks.

theundertaker
  • 99
  • 2
  • 11
  • What kind of "data"? User documents? Application settings? Temporary scratch data? Caches? Is it shared among users? Among applications? Do you have any security or roaming requirements? Will you be updating this data? Does it have to be synchronized with other data? – Dour High Arch Aug 21 '12 at 22:32
  • No, just a standard project file with binary data. – theundertaker Aug 21 '12 at 23:26

3 Answers3

1

You can embed just about any type of data in a .NET assembly and read from it, but you cannot modify your own executable(s), and even if you could, that would be a bad idea. For one, think about how trippy that would be for an antivirus program seeing an EXE suddenly change while running.

If you have metadata (images or any other type of file) you need to read and write, just use the filesystem. The Environment class has functions that will return well-known per application and per-user locations where you can store anything you want.

Edit

I'm not sure if this is what you're asking, but here it goes. Let's say you have a class called Document that contains some text and some images, which are rendered in some unspecified way. So basically it would look like this:

[Serializable]
class Document
{
    public string Text { get; set; }
    public Image[] Images { get; set; }
}

Not sure what Image is, I just made it up. Using the .NET serialization functions you can turn this into a byte stream:

Document doc = GetDocumentFromUI();

MemoryStream stm = new MemoryStream(1024);
BinaryFormatter fmt = new BinaryFormatter();
fmt.Serialize(stm, doc);
byte[] data = stm.ToArray();

Which you can then save to a file. Then you can load the file and turn it into a document:

byte[] data = LoadDocumentFromDisk();

BinaryFormatter fmt = new BinaryFormatter();
MemoryStream stm = new MemoryStream(data);
stm.Position = 0;
Document doc = fmt.Deserialize(stm) as Document;

This is very rough obviously, but you can serialize and deserialize most objects in .NET, including some of the built-in types as well.

kprobst
  • 16,165
  • 5
  • 32
  • 53
  • No, no. I didn't mean to save data inside the application itself; although I see how you may have thought that was my meaning. I am talking about *external* project files like **PSD** for Photoshop and **DOC** for Word. Binary data and all that. See my edit to the original post for clarification. – theundertaker Aug 21 '12 at 21:07
  • 1
    OK, tell me what you mean by 'external project' please. – kprobst Aug 21 '12 at 21:24
  • Like how any application saves general data like a title, and in this case, the images managed by it embedded within a saved file (something like a **Word Document** which is a poor example but effective enough). – theundertaker Aug 21 '12 at 21:28
  • OK, I'm not sure if I'm following you but I updated my answer. I've done this before; it's a 'cheap' way to get a document format that translates directly to an object graph. – kprobst Aug 21 '12 at 21:49
0

It is not possible to do what you want. You can only read embedded resources, not modify them.

ie.
  • 5,982
  • 1
  • 29
  • 44
0

Just serialize the data you want to save and deserialize it upon loading. If you want to write more specific data you'll have to determined your own file format.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272