Make a password Storage in my password manager app.
Im teaching myself how to code with c# and am curious if anyone could help me figure out how i could store passwords (strings) on a form, save them so that they would be there when i reopen the file thank you <3
217 Replies
pen and paper ๐
Thats always the best way
until you lose it
The question is a bit vague, can you clarify?
"store passwords on a form"?
Hm okay, ill try my best im kinda new to this, its a skill im trying to teach myself
I know what a password manager is, are you just trying to do the "I enter a password in my app, it saves, when I restart, it loads" part?
yeah the storage part
i have the generation part working
okay
i got a password that generates from random characters depending on what lengtrh you want, from using a slider
โฏ Method: System.IO.File.WriteAllText(String, String)
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is truncated and overwritten.
โฏ Method: System.IO.File.WriteAllText(String, String, Encoding)
Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is truncated and overwritten.
โฏ Method: System.IO.File.WriteAllTextAsync(String, String, CancellationToken)
Asynchronously creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is truncated and overwritten.
3/4 results shown ~ click here for more results
React with โ to remove this embed.
and a copy button that copys the password to the clipboard
the
File.WriteAllText
method will be of great use for you
that handles the saving. to load, you'd use File.ReadAllText
now, these methods deal with a single string (writing/reading a single string)
so to store more than one password, you'll need to come up with a way to tell them apart.
the act of turning "data" into text is called "serialization", and from text to data is called "deserialization"
you'll want to experiment with reading and writing from/to files, and then also experiment and learn serialization and deserialization
... you still alive? ๐im taking it all in
honestly, I recommend playing around with this separately from your password manager to start with
just make a fresh console app and play around with writing/reading files
since im using forms, i mean where would i even start, like what thingy from the toolbox would i benefit from dragging over
nothing
wah
the toolbox is for UI components
saving to a file isnt a UI thing
but id like to display the passwords on the screen
I get that.
but dont start there.
you'll need to understand writing/reading to files, and most likely also serialization, before you can start worrying about the UI part
I could show you the basics if you are available for screensharing rn
do you have a video? im very um
awkward xD
im sure you can find one on youtube or something, but I don't have one to link you atm :p
you could just disable your mic if you want, not a big deal
you can use a ListBox or a ListView
to display the data or even a DataGrid
but what Pobiega is saying is not based on ui its just plain c#
where u need to read or write to files
when u read from a file u can populate a class into a list that u can display in a ListBox or adequate ui component
so given that he is just suggesting u to use say a console app to practice writen and reading fro ma file
before u apply it to your winforms app
because like he mentioned above u need to come up with a way to identify each username and password from the file
to read it etc
aka a structure
ok ive read some
and made some progress
and can now write to a file
and save it
but
idk how to explain this
if i have three things in my txt file
bob
kevin
stuart
and then i add one more via my application
it overwrites all of them and then i only have 1 entry
how do i add too it rather than overwriting it?
By always overwriting
Store your things in a list, and write the list to a file
this is currently whats stored in my txt file
Right. One entry per line.
when i click load saved
it displays that
right
Sure.
then if i add
it replaces
rather than adds
Show your code for add
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/And what is
bs
here??
oh right
lmao
oh wait i might have gotten rid of password list, unless im now confusing myself
However, I see you have a list of passwords. I would recommend using that
So stick to using PasswordList
Yes.
Adding to the list won't automatically update your listview, so you need to do that yourself
And when you save, save the list, not the listview
how would i go about that?
also when i try to load saved ones
i get this
Yeah.
Because you are mixing up your data and your presentation
The listbox doesn't own the data
It's just a "window" into the data so to speak
right
Where does the data live?
passwords.txt
Well I was thinking about PasswordList :p
oh
That's where it lives in your app
im not sure kekw
No that's the answer
PasswordList is your single source of truth for passwords while inside the app
That's what you add to or remove from etc
Not the view
so this as my add button should work?
Something like that, sure
this is 100% the hardest part so far
gosh
you can use BindingList<string> on winforms, bind it to the DataSource and only manipulate that, it handles updating the UI for u
instead of using List<string>
Nice. That will save you from having to refresh the binding
So what should i replace?
yep, but its also good to know how to do it manually like you learned
Would append add to the list, while add overwrites it?
seen append a few times
if you use it, it would be something like
You set it once to the listBox DataSource
then you only manipulate PassswordList to clear, add or remove
With a list, there is no overwrite.
And don't use file append when writing, too many footguns
then im so confused cause i cant seem too add more entrys to my list
If you use a BindingList as Leo suggested, and change your code appropriately, it should work just fine
ok it works
but
the load button still breaks it
which is annoying
u dont use the listbox
u use the PasswordList
oh
I said it above ๐
no Items
PasswordList is a list
so just PasswordList(Lines);
No
just a small tip
Let visual studio help you
type
PasswordList.
and see what it showsnothing?
Ctrl+space?
nop
That should force intellisense
mmm
Do you not get the suggestion box when writing code?
dont think so
Wtf
Leo, help this poor soul
is this why this has been extremely difficult xD
this is what should have happened
dont mind the tryparse its just a random project I use to make videos
basically it gives you all the things u can do with it
so you can see the Add, Remove etc
and my goal is too load
it
I honestly dont know why he does not get the suggestion box
at the top of your code do u have
weird then should have given u that
Click on a gray one and ask it to remove unused usings
the gray one? theyre all like gray
can you go to Tool > Options
and go to that place
and show what u have checked
2 are white
i got ti!
oop
it
it auto completes now?
noice
Yay
doesnt auto complete
but i have boxes
ehhh I mean suggest box
yeah
That box is the "autocomplete"
The one that shows in Leos code is different
yeah but I think heshe meants the text autocomplete I had one mine
Copilot?
Gh
im broke sir I dont have fancy stuff
ok, so which one of these
on a click of a button
lets me load the list
What do you think?
umm
Neither.
thats why im racking my brain
well it does not have AddRange so u have to loop and Add
sorry I gave that one in because it doesn't have AddRange
You won't fmbe able to solve all problems with a readymade component or method
anyway I will leave u 2 to it ๐
That's why we write our own
I love coding :/
xD
I mean this part is trivial
It has an add method. You have a list of things to add.
Use a loop. Done.
i thought it might have been readlines xD
use a loop sounds a lot harder than you make it
i could have just made something u
up
using the auto thing
for every entry in password list, display it?
it didnt work :c
well yeah what happens if u have nothng in a list and wants to display it?
also Console.WriteLine is for console
u would have to use Debug.WriteLine
so that it at least prints to the Output of visual studio so u can see something
so what would i use to print to the display box thingy
nothing when u Add to the list it updates the listbox automagically
1. You have a
BindingList<string> PasswordList
.
2. You have a ListBox listBox1
.
3. You set the DataSource
property on listBox1
to be PasswordList
.
4. These are now linked. If you add something to PasswordList
, it will show up in your listbox.๐
ive got that
oh do you mean this?
that doesn't really matter you're doing it from code already
and where it the code u populate that list
i have three lines in the text document
but when i open the app fresh and click load saved they dont go in
show the code for your "load" method
well.. uh... yeah?
oh i thought that didnt matter if i did all of the above xD
ok u have an array of string
what do you think that line does
no u still need to add items to the list
the binding list thing is just so that it automatically updates the userinterface
it doesnt involve your file at all
ok so i still need to load the items to the listbox
no
load the items to your list
right
thats what i meant
listbox is the UI component
PasswordList is your list
that is bound to the listbox
so which do u update
the list
oo it auto put this
and u understand what that does? and what u have to do with it?
absolutely not
then I suggest you check these videos first, there is one for array and 2 for loops https://learn.microsoft.com/en-us/shows/csharp-fundamentals-for-absolute-beginners/ so u understand the basics of loops, etc
winforms is nice because u can build something without even using code, until u realize to power it u need to write code that makes it function behind
always study your fundamentals
variables, classes, loops, control flow. the absolute core pillars
im still lost
its probably a couple easy asf lines
they are easy lines if u gone thru the basics and learned about types, loops, arrays, they are not simple otherwise.
i stil cant figure it out, can someone tell me what it is and why? i dont think its clicking.
do you understand what the
string[] lines = File.ReadAllLines(...)
line of code does?It reads all the lines of strings in the txt document?
thats the right side of the
=
sign yes
but what does the program do with it after that?
you alive? :psorry, i got caught up with something
Like what should it do?
no, like what it does
nope then :c
I mean u have to make some effort really... do u understand what arrays are?
A list
right?
if i remember
i also have put effort i, ive been reading around the topic since yesterday when this issue first came up and i cant seem to get the actual answer, so i can understand what the point is.
the answer is understand how array and loop works in c#
u need to use the loop to read the array
there are different types of loops
inside the loop u get 1 item
and then u can do something with that item
and it will then move to the next item
u have for, while, foreach loops
if you focus on the "load" method, the problem you're having is that you read the data into an array, but then you just leave it there and it goes away. What must you do with it?
save it?
Well we just loaded it
But we loaded it to a temporary array.. where can we move it to?
the list?
PasswordList
yes!
i understand that, but idk how i would go about typing that :c
well, what methods does PasswordList have that can be used to add things to it?
im not sure
Well have a look then
But look at what
type PasswordList. then look what comes up?
Sure
i would say add, but i think thats too simple
Or check the documentation for the type
Add does what we want
just add?
Sure.
what does this mean?
What part?
all of the above
Well the top part is the method signature
Then comes a description of the method
Then comes an error, because you don't have a complete valid statement
so what would go after
So im adding to the list
password list
how do i state i want it to be from the array
You don't.
You a loop to run .Add once for each item in the array
then what goes after the PasswordList.Add
I'm not gonna write it for youm
im really stumped, i understand that youre helping but im generally clueless this is my first time doing this, sometimes its better to just tell me so i know what it looks like and explain why it looks like that, rather than me sat here guessing one of 1 million different ways i could do it
cause im generally lost
i havent been all this lost up til this point
Honestly, go do the hello world tutorial again
You have missed the fundamentals
You don't know loops, or calling methods, and don't understand the type system
$helloworld
we aren't trying to be rude or anything with you, we want to help u learn, not to feed you code that u will use, not learn and keep asking for more code until u complete your app.
that's why I suggest u the fundamentals way back if u read back
i totally understand that
it shows how to use arrays, how to interact with it, how to use loops, what are the different data types etc
Thats why i had been going along the entire time, doing what you said, i did all this yesterday thats why i was offline for so long
i was doing everything you asked.
part of problem solving is understanding the language so u know what u can use to solve your problems
I mean u didn't check the videos I sent u, if u had u would know what arrays are, how to interact with it.
anyway check the links above
No, not everyone learns the same way from a video
open a console project
and try to practice it to understand what it does
indeed they dont, the video is there showing how different things work, practicing your self, trying to understand and asking question is on your side
if videos are not good for u then book, text... just say it
like i enjoy talking to you, and i have learnt from both of you
but at times, just saying sure isnt giving me any infomation? like is that right? wrong? do i need to do it again? change it?
so here is the thing, we did told u... but apparently u didnt read or tried to look it up
leowest
the answer is understand how array and loop works in c#
Quoted by
<@1102729783969861782> from #Make a password Storage in my password manager app. (click here)
React with โ to remove this embed.
Don't know how many more times we can say, use a loop :p
It should be a 5 second Google and a 10 minute read
and a few tries in a console app
Yep
seemingly i cant find anything useful, link me something that should work in 10 minutes, and ill never bother you again
Iteration statements -for, foreach, do, and while - C#
C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.
ive made progress
ive got the data from the array to display in a messagebox to confirm that it was working
But have you added it from that array to your password list?
I did it
Congratulations
And it makes sense to me now
For each string / line, in PasswordList List it loads it