c# modifying resource file in a pre-built application - c#

I have a win forms application written in C# which has a resource file, after this application has been built I want to alter the contents of the resource file, how would I go about doing this?

I've realised that I was doing this the wrong way around, instead of building an application and then attempting to modify the resources of that application on the fly I am not creating and building an application (including resources) on the fly and this seems to work perfectly for my purposes.

Related

Android and iOS cross-platform string resources (MonoTouch & MonoDroid)

I'm busy with a cross-platform mobile app using the Xamarin mono libraries and I ran into the following situation...
I've got a bunch of string resources in my monodroid project (strings.xml) that are being used by my layouts and my C# code.
What I would like to to is to move these string resources to a file in a shared project so that both iOS, Android and Windows phone will be able to use them.
What is the best way to achieve this?
*Currently i'm using a Resource.resx file that contains the non-UI strings. And then I have the strings.xml file in the android(monodroid) project to cater for the UI resource strings.
Keep the following in mind:
The same file must be used by all the platforms.
I need the android layouts to also use the same resources. (which currently is the strings.xml file)
It should preferably be one file.
Any opinions?
There are (at least) a couple of 'standard' libraries/tools you could look at:
Rdio's Vernacular tool looks to help with this problem using a GetText layer - https://github.com/rdio/vernacular
MvvmCross uses JSON files to share text (see Localising text in MvvmCross ViewModels)
Both of those are open source - you should be able to make your own design-time or run-time tools out from those.
Another option would be to use a generator to generate the appropriate resources at build time. For simplicity, I decided to create a Resource Migrator that I run with every build.
Essentially, I take everything in my PCL's *.resx files, and generate Android or iOS resources before building the app. This is a similar approach to combining/minifying CSS/JS in a web application during build time... it reduces performance overhead at runtime.

Embed serialized data into executable

I'm currently working on a small project where I have a list of lists of objects, which I need to store between program executions. The scale of the project is in my opinion not large enough to start developing an external DB-solution, so I would like to store the data inside the executable, so the end-user does not have to keep track of multiple files. Is this possible at all? I've been thinking about embedding the file as a resource, but as I have read, it is not possible to edit this resource file without recompiling the project, so this is not a solution. Alternatively I have read about Alternate Data Streams, but I don't know if it is a good idea to edit the executable this way?
So all in all I need the executable to store data between executions, without the need for managing other files.
I hope you are able to help me.
I believe Application Settings are the way to go here.
More explained at Best practice to save application settings in a Windows Forms Application

Windows forms application localization problem?

I have a windows form application. It has textboxs, checkboxs, labels and devxpress xtragrids etc. I need to apply localization to my application. I searched the net and came over some solutions about .resx . But its taking time to apply this method and if i need more languages in the future i need to create a new resx than customize that resx for that form.
Is there any other way to create a xml file so that i or translator change only the xml files.
Regards.
Resx files are in XML to begin with. You don't need to "add more resx" when you add language, at least you do not have to add them to your solution/project. What you need is to apply proper build process (I suspect that you build your application directly from Visual Studio, which is not very good idea) - you could use MSBuild to do that. In that case, all you need to do is to place translated files in their right paths and start building.
There are also other localization methods for Winforms applications - for example you might want to try WinRes.

Embed MS Access Database in C# WinForm app

I have a C# winform application that accesses data from an MS Access database. This means my applications requires at least 2 files, the .exe file and the .accdb file. Is it possible to include the database in the .exe file, so my solution consists of a single file (the same way you would include an image in the project resources)? If it is possible, are they any major reasons why it shouldn't be done and how would you access the data from code? The project is a only a little one for personal use so if performance is hit it doesn't matter too much.
thanks in advance
It can be done. Simply add it to your project as you would add any other file (right click project -> Add -> Existing Item), then cancel all the dialogs that will popup offering you to handle it for you, then right click your database from your project explorer, go to properties and select Build Action: Embedded Resource.
Then use the method below to dump your database into a temporary file, which you can create by calling Path.GetTempFileName.
internal void CreateBlankDatabase(string destFile)
{
using (Stream source = new MemoryStream(Properties.Resources.MyEmbeddedDatabase))
using (Stream target = File.Open(destFile, FileMode.Truncate))
{
source.CopyTo(target);
}
}
(Note that MyEmbeddedDatabase would be your embedded database name). Then use your temporary file name in your connection string. Make sure you delete your temporary file after you're done. Also, as other said, you won't be able to modify and save any data.
No it shouldn't be done. How would you send someone and update to the .exe file without them losing their data? Keep it separate.
You need to have a way to manage how your applications installs and the file location in your connection string(s). There could be a \Data subfolder in your app folder with the .accdb file(s) in it.
You probably can't achieve what you want with an access database as an embedded resource, but you effectively get the same result by wrapping all your files in another executable app.
When you run the wrapper application, it extracts the "main" C# app, database file, and an updater app (more on this below) to the temporary files folder and runs the main app.
When the main app is closed, it runs the updater app, passing in the paths to the database file and original wrapper application. The updater app updates the wrapper application file with the changed database file. It then finally deletes the database main app and database file from the temp folder. Unfortunately, the updater app can't delete itself, but you could work around that by adding a command to the runonce section of the registry to delete the updater app on the next reboot.
Instead of figuring out how to extract and insert embedded resources, consider having the wrapper application as a compressed, self-extracting executable (like a self-extracting zip or rar file). Here's a codeproject article that describes how to turn a .Net app into a compressed, self extracting exe.
Access requires to be able to read and write to the file. The OS will lock the exe when it is run so that it can't be changed while in use. This along will cause it to not work, not to mention that Access simple wouldn't be able to read the exe as it is expecting a different file format.

Embedded a *.exe into a dll

does somebody know how can I embedd an exe file into a dll ?
I have a tool which is an exe file that I call from c# code.
The thing is that I want to have 1 dll containing this tool (exe file) and the dll containg my c# code.
Is it possible to embedd this exe file within the resources?
Thx in advance
Sure it is. You can add any file as RC_DATA in application as resource. But I believe you will need to extract it to disk first before calling it!
Which IDE/Language you are using?
[EDIT]
Sorry! you did mention that you are using C#.
Add a resource file to you application (right click application in IDE and select "Add new item".
Use the toolbar in resource editor to add an existing file.
Then extract the exe whenever required by calling code something like:
System.IO.File.WriteAllBytes (#"C:\MyEXE\", Resource1.MyEXE);
It's worth baring in mind that your uses may not be too happy about you doing this. Embedding an executable that they've got no control over into a DLL that you'll extract and run will probably make people worry about the running a Trojan on their machine.
It's better to leave the .EXE in the filesystem and be transparent about what your application is doing.
You can load an Assembly from a byte[]. This can be obtained via the ManifestResourceStream of an embedded resource.
An alternative may be to not embed the .exe itself, but rather include its functionality in the dll, and use rundll32[1] to execute it.
On a side note, remember that when you pull a file from your resources to disk and then execute code on it, you may trigger Windows Data Execution Prevention - basically, Windows tries to automatically detect if something is supposed to be code or data, and if it looks like data (which a resource would), then it will prevent that data from being executed as code.
This becomes a particularly sticky issue if your .NET assembly is going to be used over a network instead of from a local drive - there are all sorts of .NET security configurations that might prevent this from working correctly.
Another option, and not knowing the details of your project, take this with a grain of salt: add a .exe.readme file to your install that describes to any curious users or IT people why there is an executable they weren't expecting in the installation directory :)

Categories

Resources