In my C# windows application (using visual C# 2012 if you need to know) I need to add things to listboxes and save the items so when the program closes they stay there next time it opens. I would prefer not using the settings to store the information.
Some more things I need for this. Saving them to a text file can't happen either. It needs to be saved in a way that it can not be edited outside of the program.
Sorry but its not possible. Anything that you save can be modified by another program.
You could sign the saved data and then detect that some other has modified the data, but you cannot prevent other programs from read/changing the data.
EDIT
DPAPI can be used to encrypt/decrypt the data. See Really simple encryption with C# and SymmetricAlgorithm
Set up SQL on a web server.
Save the info to your web server.
They can't modify data on a remote server, so I think a database is the way to go.
If you are the only person using that program... I mean... if you are the only one that knows what the program is doing, you could go for saving data in binary format into binary files. I doubt that someone looking at your files will say "Oh damn there is a .bin file in this directory! I have to find a way to discover how to read it and what's it's content!".
As long as you keep your program and its sources protected... I doubt someone will get something out of that content. You transform all your data into a Byte[], you compress it, you encrypt it and you write in a file. Bye bye.
Related
I have a program in c# that downloads images from a web service.
The download usually takes time so I want to save the images locally so I would only need to download each image once. The problem with that is when the images saves the user of the program can see the image in the files and change it.
Is there a way to save the image in the program, yet keep it from users to see and change in the folder?
EDIT: solution used:
Encrypting the images and their names when I save them, and only access them this way. (decrypting when after reading them).
What is your intent? Anything your program has access to do, your user does as well. If you're just trying to prevent people from accidentally mucking with your images, then save off a SHA1 or similar hash of the file and store it separately. When you need an image, check the SHA1 and redownload if it doesn't match. This will prevent casual tampering, but still isn't 100% effective against malicious changes.
Following requirements we have users, their passwords and their profile picture stored in a .txt file. A user can be redirected to a profile modification page where they can change their password and/or password picture.
I need to replace the password and/or profile picture name in the .txt file when the user clicks OK.
The users.txt file is laid out like:
mike mike Avatar1
jessica123 123jessica Avatar4
mohd MoHd Avatar3
xiao AxiaoA Avatar2
anna abcANNAabc Avatar1
After doing some research i've tried using:
userFilePath = userFilePath.Replace(Session["password"].ToString(),
passwordTextBox.Text);
The original password is stored on log-in, through the session variable which I was using to find the original text in the file.
But this doesn't work. I'm pretty new to c#. Any help would be appreciated.
Be careful you have to change only the line concerned, many users could have the same passeword, so you have to read the file by line , identify the login concerned and of course not replace anything because also the login or avatar could has the same value as the password, but recreate the line with the new info, and after save the file with the new whole text value.
And just an advice for such needs it's better to use relational database.
Other than storing information in plain text files and saving passwords in clear text which others already mentioned as a bad idea, I think it is best to void direct string modification on the file level. It is best to abstract this away in a (list of) object and the modify the object and save it back to disk. You can easily do this using a library named FileHelpers, if you still want to use plain text files, without reinventing the wheel.
Also thing to consider, if you insist keeping this on a text file, is to switch to a better structured text file like JSON or XML format. You wouldn't need to use the above mentioned library anymore as .NET already comes with de/serializers for both.
If at all possible you should take the advice of the comments on your question and not do this in this manner.
If for some reason that is not possible, then here is a place to get started. You want to load the entire file from the file system into memory during a load phase, generally when your application starts, or first has need of the data.
Once the data is in some structure in memory you can alter like you would alter any object in C#.
Once the changes to the memory are done, you would write the entire thing back out to the file, overwriting the old file.
So your question is a very large one, there is the question of how do you read data from a file? How do you create a data structure in memory to hold this particular set of data? And how do you write the data back out to a file? I'd suggest working on one at a time in that order.
Oh and if you can't have a database, but you can change the file format, switch it to XML or JSON and the process of taking it from a file to memory, and back to a file will be much easier.
I have a task at hand, read a database that was created with HTML-OS, i assume the format is DB4 or DB5. the task is to open and parse this database in a c# application...it can be ASP.NET or WinForms...bottom line is i need to extract this indexed data. below is a small sample of what it looks like when i open the file with notepad:
dbtype 3.046 = 0 T ¨j
I have some ideas on parsing with possibly using RegEx but i would like to see some ideas or a real way of reading this file would be even better!
Thank you in advance!
It looks like the database format is partially defined on their site:
The HTML/OS Database (PDF)
If possible I'd look at using their HTML/OS language to export it, rather than trying to reverse-engineer the database format.
You could make an htmlos page that would export the data from the database and store this in a csv for example. Then pick up the file from the other machine, after which you could execute something else to remove the csv file.
To answer devHead's question.. yes, HTML/OS accepts http posts.
(Just FYI: If you have any questions, feel free to ask.. I program in html/os.)
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've seen many article on encrypt/decrypt of file and typically a button is used to choose the file for encrypt and another button to decrypt the file.
i've seen some application like truecrypt and probably others which does file encryption on-the-fly with transparent. this means that when a encrypted file is clicked to access, it will automatically decrypt and play/open the file. then when the file is closed, it will automatically encrypt again.
some have said that the only way to detect file open is through file system filter.
but is there other ways to do this in c# compact framework?
You could give all the encrypted files a specific file extension which is handled by your decryption program, then when the user opens the file, your program would decrypt it and then open it in the correct application.
I think I understand your question. You don't control the programs which display the files, you want to just control the encryption anddecryption of the files behind the scenes without the program knowing it even happened (like a .docx file, would be encrypted and decrypted without Word knowing it). I think you should look into WMI. I haven't specifically used it to check for system events having to do with files being retrieved or saved but I have used it to hook into system events and set off code at certain times very effectively. Here is the MSDN reference:
WMI Reference
I would look into the __Instance Events. Like the __InstanceModificationEvent class for file save events to the IO:
__InstanceModificationEvent Reference
Hope this get you onto the right track!
I answered this in the MS Foums. You have to write a file system filter and that filter can only be written in C.