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
Cubic
Cubicโ€ข8mo ago
pen and paper ๐Ÿ‘
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
Thats always the best way until you lose it
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
okay
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
i got a password that generates from random characters depending on what lengtrh you want, from using a slider
MODiX
MODiXโ€ข8mo ago
โฏ 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
Pobiega
Pobiegaโ€ข8mo ago
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? ๐Ÿ˜„
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
nothing
Pobiega
Pobiegaโ€ข8mo ago
the toolbox is for UI components saving to a file isnt a UI thing
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
but id like to display the passwords on the screen
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
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?
Pobiega
Pobiegaโ€ข8mo ago
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
No description
Pobiega
Pobiegaโ€ข8mo ago
Right. One entry per line.
Pobiega
Pobiegaโ€ข8mo ago
Sure.
Pobiega
Pobiegaโ€ข8mo ago
Show your code for add $code
MODiX
MODiXโ€ข8mo ago
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/
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
// PasswordList.Add(txt_newPassword.Text);
listBox1.DataSource = bs;
bs.ResetBindings(false);
label1.Text = "There are " + PasswordList.Count + " Passwords in the list.";
// PasswordList.Add(txt_newPassword.Text);
listBox1.DataSource = bs;
bs.ResetBindings(false);
label1.Text = "There are " + PasswordList.Count + " Passwords in the list.";
Pobiega
Pobiegaโ€ข8mo ago
And what is bs here?
Pobiega
Pobiegaโ€ข8mo ago
?
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
oh right lmao oh wait i might have gotten rid of password list, unless im now confusing myself
Pobiega
Pobiegaโ€ข8mo ago
However, I see you have a list of passwords. I would recommend using that
Pobiega
Pobiegaโ€ข8mo ago
Yes.
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
Where does the data live?
Pobiega
Pobiegaโ€ข8mo ago
Well I was thinking about PasswordList :p
Pobiega
Pobiegaโ€ข8mo ago
That's where it lives in your app
Pobiega
Pobiegaโ€ข8mo ago
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?
Pobiega
Pobiegaโ€ข8mo ago
Something like that, sure
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
this is 100% the hardest part so far gosh
leowest
leowestโ€ข8mo ago
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>
Pobiega
Pobiegaโ€ข8mo ago
Nice. That will save you from having to refresh the binding
leowest
leowestโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
if you use it, it would be something like
private BindingList<string> PasswordList = new BindingList<string>();
public Form1()
{
InitializeComponent();
listBox1.DataSource = PasswordList;
}
private BindingList<string> PasswordList = new BindingList<string>();
public Form1()
{
InitializeComponent();
listBox1.DataSource = PasswordList;
}
You set it once to the listBox DataSource then you only manipulate PassswordList to clear, add or remove
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
u dont use the listbox u use the PasswordList
leowest
leowestโ€ข8mo ago
No description
leowest
leowestโ€ข8mo ago
I said it above ๐Ÿ˜‰
leowest
leowestโ€ข8mo ago
no Items PasswordList is a list
Pobiega
Pobiegaโ€ข8mo ago
No
leowest
leowestโ€ข8mo ago
just a small tip
Pobiega
Pobiegaโ€ข8mo ago
Let visual studio help you
leowest
leowestโ€ข8mo ago
type PasswordList. and see what it shows
Pobiega
Pobiegaโ€ข8mo ago
Ctrl+space?
Pobiega
Pobiegaโ€ข8mo ago
That should force intellisense
leowest
leowestโ€ข8mo ago
mmm
Pobiega
Pobiegaโ€ข8mo ago
Do you not get the suggestion box when writing code?
Pobiega
Pobiegaโ€ข8mo ago
Wtf Leo, help this poor soul
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
is this why this has been extremely difficult xD
leowest
leowestโ€ข8mo ago
leowest
leowestโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
I honestly dont know why he does not get the suggestion box at the top of your code do u have
using System.ComponentModel;
using System.ComponentModel;
leowest
leowestโ€ข8mo ago
weird then should have given u that
Pobiega
Pobiegaโ€ข8mo ago
Click on a gray one and ask it to remove unused usings
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
the gray one? theyre all like gray
leowest
leowestโ€ข8mo ago
can you go to Tool > Options
No description
leowest
leowestโ€ข8mo ago
and go to that place and show what u have checked 2 are white
leowest
leowestโ€ข8mo ago
it auto completes now?
leowest
leowestโ€ข8mo ago
noice
Pobiega
Pobiegaโ€ข8mo ago
Yay
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
doesnt auto complete but i have boxes
leowest
leowestโ€ข8mo ago
ehhh I mean suggest box yeah
Pobiega
Pobiegaโ€ข8mo ago
That box is the "autocomplete" The one that shows in Leos code is different
leowest
leowestโ€ข8mo ago
yeah but I think heshe meants the text autocomplete I had one mine
Pobiega
Pobiegaโ€ข8mo ago
Copilot? Gh
leowest
leowestโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
What do you think?
Pobiega
Pobiegaโ€ข8mo ago
Neither.
leowest
leowestโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
You won't fmbe able to solve all problems with a readymade component or method
leowest
leowestโ€ข8mo ago
anyway I will leave u 2 to it ๐Ÿ˜‰
Pobiega
Pobiegaโ€ข8mo ago
That's why we write our own
Pobiega
Pobiegaโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
nothing when u Add to the list it updates the listbox automagically
Pobiega
Pobiegaโ€ข8mo ago
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.
leowest
leowestโ€ข8mo ago
๐Ÿ‘†
leowest
leowestโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
show the code for your "load" method
Pobiega
Pobiegaโ€ข8mo ago
well.. uh... yeah?
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
oh i thought that didnt matter if i did all of the above xD
leowest
leowestโ€ข8mo ago
ok u have an array of string
Pobiega
Pobiegaโ€ข8mo ago
what do you think that line does
leowest
leowestโ€ข8mo ago
no u still need to add items to the list
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
no load the items to your list
leowest
leowestโ€ข8mo ago
listbox is the UI component PasswordList is your list that is bound to the listbox so which do u update
leowest
leowestโ€ข8mo ago
and u understand what that does? and what u have to do with it?
leowest
leowestโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
always study your fundamentals variables, classes, loops, control flow. the absolute core pillars
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
im still lost its probably a couple easy asf lines
leowest
leowestโ€ข8mo ago
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.
Pobiega
Pobiegaโ€ข8mo ago
do you understand what the string[] lines = File.ReadAllLines(...) line of code does?
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
It reads all the lines of strings in the txt document?
Pobiega
Pobiegaโ€ข8mo ago
thats the right side of the = sign yes but what does the program do with it after that? you alive? :p
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
sorry, i got caught up with something Like what should it do?
Pobiega
Pobiegaโ€ข8mo ago
no, like what it does
leowest
leowestโ€ข8mo ago
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.
leowest
leowestโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
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?
Pobiega
Pobiegaโ€ข8mo ago
Well we just loaded it But we loaded it to a temporary array.. where can we move it to?
Pobiega
Pobiegaโ€ข8mo ago
yes!
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
i understand that, but idk how i would go about typing that :c
Pobiega
Pobiegaโ€ข8mo ago
well, what methods does PasswordList have that can be used to add things to it?
Pobiega
Pobiegaโ€ข8mo ago
Well have a look then
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
But look at what type PasswordList. then look what comes up?
Pobiega
Pobiegaโ€ข8mo ago
Sure
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
i would say add, but i think thats too simple
Pobiega
Pobiegaโ€ข8mo ago
Or check the documentation for the type Add does what we want
Pobiega
Pobiegaโ€ข8mo ago
Sure.
Pobiega
Pobiegaโ€ข8mo ago
What part?
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
You don't. You a loop to run .Add once for each item in the array
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
then what goes after the PasswordList.Add
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
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.
leowest
leowestโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
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?
leowest
leowestโ€ข8mo ago
so here is the thing, we did told u... but apparently u didnt read or tried to look it up
MODiX
MODiXโ€ข8mo ago
leowest
the answer is understand how array and loop works in c#
React with โŒ to remove this embed.
Pobiega
Pobiegaโ€ข8mo ago
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
leowest
leowestโ€ข8mo ago
and a few tries in a console app
Pobiega
Pobiegaโ€ข8mo ago
Yep
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
seemingly i cant find anything useful, link me something that should work in 10 minutes, and ill never bother you again
Pobiega
Pobiegaโ€ข8mo ago
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
Pobiega
Pobiegaโ€ข8mo ago
But have you added it from that array to your password list?
Pobiega
Pobiegaโ€ข8mo ago
Congratulations
๐šœ๐šŠ๐š”๐šž๐š›๐šŠ แกฃ๐ญฉ
And it makes sense to me now For each string / line, in PasswordList List it loads it
Want results from more Discord servers?
Add your server