You have a library, and one of the function in the library provides a functionality that, among other things output a file (maybe to different paths in every execution). The content and the name of the file is constant in each execution. What is the best way to carry this out? declare the content of the file as a string and print it each time (probably not a good idea, the file is around 1000 lines long)? or have a file which always exist in the same folder as the library dll? How to make sure that the compiler will always include this file? Or is there any better way, for example embedding the file to the dll somehow?
Note that outputting this file is not the only thing that the library do, it's one of the subtasks that is done by a function in the library
Thanks!
If the content is going to be the same in every case and it's non-trivial (i.e. you couldn't just write it out in a couple of small lines of code) then embed it as a resource file in the assembly.
Then you can just use Assembly.GetManifestResourceStream to get an input stream, and copy the data out to a file. (If you're using .NET 4.0, you can use the Stream.CopyTo method and you're done.)
You can create a Resource file in your VS2010 project and have typed access to the file as a static member of an automatically generated class. The resource will then be embedded automatically as well.
Try Add New Item -> Resources File. Once the file is created, drag your file onto the designer surface.
Alternatively there is the file.copy method http://msdn.microsoft.com/en-us/library/system.io.file.copy(VS.71).aspx
You can just include the file with your project (Add existing item) and copy it whenever needed.
Related
Is it possible to store an actual XML file in a Visual Studio 2010 project and then refer to it in code?
ie. Can I store an XML file (see fragment below) and then refer to data elements within the XML data? How can this be done and how would you refer to data elements deep within the XML data using C# or VB code?
If the XML file is added to the project, will it be embedded into the DLL or EXE when compiled? If not, how can it be embedded into the DLL or EXE?
<readReferenceDataResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<readReferenceDataResult xmlns="http://test.website.com/hi/xsd/providermessages/ReadReferenceData/5.6.9">
<elementReferenceValues>
<elementName>organisationTypeCode</elementName>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">1678</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Accupuncture and Eastern Medicine</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">6454</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Technical and Laboratory Services</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">9782</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Naturopathy and Natural Health</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9>8557</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Aged Care Services</referenceDescription>
Just add a resource file (.resx) to your project if one is not already present. Select File as the resource type and drag your XML file into that and give it a name (if you don't like the automatically generated one).
This will create an embedded resource type which you can then access in code by the automatically generated class representing the resource, so for example if your project is called Foo and you add a resource file called Resources and add an XML file resource called MyData, then you can access that resource at Foo.Resources.MyData in code. This will expose the contents of the file as a string, which you can then load into an XmlDocument object when you need to by using XmlDocument.LoadXml.
By the sounds of it, the best solution I can think of would be using XmlDocument
You use the XmlDocument to hold the XML file you want to read and then you can retrieve the elements/nodes from there.
If you wanted something a bit faster you can also use Linq to XML, which is nice and simple to use.
Finally, if you have memory constraints/don't want to load the whole document into memory to read it every so often you can use XmlReader. Which allows you to read it directly from file, node by node. It is however forward only, and is a bit tricky to use.
Hello I am making an application that use different .wav files.
What i want is to compress those wavs into a .example file with a custom extension and them make my application use those wavs
The point for this is for creating .wav packs so when a user load the .example file it will load the .wav files compressed in the file...
So if i load the file "pack1.bmkv" it will load as i said the .wav files compressed with it.
If this really must be a DLL, then you only need to create a new Project in VS and assign resources to it in the normal way.
However, a much better (and probably easier) way to do it, is to just use a ZIP file with a custom extension and possibly a password.
Where Visual Studio 2010 (using c#) stores the resources? I need to monitor the files that stores resources in my product to know when I need a new translation.
I have found this article that talks about it:
http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.90).aspx
According to that, all the files of type *.resx
I was wondering if there were any exceptions to that. In other words, it is possible that some resources would get stored in files with extension different from *.resx?
Thanks
-Tony
It actually tells you in the first paragraph in the the link you provided
Visual C# applications often include data that is not source code.
Such data is referred to as a project resource and it can include
binary data, text files, audio or video files, string tables, icons,
images, XML files, or any other type of data that your application
requires. Project resource data is stored in XML format in the .resx
file (named Resources.resx by default) which can be opened in Solution
Explorer.
If you want to be sure you can always right-click on the file and select open with. This lets you choose a different editor to view the file in.
If you follow recommended practice - yes all text resources are stored in resx files.
There are also other types of resources that may need to be localized - i.e. images may contain text/language specific content.
Note that if code does not strictly follow recommended practice some text that must be localized ends up in source code (i.e. format strings for String.Format calls).
I am building an interface whose primary function would be to act as a file renaming tool (the underlying task here is to manually classify each file within a folder according to rules that describe their content). So far, I have implemented a customized file explorer and a preview window for the files.
I now have to find a way to inform a user if a file has already been renamed (this will show up in the file explorer's listView). The program should be able to read as well as modify that state as the files are renamed. I simply do not know what method is optimal to save this kind of information, as I am not fully used to C#'s potential yet. My initial solution involved text files, but again, I do not know if there should be only one text file for all files and folders or simply a text file per folder indicating the state of its contained items.
A colleague suggested that I use an Excel spreadsheet and then simply import the row or columns corresponding to my query. I tried to find more direct data structures, but again I would feel a lot more comfortable with some outside opinion.
So, what do you think would be the best way to store this kind of data?
PS: There are many thousands of files, all of them TIFF images, located on a remote server to which I have complete access.
I'm not sure what you're asking for, but if you simply want to keep some file's information such as name, date, size etc. you could use the FileInfo class. It is marked as serializable, so that you could easily write an array of them in an xml file by invoking the serialize method of an XmlSerializer.
I am not sure I understand you question. But what I gather you want to basically store the meta-data regarding each file. If this is the case I could make two suggestions.
Store the meta-data in a simple XML file. One XML file per folder if you have multiple folders, the XML file could be a hidden file. Then your custom application can load the file if it exists when you navigate to the folder and present the data to the user.
If you are using NTFS and you know this will always be the case, you can store the meta-data for the file in a file stream. This is not a .NET stream, but a extra stream of data that can be store and moved around with each file without impacting the actual files content. The nice thin about this is that no matter where you move the file, the meta-data will move with the file, as long as it is still on NTFS
Here is more info on the file streams
http://msdn.microsoft.com/en-us/library/aa364404(VS.85).aspx
You could create an object oriented structure and then serialize the root object to a binary file or to an XML file. You could represent just about any structure this way, so you wouldn't have to struggle with the
I do not know if there should be only one text file for all files and folders or simply a text file per folder indicating the state of its contained items.
design issues. You would just have one file containing all of the metadata that you need to store. If you want speedier opening/saving and smaller size, go with binary, and if you want something that other people could open and view and potentially write their own software against, you can use XML.
There's lots of variations on how to do this, but to get you started here is one article from a quick Google:
http://www.codeproject.com/KB/cs/objserial.aspx
I have a portable executable that saves data to a file in the same folder as the executable. Is there any way that I can save data into the executable itself when I close the app?
This maybe weird, but taking the data with me and only have one file for the exe and data would be great.
Would prefer if this was made with C#, but is not a requisite.
You cannot modify your own EXE to contain stored data in anything approaching an elegant or compact way. First off, the OS obtains a lock on the EXE file while the application contained within is being run. Second, an EXE comes pre-compiled (into MSIL at least), and modification of the file's source data usually requires recompilation to reset various pointers to code handles, or else a SERIOUS knowledge on a very esoteric level about what you're doing to the file.
The generally-accepted methods are the application config file, a resource file, or some custom file you create/read/modify at runtime, like you're doing now. Two files for an application should not be cause for concern
You can, by reserving space through the means of using a string resource and pad it out. You need to do a bit of detective work to find out exactly where in the offset to the executable you wish to dump the data into, but be careful, the file will be "in use", so proceed cautiously with that.
So right now you're using an app.config (and Settings.settings) file?
I believe this is the most compact way to save data close to the .exe.
I would highly doubt you can alter the manifest of the .exe, or any other part of it.
Edit: Apparently, there might be some ways after all: http://www.codeproject.com/KB/msil/reflexil.aspx
There is one way using multiple streams, but only works in NTFS filesystems.
NTFS allows you to define alternative "named" streams in one file. The usual content is in the main = unnamed stream. It has something to do with the extra info you can see when you right click a file and check properties.
Unfortunatly C# has no support for multiple streams, but there are open source pojects that can help you.
See this link for a nice wrapper to read and write multiple streams to one single file in C#
Alternate data streams might work. By using the ::stream syntax you can create a data stream within your exe and read/write data.
Edit:
to create/access an alternate data stream, you will use a different filename. Something like:
applicAtion.exe:settings:$data
this will access a data stream named "settings" within application.exe. To do this you need to add the :settings:$data to the filename when reading or writing to the file. This functionality is provided by ntfs so it shold work in c# and should work when the application is running.
Additional information is available at: http://msdn.microsoft.com/en-us/library/aa364404(VS.85).aspx
If you want to take the data with you and only have one file for the exe and data, .zip them into a self-extracting .exe.
you can add data to end of executable file :
var executableName = Process.GetCurrentProcess().MainModule.FileName;
// rename executable file
var newExecutableName = fullPath.Replace(".exe", "_.exe");
FileInfo fi = new FileInfo(executableName);
fi.MoveTo(newExecutableName);
// make copy of executable file to original name
File.Copy(newExecutableName, executableName);
// write data end of new file
var bytes = Encoding.ASCII.GetBytes("new data...");
using (FileStream file = File.OpenWrite(executableName))
{
file.Seek(0, SeekOrigin.End);
file.Write(bytes, 0, bytes.Length);
}
// we can delete old file when exited