C
C#ā€¢3mo 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ā€¢3mo ago
have you set a breakpoint and verified that your code runs as expected?
Nadekai
Nadekaiā€¢3mo ago
Runs as expected? Also yea, I did breakpoints But for some reason it just doesn't write text into the file
Pobiega
Pobiegaā€¢3mo 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
Nadekaiā€¢3mo ago
How do I see variables in debug mode? Hmm... MK content is null
Pobiega
Pobiegaā€¢3mo ago
only time you set it is when reading from the file so if the file doesnt exist, you write null to it šŸ˜®
Nadekai
Nadekaiā€¢3mo ago
It's a WPF And file exists
Pobiega
Pobiegaā€¢3mo ago
and its non-empty? again, set a breakpoint on the load and verify
Nadekai
Nadekaiā€¢3mo ago
Oh right, I should probably update you on my code
Pobiega
Pobiegaā€¢3mo ago
breakpoint + debugging = the way to go
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo ago
the code you pasted does not contain any string.INOE fyi
Nadekai
Nadekaiā€¢3mo ago
string.INOE?
Pobiega
Pobiegaā€¢3mo ago
IsNullOrEmpty
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo 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
Nadekaiā€¢3mo ago
using (File.Create(MK_path)) { }
Pobiega
Pobiegaā€¢3mo ago
yeah that makes an empty file
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo ago
you uh... don't need that :p its trivial to create a file when writing to it, if it doesnt already exist
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo ago
your code is very hard to read btw
Nadekai
Nadekaiā€¢3mo ago
Why?
Pobiega
Pobiegaā€¢3mo ago
random indentations and blank lines, terrible variable names
Nadekai
Nadekaiā€¢3mo ago
Variables would be really long otherwise
Pobiega
Pobiegaā€¢3mo 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
Nadekaiā€¢3mo ago
File-io?
Pobiega
Pobiegaā€¢3mo ago
file input/output ie, reading or writing to a file
Nadekai
Nadekaiā€¢3mo ago
Then what am I supposed to do šŸ˜­ I'll probably... just restart the project
Pobiega
Pobiegaā€¢3mo ago
describe to me exactly what is going on here. On startup, you want to know if a masterkey is set or not?
Nadekai
Nadekaiā€¢3mo ago
Gimme second
Pobiega
Pobiegaā€¢3mo ago
and if not, prompt the user for one and save it, yeah?
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo ago
how does the prompt work?
Nadekai
Nadekaiā€¢3mo ago
hm?
Pobiega
Pobiegaā€¢3mo ago
is it a separate window? a popup?
Nadekai
Nadekaiā€¢3mo ago
A window
Pobiega
Pobiegaā€¢3mo ago
separate window?
Nadekai
Nadekaiā€¢3mo ago
Ye I currently don't have a window for accounts and passwords...
Pobiega
Pobiegaā€¢3mo ago
okay.
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo ago
you're building something similar to KeePass right?
Nadekai
Nadekaiā€¢3mo ago
Ye
Nadekai
Nadekaiā€¢3mo ago
No description
No description
Nadekai
Nadekaiā€¢3mo ago
Had a small sketch The "after user choice" it displays the box with password
Pobiega
Pobiegaā€¢3mo 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
Nadekaiā€¢3mo ago
Fair, but I have no idea how to make these
Pobiega
Pobiegaā€¢3mo ago
you just make a normal window and show it with .ShowDialog
Nadekai
Nadekaiā€¢3mo ago
I'll probably restart the project as I've already made a massive mess out of it
Pobiega
Pobiegaā€¢3mo 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
Nadekaiā€¢3mo ago
What's keycore?
Pobiega
Pobiegaā€¢3mo ago
something I just came up with. in this case, its a static class that keeps track of the master key
Nadekai
Nadekaiā€¢3mo ago
Sounds nice But am still hellishly confused where to even start
Pobiega
Pobiegaā€¢3mo 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
Nadekaiā€¢3mo 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ā€¢3mo 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
Nadekaiā€¢3mo ago
Do I just make multiple Windows? internal?
Pobiega
Pobiegaā€¢3mo 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
Nadekaiā€¢3mo ago
Woudln't that be problematic?
Pobiega
Pobiegaā€¢3mo ago
why?
Nadekai
Nadekaiā€¢3mo ago
dunno, overwriting masterkey every single time...
Pobiega
Pobiegaā€¢3mo 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
Nadekaiā€¢3mo ago
my brain too dumb šŸ˜­
Pobiega
Pobiegaā€¢3mo ago
only if TryLoadMasterKey() returns false do we actually call KeyCore.SetMasterKey
Nadekai
Nadekaiā€¢3mo ago
I just can't wrap my head around it I guess...
Pobiega
Pobiegaā€¢3mo ago
what in particular is it you cant understand atm?
Nadekai
Nadekaiā€¢3mo ago
Probably everything, I don't know where to start, just too many methods
Pobiega
Pobiegaā€¢3mo ago
well I've given you a fairly solid beginning atm
Nadekai
Nadekaiā€¢3mo 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
Nadekaiā€¢3mo ago
@Pobiega Why isn't it displaying there?
No description
No description
Nadekai
Nadekaiā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
I'm not sure if WPF allows you to show a dialog without having a window shown already. Maybe maybe not.
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo 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
Nadekaiā€¢3mo ago
cmon, it's not that bad you should have seen my first game's code
Pobiega
Pobiegaā€¢3mo ago
probably not šŸ˜„
Nadekai
Nadekaiā€¢3mo ago
anyways, any idea why it just... doesn't work? The program stops when I enter correct password
Pobiega
Pobiegaā€¢3mo ago
mhm because you killed your only window
Nadekai
Nadekaiā€¢3mo ago
oh right
Pobiega
Pobiegaā€¢3mo ago
a WPF/winforms app must at all times have a window active, or it terminates
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo 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
Nadekaiā€¢3mo ago
nooo
Pobiega
Pobiegaā€¢3mo ago
gl with your stuff. stop doing conditional creation of your primary form :p
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo ago
šŸ’¤ ... you start 11 hours before the final deadline? bold move cotton, lets see if it pays off
Nadekai
Nadekaiā€¢3mo ago
It's not that Teached gave us the assignment today He wants it submitted tommorow
Pobiega
Pobiegaā€¢3mo ago
thats a pretty rough timeline
Nadekai
Nadekaiā€¢3mo ago
you tell me I have to learn whole wpf, make a code, design in a single day
Pobiega
Pobiegaā€¢3mo ago
ĀÆ\_(惄)_/ĀÆ stop overcomplicating things with your nested ifs everywhere
Nadekai
Nadekaiā€¢3mo ago
I have no idea what you mean by that šŸ˜­
Pobiega
Pobiegaā€¢3mo ago
having 8 branches in your app startup is a massive codesmell
Nadekai
Nadekaiā€¢3mo 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ā€¢3mo ago
that seems way too many to ever be good but i digress
Nadekai
Nadekaiā€¢3mo ago
Got full points ĀÆ\_(惄)_/ĀÆ button does not work šŸ˜­
Pobiega
Pobiegaā€¢3mo ago
šŸ’¤ good night
Want results from more Discord servers?
Add your server
More Posts
Superclass gunI am trying to make a comparison of guns ona video game. I cant figure out how to make the gun classConnecting to an ATEM Switcher via C#Ā (Multiplatform)So, I am trying to invoke some disconnection logic, in order to access COM ports. The SDK says it is.net 8 blazor wasm front end breakpoints only work SOME of the timesometimes i launch the project and they hit, and sometimes they dont. I have no idea why. does anyonāœ… Problems with Dijkstra AlgorithmSo this is my take on Dijkstra in C#. After doing everything. My output is completely wrong. Can som.NET Core - Azure OAuth 2.Hi I am currently building out an System which requires the use of a webhook from a thrid party systRedirect after form POST Blazor StaticAm I not able to have a statically rendered Blazor `@code` block return a redirect? e.g.: ```c# @coHow should my dotnet 6.0 WPF application interface with a Mongo database in prod environment?was thinking of a more solid 3 tier architecture with having my application interface with some sort.NET Core `dotnet watch run` on MacOSFor me, `dotnet watch run` frequently needs to be restarted because it stops seeing changes. Does anHelp implementing multithreading in a distributed system, please?I got a distributed system, meaning one solution that's a server and two which are clients. The objeHow to retrive info about CPU usage GPU usage and RAM usage on Linux .NETI'm creating a resource manager in Avalonia and want it to run mostly on Linux for a school project.