joe_pool_is Posted October 14, 2008 Posted October 14, 2008 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 isn't 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. I'm trying to do something like this: 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? Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted October 14, 2008 Administrators Posted October 14, 2008 If you bring up the project's 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.) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
joe_pool_is Posted October 14, 2008 Author Posted October 14, 2008 Hey Plausibly, I already have the file(s) in my project's resources, but I can't seem to find a clean way of reading the files out of the resource's byte stream. I've got a control that I need to pass the file's path to, so I need to get the byte stream to a physical location on the PC so that it can be accessed. I don't work with streams very often, so I'm not sure of the best way to do this and the examples I find online don't really address this approach. Quote Avoid Sears Home Improvement
joe_pool_is Posted October 14, 2008 Author Posted October 14, 2008 Ok, here is what I am doing: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?) Quote Avoid Sears Home Improvement
Nate Bross Posted October 14, 2008 Posted October 14, 2008 If you are under the constraints of another control/library that only accepts a file path as input then I'd say that is probably the best you'll 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
joe_pool_is Posted October 14, 2008 Author Posted October 14, 2008 Those "cs" tags need some work. They jacked up my code so that it just about isn't readable. I think I'll 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 won't show up. I'm worried that copying these files out there every time is going to cause a lot of fragmentation on that drive over time. Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted October 14, 2008 Administrators Posted October 14, 2008 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 application's folder would require admin rights on xp or higher to modify them, How large are these files? Fragmentation isn't 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.