Resource Files in Application

joe_pool_is

Well-known member
Joined
Jan 18, 2004
Messages
451
Location
Texas
How would I load a file from a resource?

We have a PDF-like file that is formatted for printing the our label forms (it isnt quite like a PDF, but pretty close. It lets us enter text that comes out as a barcode).

Anyway, the file often gets lost or deleted by someone fooling around with the PC when they should not be.

To resolve this problem, I inserted the file under Project Properties > Resources > Files section.

Im trying to do something like this:
Code:
File.Open(global::Project2.Properties.Resources.File1);
Obviously, this will not compile.

Can I use the Resource object to store files that I can access in my application? If so, how?
 
If you bring up the projects property page then there should be a resources tab - add the file in question in as an existing file.

At runtime you will be able to access this via the resources object as a byte array (you could always open a memory stream over it if you need to use something like a binary reader etc.)
 
Hey Plausibly,

I already have the file(s) in my projects resources, but I cant seem to find a clean way of reading the files out of the resources byte stream.

Ive got a control that I need to pass the files path to, so I need to get the byte stream to a physical location on the PC so that it can be accessed.

I dont work with streams very often, so Im not sure of the best way to do this and the examples I find online dont really address this approach.
 
Ok, here is what I am doing:
C#:
byte[][] data = new byte[][] {
  global::Responder.Properties.Resources.BoxLabel_lbl,
  global::Responder.Properties.Resources.BoxLabel_lvd,
  global::Responder.Properties.Resources.BoxLabel_prv
};
string[] sResFile = new string[] { "BoxLabel_lbl", "BoxLabel_lvd", "BoxLabel_prv" };
for (int i = 0; i < sResFile.Length; i++) {
  string[] part = sResFile[i].Split(new char[] { _ });
  string strFile = string.Format("{0}\\BoxLabel.{1}", Application.CommonAppDataPath, part[1]);
  using (FileStream fs = new FileStream(strFile, FileMode.Create)) {
    fs.Write(data[i], 0, data[i].Length);
    fs.Close();
  }
}
m_objLvDoc = new LabelDocument();
if (m_objLvDoc.Open(Application.CommonAppDataPath + "[URL="file://\\BoxLabel.lbl"]\\BoxLabel.lbl[/URL]", true) == 0) {
  OpenFileDialog ofd = new OpenFileDialog();
  ofd.FileName = Application.CommonAppDataPath + "[URL="file://\\BoxLabel.lbl"]\\BoxLabel.lbl[/URL]";
  ofd.DefaultExt = "lbl";
  ofd.InitialDirectory = Application.CommonAppDataPath;
  if (File.Exists(ofd.FileName) == false) {
    if (ofd.ShowDialog() != DialogResult.OK) return;
  }
  if (m_objLvDoc.Open(ofd.FileName, true) == 0) {
    string msg = string.Format("Unable to open LabelView form.\r\n{0}", m_objLvDoc.LastError);
    MessageBox.Show(msg, "LabelView - Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error, 0);
    Close();
    return;
  }
}
If anyone sees bad logic in my code, please let me know.

If no one sees bad logic, the code above will be there for others to reference.

(I should have posted this in the "Directory / File IO / Registry" section. Could a moderator please relocate this?)
 
If you are under the constraints of another control/library that only accepts a file path as input then Id say that is probably the best youll be able to do.

One thing you may try is to check if the files already exist before you export the resources, using the resources as a "backup" for when/if users delete the files by mistake you can then "refresh" the data from the resources.
 
Those "cs" tags need some work. They jacked up my code so that it just about isnt readable. I think Ill stick with the basic "code" tag for now.

Nate: Is there some way to verify that a file has not been altered? If one of these files is opened in the application that made them (LabelView), small changes could cause the labels to print incorrectly - generally, this means that fields wont show up.

Im worried that copying these files out there every time is going to cause a lot of fragmentation on that drive over time.
 
Do the files need to physically exist on the disk at runtime? If they are not supposed to be changed then installing them into your applications folder would require admin rights on xp or higher to modify them,

How large are these files? Fragmentation isnt likely to be an issue unless these are large files anyway.

If you wanted to detect changes then you could calculate a hash of the files and store the hash in the exe - at runtime re-hash the files to see if they match.
 
Back
Top