-4

i wanna make a button that opens a check file dialog and after that check if that file is the correct one,thanks. file name is key.txt,i already tried to get it but i have a way to check the path,i need help.

    private void button5_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.ShowDialog();

        if (System.IO.File.Exists("E:\\Key\\key.txt"))
        {
            MessageBox.Show("Injected");
        }
        else
        {
            MessageBox.Show("Wrong File");
        }
    }

This is what i have,any solution?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
MEEXT Tv
  • 7
  • 2
  • 1
    can you just check the filename after they've selected one, and if it is not `key.txt`, make them re-select – Jonesopolis Dec 10 '20 at 15:45
  • 6
    I'm confused - you have a program which knows the path of a file (it presumably wants to do something with) but instead of just using it you're forcing the user to select it? What's the purpose here? – Damien_The_Unbeliever Dec 10 '20 at 15:48
  • @Damien_The_Unbeliever i wanna make a priv system with a custom file to execute the app to make it more secure,so people need the file or a file with the same name to open it – MEEXT Tv Dec 10 '20 at 15:54
  • If you already know where this file is, you don't need an `OpenFileDialog`. – Robert Harvey Dec 10 '20 at 15:56
  • yes i do, i wanna make the user to select the specific file,and that file must have a specific name. Thats all,you press a button to open file dialog and you select the file ,then it checks if the file is the correct one with that custom name and it executes an option – MEEXT Tv Dec 10 '20 at 15:58
  • I think the point that people are trying to make is, if the user just has the file already there where you're expecting it and with the correct filename (and perhaps even contents) then there is no point in making them browse for and select that file. Your app can just check it internally. You're already doing that existence check in the code you posted. Forcing the user to go through unnecessary tasks is bad UX. – squillman Dec 10 '20 at 16:01
  • looks like he want to give user an opportunity to load config from different folders on device, but the name must be constant, that's all he wants. – Ivan Khorin Dec 10 '20 at 16:12
  • If all you want is to secure a program, that won't help at all. A filename can be trivially changed and won't be much of a secret once you've found the right one. It's [security though obscurity](https://en.wikipedia.org/wiki/Security_through_obscurity) at best. Instead, ask for a password and compare it to a hashed version. Or ask for a file (any name, it doesn't matters really), read its contents and get a strong cryptographic key from it, which you compare to something stored. – Alejandro Dec 10 '20 at 16:12

2 Answers2

0

It sounds like you just want to filter the available files that someone can select to those which match a particular file name, but still allow users to find that file in any folder.

Use the Filter property on the OpenFileDialog.

var ofd = new OpenFileDialog();
ofd.Filter = "key files|key.txt";
var dialogResult = ofd.ShowDialog();

if (dialogResult == DialogResult.OK && ofd.CheckFileExists)
{
    MessageBox.Show("Injected " + ofd.FileName);
}
else
{
    MessageBox.Show("Cancelled");
}

enter image description here

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0
private void button5_Click(object sender, EventArgs e)
{
    var ofd = new OpenFileDialog
    {
        Filter = "Key files (key.txt)|key.txt",
    };

    if (ofd.ShowDialog() == DialogResult.OK && File.Exists(ofd.FileName))
    {
        MessageBox.Show("Injected");
    }
    else
    {
        MessageBox.Show("Wrong File");
    }
}

but I strongly recommend you to add OpenFileDialog as a form control, so the code look like:

private void button5_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK && File.Exists(openFileDialog1.FileName))
    {
        MessageBox.Show("Injected");
    }
    else
    {
        MessageBox.Show("Wrong File");
    }
}

Configure filter in property window: filter property

Ivan Khorin
  • 827
  • 1
  • 5
  • 17