I would like to embed a .txt file into my C# project storing a list values for the user. These values should be configurable and therefore the .txt will have to be edited during runtime. I have found out that Embedded Resources cannot be modified. Is there any other way to do that?
Thank you.
Store the text file as an embedded resource. The first time your program is run, copy the embedded resource to a file on disk, and use it for the configuration. Your users can edit the disk file.
The embedded resource version serves as a default configuration.
You can use your app.config or web.config configuration files.
Normall if you will use large amout of data using a database is recommended. I assume you really need just a .txt document. In your assembly write a procedure that will create that text file if its not present. To be more specific lets say your program is mainProgram.exe. In onload event of mainprogram.exe write a procedure checkTxtFile(). This procedure first will check if there is a txt file in the directory.If the file is not presend it will create it with the desired values.
You can create a XML file using Filestream and using it during runtime or
make your own protocol format and store the data in the file so that no one can change it.
Over to that you can also encrypt and decrypt the file on the fly.
Lists of modifyable, persistable values are best stored in a database. There are many lightweight options to choose from; in this case I think a SqlLite database would suit your purposes best.
Related
Suppose that I would like to add extra information about a file, without writing that information as content of that file. How would I do this? A couple of good examples are:
With Word documents, you can add Author tag to a document. And,
MP3 files have lots of info stored inside of them but when you play the file, you don't see that info (unless the program playing the file has been programmed to display that information).
How does Windows do this?
This information is stored in the file system (on windows - NTFS).
In NTFS, you can actually store another file, as part of this information, and it stores much more information about each file than you may expected.
NTFS file streams
Exapmle in C how to consume them
About MP3 and word - In these cases the information is stored inside the file, as part of its format.
How do I save my data...
(compressed/encrypted) not human readable in a portable file by
influencing on directory name and file name with using easily
addressable settings by "name" and "value" like in registry/ini
with the possibility to access the same one settings file (machine based)
with any executable
In VB I did that with INI Files, but now I have heard from MS that the Framework offers no function to access INI Files anymore. Since INI Files are also not any longer up-to-date for using them in a new application I wanted something similar. Just to write to a text file line by line is not what I want to do, I will explain why: If I will need a setting named "Label4" I have to read the entire file and search for the line containing it and then I have even to split the result in setting name and setting value (.NET has another syntax split function).
Let's say I have a thousand labels and a hundred textboxes in a form and wanna save its content easily to a file or whatever and read it the same easily back addressing the settings by a name already how would I do that? I am ready to use a DB but don't know how to setup and save information there. Easy would be to access the registry with "name" and "value", but I prefer a portable file.
The data shall be stored compressed or encrypted not in a plain text human readable way (I mean XML / html is not what I search for to store the data else I could research again how to use INI files with C# which might be possible I guess). I stumbled upon "application settings" but this doesn't save my data compressed nor can I simply influence on path and filename of the settings file. With each new EXE all the stored settings are lost again for those new EXE if I have used "application settings". I need not "user/application based" settings but "machine based", I hope you understand what I wanna say. Any App on my comp shall be able to get to my settings file
and I shall be able to choose the place/filename where to save the settings.
You should write to any xml file and get your desired results.
(compressed/encrypted) not human readable in a portable file
You can both encrypt/compress a normal file
influencing on directory name and file name with using easily
Can't understand what you mean
addressable settings by "name" and "value" like in registry/ini
xml gives you flexibility you need
with the possibility to access the same one settings file (machine based)
You can keep it machine based.
with any executable
What does it mean?
As we all know that we can not get the full path of the file using File Upload control, we will follow the process for saving the file in to our application by creating a folder and by getting that folder path as follows
Server.MapPath
But i am having a scenario to select 1200 excel files, not at a time. I will select each and every excel file and read the requied content from that excel and saving the information to Database. While doing this i am saving the files to the Application Folder by creating a folder Excel. As i am having 1200 files all these files will be saved in to this folder after each and every run.
Is it the correct method to follow or not I don't know
I am looking for an alternative solution rather than saving the file to folder. I would like to save the full path of file temporarily until the process was executed.
So can any tell me the best way as per my requirement.
Grrbrr404 is correct. You can perfectly take the byte [] from the FileUpload.PostedFile and save it to the database directly without using the intermediate folder. You could store the file name with extension on a separate column so you know how to stream it later, in case you need to.
The debate of whether it's good or bad to store these things on the database itself or on the filesystem is very heated. I don't think either approach is best over the other; you'll have to look at your resources and your particular situation and make the appropriate decision. Search for "Store images on database or filesystem" in here or Google and you'll see what I mean.
See this one, for example.
I'm fairly new to coding, and I just got help figuring out how to create a Xml file; now I want to know, is there a way to protect my Xml file from being edited?
I'm making a simple Command Prompt game, and I'm going to include an Xml file for info storage purposes. Although I don't want the user to be able to change the info contained in the file.. Is there a way to achieve this? It doesn't need to be extensive at this time, due to the program only being a small project.
Anyway, I'm making the program with Visual Studio Pro 2010, and I'm coding it in C#.
Thank you, for any help in advance.
the standard way to verify that parts of your xml has not been modified is to use XML_Signature
this msdn example shows how this is done with dotnet4
I would embed your XML file as a resource of your console application's assembly. The XML file will exist as an embedded resource and not as a seperate file that the user could potentially change. If the user isn't meant to edit a configuration file, don't even let him see it, modify it, or delete it.
look at this topic decrypt and encrypt
i have created my own Encrypter class based from this classes. then you can create it for yourself for next use
You could simply compress it, if you don't need a high level of security. You could use a standard format (ZIP, CAB), or just deflate the stream and store it as a binary file. See the doc and examples about this here: DeflateStream Class
You can't prevent anyone from editing your xml file but you can encrypt your xml file to protect your data.
I have a table in the database containing "files".
I don't know their filetype.
I need to export some stuff (including these files) and be able to import them into the same application.
So I was thinking about saving the byte array as data.dat (unknown extension).
and when importing just making a byte array from that file and putting it back into the database.
Will this work?
Yes, file extensions are only a clue as to the format/purpose of the file, but don't really mean anything.
From the computer's point of view it doesn't care at all what a file is called (Windows just uses them to associate applications with their files so you can open them by double-clicking).
The extension doesn't influence on the data itself, just the app that will be launched by default when you double-click on it. Yes, it will work.
Just be aware that you should validate the file when importing it to your app.