Replacing a certain word in a txt file - c#

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.

Related

How to create a resource (text file) from c# code? Or can we edit an existing resource file?

I have built a web application with vacations period planning (didactic purpose).
I need to import a text file with bank holidays once a year (the file will be uploaded in UI).
(ex: localPath/BankHolidays.txt - contains: 1.01.2015,31.12.2015,...).
I thought it would be a good approach to save the given text file as a Resource file instead of saving it somewhere else (in ProjectDir) as a normal .txt file. Because I need to access this file quite often and I don't think it's necessary to save this information in a db...
Is this a really good approach in my case?
If it is, how can I save a file in Resources dynamically from my code?
Have a look at the resourceWriter class on the MSDN:
http://msdn.microsoft.com/en-us/library/system.resources.resourcewriter.aspx
This class will perform all of the heavy lifting in terms of file I/O and data formatting. There is also a great tutorial on how to actually use it at:
http://www.c-sharpcorner.com/UploadFile/yougerthen/105232008045338AM/1.aspx

Where does file information (like DateCreated) get stored when you create a new file?

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.

save items in a listbox

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.

Save two TextRanges together with some strings into one file, and then load this later, whats the best/easiest approach?

I have two TextRanges from two different RichTextBoxes, and four strings from regular textboxes. I would like to save all this information in one file, and then be able to load it later. Whats the best approach?
I've been reading some about it, and it seems that reading all into one memorystream and then save it to a file is one way to do it. And then parse this content later.
Anyone that want to share some experience, and simple code?
For a simple approach consider creating a class with string properties for each of your textbox texts. You could then set the properties when you want to save your text, use XML serialization to save the class to an XML formatted file, and then read it back at a later time.
The advantage of this approach is that you will not need to hande low-level file handling or parsing yourself.
Searching for C# and XML Serialization will yield plenty of code examples.
One solution is already provided by you: save in file and read after.
Another could be, in case if the data is too big, is using http://msdn.microsoft.com/en-us/library/dd997372.aspx Memory Mapped Fies.

Protecting a Xml file

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.

Categories

Resources