How can I save a project file as a bundle? - c#

I'm working on an app that needs to save some data and images together into a project file. I assume this project file will have to be some sort of bundle. An example of this would be a project file in Cakewalk SONAR, an audio editor.
To be more precise, I'm working on an application that creates trivia games. When the user exports a game project as the finalized "end product," it will include text and images. This is the "bundle" that I want to create.

You can always save the files to folder and use some archivator like zip with password. And you can rename the file extension to something like .bnd or .prd

File, Save As..., select "Sonar Bundle (.CWB)" as file type.

Related

How to create text file in .NET project folder, not in bin/Debug folder where is by default

I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.
using (StreamWriter file = File.AppendText("log1.txt"))
{
file.WriteLine("Hello from the text file");
}
If the file does not exist, the application creates it in the autogenerated folder bin/Debug.
Is there a way to create this file in the project's directory, where I have .csproj file?
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug?
No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:
User Data
Roaming User Data
All User Data
You can acess the above folders in .NET application using the Environment.SpecialFolder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
As per your given code, try this :
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
file.WriteLine("Hello from the text file");
}
This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.
That's why .NET creates them there firstly?
If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.
You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.
If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.

How do you add files to projects?

Im currently trying to add 2 files to my project. One of them is a .ovpn (vpn Config file) and the other is a batch file.
Im trying to create a directory in my code and then add these files to that newly created directory so when the users first run's this application it creates the files needed to run the application.
Is there anyway of adding these files like a resource so I can just reference them easy?
Sorry if this is a really simple question. Just never done it before.
Thankyou in advance!
You can try to change properties of your files like:
You don't need to necessarily have them embedded in the executable to have them easily referenced.
Add a 'resources' type object to your project - call it Resources.res
Right click on your project, make a New Folder
Call it 'Resources' - add your resource files in here.
Double click on your Resources.res file
Click on the 'Add New Existing File'
Navigate to your file that is in the Resources directory and select it.
You should see your file showing up inside the resources viewer now.
When you are writing code, you can now reference your added files via:
Resources.<nameoffile>
If your file is a text file, you can do things like
string jsonRequest = Resources.RegistrationRequest;
This should automatically set the files to be embedded as well, so they should be accessible at run time.

Add Text file to assets folder after export apk file with C#

Just assume some persons's information exist in Asp.net developed application.
and also another application have been written to display the data of mentioned persons for android(by Eclipse).
How could make change in "APK" file after adding data for new Person by Asp to get the data of mentioned person from website.
How mentioned person ID could be placed as text file in assets folder while APK SIGN dont interfere it.
I want have personal apk for each person and personal apk created(or modified) by Asp website(This APK contain a text file with MyPersonId and it added to ASSETS folder with C#).
I was trying to do the same, and i'm almost there.
Basically i used MIT AppInventor to create an APK for Android. On it, i created a TXT file in the assets folder. The MIT AppInventor compiled the code to create the APK file for distribution.
I than placed the APK in a folder, owned by an ASP.net page. The goal would be to open the APK file (as a Zip, currently using the ZipSharpLib), locate the "info.txt" on the assets folder, and replace it for another.
My problem so far is on the "re-pack" ou "re-compress" the copy of the file; android is not recognizing it as a valid APK.
So, up to the "decompress" the APK, run every entry, locating the one that is the asset to replace i'm good (just use ZipSharpLib, classes ZipFile and ZipEntry - they show you how on their site). But on the "compress" a new changed file, i'm failing.
To do this kind of things you would need help of a decompilation and recompilation tool such as ApkTool. You can't just zip back the files and hope android recognize zip format as an apk format immediately. There's a signing process included to make it a "real apk" again

XNA - How to add settings.ini to the game by default when installing?

I got a settings.ini file, which I want to be included in the game when installed.
It should be in a folder called Settings and it should be in the same directory as the rest of the game (Like the executable and the default content folder).
I thought I would just add an extra empty content project to my game solution and add the settings.ini file to it.
Guess What! It doesn't work.
It gives me the following error:
Error 6 Cannot autodetect which importer to use for "Settings.ini".
There are no importers which handle this file type. Specify the
importer that handles this file type in your project.
PATH_TO_GAME\Settings\Settings.ini
I googled a bit, but it looks like I need to write my own content pipeline.
Is there a better/more easy way to do this? I don't want to waste my time on writing this very difficult part.
PS. If I install the game and add the settings.ini to the settings directory, I am able to write into and read from the settings.ini file. But now I have to add the settings.ini file manually, which I don't want. I want it to be supplied with the game when you install it.
In your game project solution (not content project!), add a Settings folder, and the Settings.ini file. You might use Add -> Existing Item... for the file if you already have it.
Under Properties for the file, set Build Action to Content and Copy to Output Directory to Copy if newer.
Your settings file will now be deployed with your application.
Take all the contents of your .ini file and put them into your game's code:
string path = #"path\to\your.ini";
if (!File.Exists(path))
using (var sw = File.CreateText(path))
{
sw.WriteLine(#"This is the .ini file text.");
sw.WriteLine(#"It can contain all sorts of characters, like !##$%^&*()_+|");
sw.WriteLine(#"But you need to use double quotes "" so you don't get errors");
}

hiding xml file in windows form application

I have a language file and a settings file of my windows application. This files are in xml format and they are shown in the release folder. I want to hide, crpyt or something like that to that files so users can not see either reach my files.
What is the best way to do this.
how does it fit your needs to simply have another extension instead of .xml so the user does not know how to open the files?
you can also set the files as "Embedded Resource" and load from resource so it will be inside the dll itself instead of being available outside as separate file.
other option is to encrypt the file which could be good as well, up to you.
for a very small file, I would embed it.

Categories

Resources