C
C#3mo ago
sophisnthere

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
Cubic3mo ago
pen and paper 👍
sophisnthere
sophisnthere3mo ago
Thats always the best way until you lose it
Pobiega
Pobiega3mo ago
The question is a bit vague, can you clarify? "store passwords on a form"?
sophisnthere
sophisnthere3mo ago
Hm okay, ill try my best im kinda new to this, its a skill im trying to teach myself
Pobiega
Pobiega3mo 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?
sophisnthere
sophisnthere3mo ago
yeah the storage part i have the generation part working
Pobiega
Pobiega3mo ago
okay
sophisnthere
sophisnthere3mo ago
i got a password that generates from random characters depending on what lengtrh you want, from using a slider
MODiX
MODiX3mo 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.
sophisnthere
sophisnthere3mo ago
and a copy button that copys the password to the clipboard
Pobiega
Pobiega3mo 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? 😄
sophisnthere
sophisnthere3mo ago
im taking it all in
Pobiega
Pobiega3mo 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
sophisnthere
sophisnthere3mo ago
since im using forms, i mean where would i even start, like what thingy from the toolbox would i benefit from dragging over
Pobiega
Pobiega3mo ago
nothing
sophisnthere
sophisnthere3mo ago
wah
Pobiega
Pobiega3mo ago
the toolbox is for UI components saving to a file isnt a UI thing
sophisnthere
sophisnthere3mo ago
but id like to display the passwords on the screen
Pobiega
Pobiega3mo 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
sophisnthere
sophisnthere3mo ago
do you have a video? im very um awkward xD
Pobiega
Pobiega3mo 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
leowest3mo 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
sophisnthere
sophisnthere3mo ago
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
Pobiega3mo ago
By always overwriting Store your things in a list, and write the list to a file
sophisnthere
sophisnthere3mo ago
this is currently whats stored in my txt file
No description
Pobiega
Pobiega3mo ago
Right. One entry per line.
sophisnthere
sophisnthere3mo ago
when i click load saved
No description
sophisnthere
sophisnthere3mo ago
it displays that right
Pobiega
Pobiega3mo ago
Sure.
sophisnthere
sophisnthere3mo ago
then if i add
sophisnthere
sophisnthere3mo ago
No description
sophisnthere
sophisnthere3mo ago
it replaces rather than adds
Pobiega
Pobiega3mo ago
Show your code for add $code
MODiX
MODiX3mo 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/
sophisnthere
sophisnthere3mo ago
// 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
Pobiega3mo ago
And what is bs here?
sophisnthere
sophisnthere3mo ago
No description
Pobiega
Pobiega3mo ago
?
sophisnthere
sophisnthere3mo ago
oh right lmao oh wait i might have gotten rid of password list, unless im now confusing myself
Pobiega
Pobiega3mo ago
However, I see you have a list of passwords. I would recommend using that
sophisnthere
sophisnthere3mo ago
So stick to using PasswordList
Pobiega
Pobiega3mo ago
Yes.
sophisnthere
sophisnthere3mo ago
No description
Pobiega
Pobiega3mo 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
sophisnthere
sophisnthere3mo ago
how would i go about that? also when i try to load saved ones
sophisnthere
sophisnthere3mo ago
i get this
No description
Pobiega
Pobiega3mo 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
sophisnthere
sophisnthere3mo ago
right
Pobiega
Pobiega3mo ago
Where does the data live?
sophisnthere
sophisnthere3mo ago
passwords.txt
Pobiega
Pobiega3mo ago
Well I was thinking about PasswordList :p
sophisnthere
sophisnthere3mo ago
oh
Pobiega
Pobiega3mo ago
That's where it lives in your app
sophisnthere
sophisnthere3mo ago
im not sure kekw
Pobiega
Pobiega3mo 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
sophisnthere
sophisnthere3mo ago
No description
sophisnthere
sophisnthere3mo ago
so this as my add button should work?
Pobiega
Pobiega3mo ago
Something like that, sure
sophisnthere
sophisnthere3mo ago
this is 100% the hardest part so far gosh
leowest
leowest3mo 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
Pobiega3mo ago
Nice. That will save you from having to refresh the binding
sophisnthere
sophisnthere3mo ago
So what should i replace?
leowest
leowest3mo ago
yep, but its also good to know how to do it manually like you learned
sophisnthere
sophisnthere3mo ago
Would append add to the list, while add overwrites it? seen append a few times
leowest
leowest3mo 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
Pobiega3mo ago
With a list, there is no overwrite. And don't use file append when writing, too many footguns
sophisnthere
sophisnthere3mo ago
then im so confused cause i cant seem too add more entrys to my list
Pobiega
Pobiega3mo ago
If you use a BindingList as Leo suggested, and change your code appropriately, it should work just fine
sophisnthere
sophisnthere3mo ago
ok it works but the load button still breaks it
sophisnthere
sophisnthere3mo ago
No description
sophisnthere
sophisnthere3mo ago
which is annoying
leowest
leowest3mo ago
u dont use the listbox u use the PasswordList
sophisnthere
sophisnthere3mo ago
oh
leowest
leowest3mo ago
No description
leowest
leowest3mo ago
I said it above 😉
sophisnthere
sophisnthere3mo ago
No description
leowest
leowest3mo ago
no Items PasswordList is a list
sophisnthere
sophisnthere3mo ago
so just PasswordList(Lines);
Pobiega
Pobiega3mo ago
No
leowest
leowest3mo ago
just a small tip
Pobiega
Pobiega3mo ago
Let visual studio help you
leowest
leowest3mo ago
type PasswordList. and see what it shows
sophisnthere
sophisnthere3mo ago
nothing?
sophisnthere
sophisnthere3mo ago
No description
Pobiega
Pobiega3mo ago
Ctrl+space?
sophisnthere
sophisnthere3mo ago
nop
Pobiega
Pobiega3mo ago
That should force intellisense
leowest
leowest3mo ago
mmm
Pobiega
Pobiega3mo ago
Do you not get the suggestion box when writing code?
sophisnthere
sophisnthere3mo ago
dont think so
Pobiega
Pobiega3mo ago
Wtf Leo, help this poor soul
sophisnthere
sophisnthere3mo ago
is this why this has been extremely difficult xD
leowest
leowest3mo ago
leowest
leowest3mo 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
sophisnthere
sophisnthere3mo ago
and my goal is too load it
leowest
leowest3mo 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;
sophisnthere
sophisnthere3mo ago
No description
leowest
leowest3mo ago
weird then should have given u that
Pobiega
Pobiega3mo ago
Click on a gray one and ask it to remove unused usings
sophisnthere
sophisnthere3mo ago
the gray one? theyre all like gray
leowest
leowest3mo ago
can you go to Tool > Options
No description
leowest
leowest3mo ago
and go to that place and show what u have checked 2 are white
sophisnthere
sophisnthere3mo ago
i got ti! oop it
leowest
leowest3mo ago
it auto completes now?
sophisnthere
sophisnthere3mo ago
No description
leowest
leowest3mo ago
noice
Pobiega
Pobiega3mo ago
Yay
sophisnthere
sophisnthere3mo ago
doesnt auto complete but i have boxes
leowest
leowest3mo ago
ehhh I mean suggest box yeah
Pobiega
Pobiega3mo ago
That box is the "autocomplete" The one that shows in Leos code is different
leowest
leowest3mo ago
yeah but I think heshe meants the text autocomplete I had one mine
Pobiega
Pobiega3mo ago
Copilot? Gh
leowest
leowest3mo ago
im broke sir I dont have fancy stuff
sophisnthere
sophisnthere3mo ago
ok, so which one of these on a click of a button lets me load the list
Pobiega
Pobiega3mo ago
What do you think?
sophisnthere
sophisnthere3mo ago
umm
Pobiega
Pobiega3mo ago
Neither.
sophisnthere
sophisnthere3mo ago
thats why im racking my brain
leowest
leowest3mo 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
Pobiega3mo ago
You won't fmbe able to solve all problems with a readymade component or method
leowest
leowest3mo ago
anyway I will leave u 2 to it 😉
Pobiega
Pobiega3mo ago
That's why we write our own
sophisnthere
sophisnthere3mo ago
I love coding :/ xD
Pobiega
Pobiega3mo ago
I mean this part is trivial It has an add method. You have a list of things to add. Use a loop. Done.
sophisnthere
sophisnthere3mo ago
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
sophisnthere
sophisnthere3mo ago
No description
sophisnthere
sophisnthere3mo ago
for every entry in password list, display it? it didnt work :c
leowest
leowest3mo 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
sophisnthere
sophisnthere3mo ago
so what would i use to print to the display box thingy
leowest
leowest3mo ago
nothing when u Add to the list it updates the listbox automagically
Pobiega
Pobiega3mo 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
leowest3mo ago
👆
sophisnthere
sophisnthere3mo ago
ive got that
sophisnthere
sophisnthere3mo ago
No description
sophisnthere
sophisnthere3mo ago
No description
sophisnthere
sophisnthere3mo ago
oh do you mean this?
No description
leowest
leowest3mo ago
that doesn't really matter you're doing it from code already and where it the code u populate that list
sophisnthere
sophisnthere3mo ago
i have three lines in the text document but when i open the app fresh and click load saved they dont go in
Pobiega
Pobiega3mo ago
show the code for your "load" method
sophisnthere
sophisnthere3mo ago
No description
Pobiega
Pobiega3mo ago
well.. uh... yeah?
sophisnthere
sophisnthere3mo ago
oh i thought that didnt matter if i did all of the above xD
leowest
leowest3mo ago
ok u have an array of string
Pobiega
Pobiega3mo ago
what do you think that line does
leowest
leowest3mo ago
no u still need to add items to the list
Pobiega
Pobiega3mo ago
the binding list thing is just so that it automatically updates the userinterface it doesnt involve your file at all
sophisnthere
sophisnthere3mo ago
ok so i still need to load the items to the listbox
Pobiega
Pobiega3mo ago
no load the items to your list
sophisnthere
sophisnthere3mo ago
right thats what i meant
leowest
leowest3mo ago
listbox is the UI component PasswordList is your list that is bound to the listbox so which do u update
sophisnthere
sophisnthere3mo ago
the list
sophisnthere
sophisnthere3mo ago
oo it auto put this
No description
leowest
leowest3mo ago
and u understand what that does? and what u have to do with it?
sophisnthere
sophisnthere3mo ago
absolutely not
leowest
leowest3mo 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
Pobiega3mo ago
always study your fundamentals variables, classes, loops, control flow. the absolute core pillars
sophisnthere
sophisnthere3mo ago
im still lost its probably a couple easy asf lines
leowest
leowest3mo ago
they are easy lines if u gone thru the basics and learned about types, loops, arrays, they are not simple otherwise.
sophisnthere
sophisnthere3mo ago
i stil cant figure it out, can someone tell me what it is and why? i dont think its clicking.
Pobiega
Pobiega3mo ago
do you understand what the string[] lines = File.ReadAllLines(...) line of code does?
sophisnthere
sophisnthere3mo ago
It reads all the lines of strings in the txt document?
Pobiega
Pobiega3mo ago
thats the right side of the = sign yes but what does the program do with it after that? you alive? :p
sophisnthere
sophisnthere3mo ago
sorry, i got caught up with something Like what should it do?
Pobiega
Pobiega3mo ago
no, like what it does
sophisnthere
sophisnthere3mo ago
nope then :c
leowest
leowest3mo ago
I mean u have to make some effort really... do u understand what arrays are?
sophisnthere
sophisnthere3mo ago
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
leowest3mo 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
Pobiega3mo 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?
sophisnthere
sophisnthere3mo ago
save it?
Pobiega
Pobiega3mo ago
Well we just loaded it But we loaded it to a temporary array.. where can we move it to?
sophisnthere
sophisnthere3mo ago
the list? PasswordList
Pobiega
Pobiega3mo ago
yes!
sophisnthere
sophisnthere3mo ago
i understand that, but idk how i would go about typing that :c
Pobiega
Pobiega3mo ago
well, what methods does PasswordList have that can be used to add things to it?
sophisnthere
sophisnthere3mo ago
im not sure
Pobiega
Pobiega3mo ago
Well have a look then
sophisnthere
sophisnthere3mo ago
But look at what type PasswordList. then look what comes up?
Pobiega
Pobiega3mo ago
Sure
sophisnthere
sophisnthere3mo ago
No description
sophisnthere
sophisnthere3mo ago
i would say add, but i think thats too simple
Pobiega
Pobiega3mo ago
Or check the documentation for the type Add does what we want
sophisnthere
sophisnthere3mo ago
just add?
No description
Pobiega
Pobiega3mo ago
Sure.
sophisnthere
sophisnthere3mo ago
what does this mean?
No description
Pobiega
Pobiega3mo ago
What part?
sophisnthere
sophisnthere3mo ago
all of the above
Pobiega
Pobiega3mo 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
sophisnthere
sophisnthere3mo ago
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
Pobiega3mo ago
You don't. You a loop to run .Add once for each item in the array
sophisnthere
sophisnthere3mo ago
then what goes after the PasswordList.Add
Pobiega
Pobiega3mo ago
I'm not gonna write it for youm
sophisnthere
sophisnthere3mo ago
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
Pobiega3mo 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
leowest3mo 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
sophisnthere
sophisnthere3mo ago
i totally understand that
leowest
leowest3mo ago
it shows how to use arrays, how to interact with it, how to use loops, what are the different data types etc
sophisnthere
sophisnthere3mo ago
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
leowest3mo 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
sophisnthere
sophisnthere3mo ago
No, not everyone learns the same way from a video
leowest
leowest3mo 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
sophisnthere
sophisnthere3mo ago
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
leowest3mo ago
so here is the thing, we did told u... but apparently u didnt read or tried to look it up
MODiX
MODiX3mo ago
leowest
the answer is understand how array and loop works in c#
React with ❌ to remove this embed.
Pobiega
Pobiega3mo 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
leowest3mo ago
and a few tries in a console app
Pobiega
Pobiega3mo ago
Yep
sophisnthere
sophisnthere3mo ago
seemingly i cant find anything useful, link me something that should work in 10 minutes, and ill never bother you again
Pobiega
Pobiega3mo 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.
sophisnthere
sophisnthere3mo ago
ive made progress ive got the data from the array to display in a messagebox to confirm that it was working
Pobiega
Pobiega3mo ago
But have you added it from that array to your password list?
sophisnthere
sophisnthere3mo ago
I did it
sophisnthere
sophisnthere3mo ago
No description
Pobiega
Pobiega3mo ago
Congratulations
sophisnthere
sophisnthere3mo ago
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
More Posts