C
C#ā€¢7mo ago
Nadekai

I am lost

For some reason whenever I press enter, nothing happens, the MK_content doesn't get written into the file.
c#
using System;
using System.Collections.Generic;
using System.IO;


namespace Password_Manager
{

public partial class MainWindow : Window
{
public bool MK_exists = false;
public string MK_content = null;
public string MK_path = "Data\\MasterKey.txt";
public MainWindow()
{
InitializeComponent();

// Accessing variables




if (File.Exists(MK_path))
{
// Check if MasterKey.txt exists and has content
if (new FileInfo(MK_path).Length > 0)
{
MK_exists = true;
MK_content = File.ReadAllText(MK_path); // Read the content of MasterKey.txt
}
}
else
{
// Create MasterKey.txt if it doesn't exist
using (File.Create(MK_path)) { }
}

// Show the appropriate stack panel based on MK existence
if (MK_exists)
{
sp_MK_Login.Visibility = Visibility.Visible;
}
else
{
sp_MK_creation.Visibility = Visibility.Visible;
}

// Set the content of the password box if MK_content is not null or empty


debug_label.Content = MK_content;

tb_MK_PasswordBox_Creation.KeyDown += MK_PB_Creation_KeyDown;
}

private void MK_PB_Creation_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
if (!string.IsNullOrEmpty(MK_content))
{

tb_MK_PasswordBox_Creation.Password = MK_content;
File.WriteAllText(MK_path, MK_content);
}
}
}
}
}
c#
using System;
using System.Collections.Generic;
using System.IO;


namespace Password_Manager
{

public partial class MainWindow : Window
{
public bool MK_exists = false;
public string MK_content = null;
public string MK_path = "Data\\MasterKey.txt";
public MainWindow()
{
InitializeComponent();

// Accessing variables




if (File.Exists(MK_path))
{
// Check if MasterKey.txt exists and has content
if (new FileInfo(MK_path).Length > 0)
{
MK_exists = true;
MK_content = File.ReadAllText(MK_path); // Read the content of MasterKey.txt
}
}
else
{
// Create MasterKey.txt if it doesn't exist
using (File.Create(MK_path)) { }
}

// Show the appropriate stack panel based on MK existence
if (MK_exists)
{
sp_MK_Login.Visibility = Visibility.Visible;
}
else
{
sp_MK_creation.Visibility = Visibility.Visible;
}

// Set the content of the password box if MK_content is not null or empty


debug_label.Content = MK_content;

tb_MK_PasswordBox_Creation.KeyDown += MK_PB_Creation_KeyDown;
}

private void MK_PB_Creation_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
if (!string.IsNullOrEmpty(MK_content))
{

tb_MK_PasswordBox_Creation.Password = MK_content;
File.WriteAllText(MK_path, MK_content);
}
}
}
}
}
95 Replies
Pobiega
Pobiegaā€¢7mo ago
have you set a breakpoint and verified that your code runs as expected?
Nadekai
NadekaiOPā€¢7mo ago
Runs as expected? Also yea, I did breakpoints But for some reason it just doesn't write text into the file
Pobiega
Pobiegaā€¢7mo ago
but it does hit the File.WriteAllText(MK_path, MK_content); line, and MK_content has a non-null non-empty value at that point? you verified this with the debugger?
Nadekai
NadekaiOPā€¢7mo ago
How do I see variables in debug mode? Hmm... MK content is null
Pobiega
Pobiegaā€¢7mo ago
only time you set it is when reading from the file so if the file doesnt exist, you write null to it šŸ˜®
Nadekai
NadekaiOPā€¢7mo ago
It's a WPF And file exists
Pobiega
Pobiegaā€¢7mo ago
and its non-empty? again, set a breakpoint on the load and verify
Nadekai
NadekaiOPā€¢7mo ago
Oh right, I should probably update you on my code
Pobiega
Pobiegaā€¢7mo ago
breakpoint + debugging = the way to go
Nadekai
NadekaiOPā€¢7mo ago
c#

using System;
using System.Collections.Generic;



namespace Password_Manager
{

public partial class MainWindow : Window
{
public int debugInt = 0;
public bool MK_exists = false;
public string MK_content = null;
public string MK_path = "Data\\MasterKey.txt";
public MainWindow()
{
InitializeComponent();

// Accessing variables




if (File.Exists(MK_path))
{
// Check if MasterKey.txt exists and has content
if (new FileInfo(MK_path).Length > 0)
{
MK_exists = true;
MK_content = File.ReadAllText(MK_path); // Read the content of MasterKey.txt
}
}
else
{
// Create MasterKey.txt if it doesn't exist
using (File.Create(MK_path)) { }
}

// Show the appropriate stack panel based on MK existence
if (MK_exists)
{
sp_MK_Login.Visibility = Visibility.Visible;
}
else
{
sp_MK_creation.Visibility = Visibility.Visible;
}

// Set the content of the password box if MK_content is not null or empty


debug_label.Content = MK_content;

PasswordBox_MK_Creation.KeyDown += MK_PB_Creation_KeyDown;
}

private void MK_PB_Creation_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{

PasswordBox_MK_Creation.Password = MK_content;
File.WriteAllText(MK_path, MK_content);
debugInt++;


}
}




}
}
c#

using System;
using System.Collections.Generic;



namespace Password_Manager
{

public partial class MainWindow : Window
{
public int debugInt = 0;
public bool MK_exists = false;
public string MK_content = null;
public string MK_path = "Data\\MasterKey.txt";
public MainWindow()
{
InitializeComponent();

// Accessing variables




if (File.Exists(MK_path))
{
// Check if MasterKey.txt exists and has content
if (new FileInfo(MK_path).Length > 0)
{
MK_exists = true;
MK_content = File.ReadAllText(MK_path); // Read the content of MasterKey.txt
}
}
else
{
// Create MasterKey.txt if it doesn't exist
using (File.Create(MK_path)) { }
}

// Show the appropriate stack panel based on MK existence
if (MK_exists)
{
sp_MK_Login.Visibility = Visibility.Visible;
}
else
{
sp_MK_creation.Visibility = Visibility.Visible;
}

// Set the content of the password box if MK_content is not null or empty


debug_label.Content = MK_content;

PasswordBox_MK_Creation.KeyDown += MK_PB_Creation_KeyDown;
}

private void MK_PB_Creation_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{

PasswordBox_MK_Creation.Password = MK_content;
File.WriteAllText(MK_path, MK_content);
debugInt++;


}
}




}
}
I fixed the issue with if (!string.IsNullOrEmpty(MK_content))
Pobiega
Pobiegaā€¢7mo ago
the code you pasted does not contain any string.INOE fyi
Nadekai
NadekaiOPā€¢7mo ago
string.INOE?
Pobiega
Pobiegaā€¢7mo ago
IsNullOrEmpty
Nadekai
NadekaiOPā€¢7mo ago
I know This is just creating the MK It's supposed to be empty Basically the purpose of this is to check whetehr MK exists, it doesn't, so I create MK and save it into the file But the problem is it doesn't save to the file
Pobiega
Pobiegaā€¢7mo ago
where exactly do you "create the MK"? again, nowhere do you assign a value to that variable it will never not be null or ""
Nadekai
NadekaiOPā€¢7mo ago
using (File.Create(MK_path)) { }
Pobiega
Pobiegaā€¢7mo ago
yeah that makes an empty file
Nadekai
NadekaiOPā€¢7mo ago
Yea, it;s supposed to be empty After creation I want for user to input a value and that value gets saved into that So I can move to MK_login
Pobiega
Pobiegaā€¢7mo ago
you uh... don't need that :p its trivial to create a file when writing to it, if it doesnt already exist
Nadekai
NadekaiOPā€¢7mo ago
oh, so what you're saying I should move the creation of the fil I found something even worse f (MK_exists) { sp_MK_Login.Visibility = Visibility.Visible; } else { sp_MK_creation.Visibility = Visibility.Visible; } This doesn't even work
Pobiega
Pobiegaā€¢7mo ago
your code is very hard to read btw
Nadekai
NadekaiOPā€¢7mo ago
Why?
Pobiega
Pobiegaā€¢7mo ago
random indentations and blank lines, terrible variable names
Nadekai
NadekaiOPā€¢7mo ago
Variables would be really long otherwise
Pobiega
Pobiegaā€¢7mo ago
filePath fileContent you can (and should) also create methods to separate your code into blocks that have separate purposes and don't do file-io in a class constructor :d
Nadekai
NadekaiOPā€¢7mo ago
File-io?
Pobiega
Pobiegaā€¢7mo ago
file input/output ie, reading or writing to a file
Nadekai
NadekaiOPā€¢7mo ago
Then what am I supposed to do šŸ˜­ I'll probably... just restart the project
Pobiega
Pobiegaā€¢7mo ago
describe to me exactly what is going on here. On startup, you want to know if a masterkey is set or not?
Nadekai
NadekaiOPā€¢7mo ago
Gimme second
Pobiega
Pobiegaā€¢7mo ago
and if not, prompt the user for one and save it, yeah?
Nadekai
NadekaiOPā€¢7mo ago
In the beginning I am trying to learn whether masterKey exists or not. If it doesn't, I prompt user to create one. User enters the value into passwordbox and presses enter, the masterkey gets saved. After that a new window gets opened with accounts and passwords to them. On consecutive launches it prompts user to login, aka enter masterkey they had made which then opens the window with accounts and passwords
Pobiega
Pobiegaā€¢7mo ago
how does the prompt work?
Nadekai
NadekaiOPā€¢7mo ago
hm?
Pobiega
Pobiegaā€¢7mo ago
is it a separate window? a popup?
Nadekai
NadekaiOPā€¢7mo ago
A window
Pobiega
Pobiegaā€¢7mo ago
separate window?
Nadekai
NadekaiOPā€¢7mo ago
Ye I currently don't have a window for accounts and passwords...
Pobiega
Pobiegaā€¢7mo ago
okay.
Nadekai
NadekaiOPā€¢7mo ago
I am not that far <Window x:Class="Password_Manager.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Password_Manager" mc:Ignorable="d" Title="Login Window" Height="450" Width="800"> <Grid> <TextBox HorizontalAlignment="Left" Width="120" Height="120" Cursor="IBeam"></TextBox> <!-- Login Window--> <StackPanel Name="sp_MK_Login" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Hidden"> <TextBlock Name="tb_MK_login" Text="Enter your MasterKey" FontSize="20"></TextBlock> <PasswordBox Name ="PasswordBox_MK_Login" Width="120" Height="20" IsEnabled="True"></PasswordBox> </StackPanel> <!-- MK creation Window--> <StackPanel Name="sp_MK_creation" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Hidden"> <TextBlock Name="tb_MK_creation" Text="Create your MasterKey" FontSize="20"></TextBlock> <PasswordBox Name="PasswordBox_MK_Creation" Width="120" Height="20" IsEnabled="True" Focusable="True" Cursor="IBeam"></PasswordBox> </StackPanel> <Label Name="debug_label" Content="" Width="120" Height="15" HorizontalAlignment="Right"></Label> </Grid> </Window>
Pobiega
Pobiegaā€¢7mo ago
you're building something similar to KeePass right?
Nadekai
NadekaiOPā€¢7mo ago
Ye
Nadekai
NadekaiOPā€¢7mo ago
No description
No description
Nadekai
NadekaiOPā€¢7mo ago
Had a small sketch The "after user choice" it displays the box with password
Pobiega
Pobiegaā€¢7mo ago
I'd probably code this as actually loading the main window first, then immediately on load check if the masterkey exists or not if not, show a dialog that must be answered before you can interact with the main window
Nadekai
NadekaiOPā€¢7mo ago
Fair, but I have no idea how to make these
Pobiega
Pobiegaā€¢7mo ago
you just make a normal window and show it with .ShowDialog
Nadekai
NadekaiOPā€¢7mo ago
I'll probably restart the project as I've already made a massive mess out of it
Pobiega
Pobiegaā€¢7mo ago
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
if(!KeyCore.TryLoadMasterKey())
{
var ask = new AskForMasterKeyWindow();

if(ask.ShowDialog() == true)
{
KeyCore.SetMasterKey(ask.Password);
}
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
if(!KeyCore.TryLoadMasterKey())
{
var ask = new AskForMasterKeyWindow();

if(ask.ShowDialog() == true)
{
KeyCore.SetMasterKey(ask.Password);
}
}
}
}
what do you think about something like this?
Nadekai
NadekaiOPā€¢7mo ago
What's keycore?
Pobiega
Pobiegaā€¢7mo ago
something I just came up with. in this case, its a static class that keeps track of the master key
Nadekai
NadekaiOPā€¢7mo ago
Sounds nice But am still hellishly confused where to even start
Pobiega
Pobiegaā€¢7mo ago
well, with the code I just gave you is a pretty good start then just hooking up that Window_loaded event in your xaml then implementing the missing classes and methods
Nadekai
NadekaiOPā€¢7mo ago
On the note of you saying I should make the file once user inputs the value, how would I go about that?
Pobiega
Pobiegaā€¢7mo ago
internal static void SetMasterKey(string password)
{
_masterKey = password;
File.WriteAllText(MasterKeyFilePath, _masterKey);
}
internal static void SetMasterKey(string password)
{
_masterKey = password;
File.WriteAllText(MasterKeyFilePath, _masterKey);
}
Nadekai
NadekaiOPā€¢7mo ago
Do I just make multiple Windows? internal?
Pobiega
Pobiegaā€¢7mo ago
thats my implementation of SetMasterKey šŸ˜„ it does just that if the file exist, it overwrites if it doesnt, it creates as easy as that
Nadekai
NadekaiOPā€¢7mo ago
Woudln't that be problematic?
Pobiega
Pobiegaā€¢7mo ago
why?
Nadekai
NadekaiOPā€¢7mo ago
dunno, overwriting masterkey every single time...
Pobiega
Pobiegaā€¢7mo ago
someone called "SetMasterKey" well, its not the only way to call that method currently is if the file doesnt exist
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (!KeyCore.TryLoadMasterKey())
{
var ask = new AskForMasterKeyWindow();

if (ask.ShowDialog() == true)
{
KeyCore.SetMasterKey(ask.Password);
}
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (!KeyCore.TryLoadMasterKey())
{
var ask = new AskForMasterKeyWindow();

if (ask.ShowDialog() == true)
{
KeyCore.SetMasterKey(ask.Password);
}
}
}
Nadekai
NadekaiOPā€¢7mo ago
my brain too dumb šŸ˜­
Pobiega
Pobiegaā€¢7mo ago
only if TryLoadMasterKey() returns false do we actually call KeyCore.SetMasterKey
Nadekai
NadekaiOPā€¢7mo ago
I just can't wrap my head around it I guess...
Pobiega
Pobiegaā€¢7mo ago
what in particular is it you cant understand atm?
Nadekai
NadekaiOPā€¢7mo ago
Probably everything, I don't know where to start, just too many methods
Pobiega
Pobiegaā€¢7mo ago
well I've given you a fairly solid beginning atm
Nadekai
NadekaiOPā€¢7mo ago
I know that
c#
using System;
using System.IO;
using System.Windows;

namespace Password_manager
{
public partial class App : Application
{
private const string MasterKeyFilePath = "masterkey.txt";

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

if (!MasterKeyExists())
{
// If master key doesn't exist, prompt the user to create one
MasterKeyPrompt masterKeyPrompt = new MasterKeyPrompt();
masterKeyPrompt.ShowDialog();

// Save the master key
SaveMasterKey(masterKeyPrompt.PasswordBox.Password);
}

// Open the main window
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}

private bool MasterKeyExists()
{
return File.Exists(MasterKeyFilePath);
}

private void SaveMasterKey(string masterKey)
{
File.WriteAllText(MasterKeyFilePath, masterKey);
}
}
}
c#
using System;
using System.IO;
using System.Windows;

namespace Password_manager
{
public partial class App : Application
{
private const string MasterKeyFilePath = "masterkey.txt";

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

if (!MasterKeyExists())
{
// If master key doesn't exist, prompt the user to create one
MasterKeyPrompt masterKeyPrompt = new MasterKeyPrompt();
masterKeyPrompt.ShowDialog();

// Save the master key
SaveMasterKey(masterKeyPrompt.PasswordBox.Password);
}

// Open the main window
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}

private bool MasterKeyExists()
{
return File.Exists(MasterKeyFilePath);
}

private void SaveMasterKey(string masterKey)
{
File.WriteAllText(MasterKeyFilePath, masterKey);
}
}
}
And now I am stuck ;-;
Nadekai
NadekaiOPā€¢7mo ago
@Pobiega Why isn't it displaying there?
No description
No description
Nadekai
NadekaiOPā€¢7mo ago
No description
Pobiega
Pobiegaā€¢7mo ago
I'm not sure if WPF allows you to show a dialog without having a window shown already. Maybe maybe not.
Nadekai
NadekaiOPā€¢7mo ago
Either way I run into another weird problem For some reason main window does not open
c#
using System;
using System.IO;
using System.Windows;

namespace Password_manager
{
public partial class App : Application
{
private int debugInt = 0;
private const string MasterKeyFilePath = "Data\\masterkey.txt";
private string MasterKey = File.ReadAllText(MasterKeyFilePath);



protected override void OnStartup(StartupEventArgs e)
{
debugInt++;
base.OnStartup(e);

if (!MasterKeyExists())
{
if(!Directory.Exists(MasterKeyFilePath))
{
// If master key doesn't exist, prompt the user to create one
MasterKeyPrompt masterKeyPrompt = new MasterKeyPrompt();
masterKeyPrompt.ShowDialog();

// Save the master key
Directory.CreateDirectory("Data");
SaveMasterKey(masterKeyPrompt.PasswordBox.Password);

}

}
else
{
Login login = new Login();
login.ShowDialog();

if (login.PasswordBox.Password == MasterKey)
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
}

}



private bool MasterKeyExists()
{
return File.Exists(MasterKeyFilePath);
}

private void SaveMasterKey(string masterKey)
{

File.WriteAllText(MasterKeyFilePath, masterKey);
}
}
}
c#
using System;
using System.IO;
using System.Windows;

namespace Password_manager
{
public partial class App : Application
{
private int debugInt = 0;
private const string MasterKeyFilePath = "Data\\masterkey.txt";
private string MasterKey = File.ReadAllText(MasterKeyFilePath);



protected override void OnStartup(StartupEventArgs e)
{
debugInt++;
base.OnStartup(e);

if (!MasterKeyExists())
{
if(!Directory.Exists(MasterKeyFilePath))
{
// If master key doesn't exist, prompt the user to create one
MasterKeyPrompt masterKeyPrompt = new MasterKeyPrompt();
masterKeyPrompt.ShowDialog();

// Save the master key
Directory.CreateDirectory("Data");
SaveMasterKey(masterKeyPrompt.PasswordBox.Password);

}

}
else
{
Login login = new Login();
login.ShowDialog();

if (login.PasswordBox.Password == MasterKey)
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
}

}



private bool MasterKeyExists()
{
return File.Exists(MasterKeyFilePath);
}

private void SaveMasterKey(string masterKey)
{

File.WriteAllText(MasterKeyFilePath, masterKey);
}
}
}
Pobiega
Pobiegaā€¢7mo ago
you're doing way too much stuff here the whole conditional loading of windows thing you have going on makes me feel very uneasy
Nadekai
NadekaiOPā€¢7mo ago
cmon, it's not that bad you should have seen my first game's code
Pobiega
Pobiegaā€¢7mo ago
probably not šŸ˜„
Nadekai
NadekaiOPā€¢7mo ago
anyways, any idea why it just... doesn't work? The program stops when I enter correct password
Pobiega
Pobiegaā€¢7mo ago
mhm because you killed your only window
Nadekai
NadekaiOPā€¢7mo ago
oh right
Pobiega
Pobiegaā€¢7mo ago
a WPF/winforms app must at all times have a window active, or it terminates
Nadekai
NadekaiOPā€¢7mo ago
I miss the enter button void Now I forgot how to do it How would you do "When enter is pressed, the login confirms"
Pobiega
Pobiegaā€¢7mo ago
https://stackoverflow.com/questions/4194470/how-to-make-a-submit-button-in-wpf set the IsDefault property on a button anyways, gonna sleep
Nadekai
NadekaiOPā€¢7mo ago
nooo
Pobiega
Pobiegaā€¢7mo ago
gl with your stuff. stop doing conditional creation of your primary form :p
Nadekai
NadekaiOPā€¢7mo ago
I have 6 hours to finish this This makes me realize I already spent 5 hours and I haven't even started
Pobiega
Pobiegaā€¢7mo ago
šŸ’¤ ... you start 11 hours before the final deadline? bold move cotton, lets see if it pays off
Nadekai
NadekaiOPā€¢7mo ago
It's not that Teached gave us the assignment today He wants it submitted tommorow
Pobiega
Pobiegaā€¢7mo ago
thats a pretty rough timeline
Nadekai
NadekaiOPā€¢7mo ago
you tell me I have to learn whole wpf, make a code, design in a single day
Pobiega
Pobiegaā€¢7mo ago
ĀÆ\_(惄)_/ĀÆ stop overcomplicating things with your nested ifs everywhere
Nadekai
NadekaiOPā€¢7mo ago
I have no idea what you mean by that šŸ˜­
Pobiega
Pobiegaā€¢7mo ago
having 8 branches in your app startup is a massive codesmell
Nadekai
NadekaiOPā€¢7mo ago
I think it makes it easier Man, I had 98 ifs in a row In some code Each of them contain from 1 to 4 another ifs
Pobiega
Pobiegaā€¢7mo ago
that seems way too many to ever be good but i digress
Nadekai
NadekaiOPā€¢7mo ago
Got full points ĀÆ\_(惄)_/ĀÆ button does not work šŸ˜­
Pobiega
Pobiegaā€¢7mo ago
šŸ’¤ good night
Want results from more Discord servers?
Add your server