Pergunta

Im just getting into C# forms and im wondering how to change a button text to the name of a file you are opening i have it somewhat working but when i open the file the buttons text changes to the path of that file i just want the files name

OpenFileDialog op = new OpenFileDialog();
op.Title = "open";
op.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*";
 if (op.ShowDialog() == DialogResult.OK)
   {
      richTextBox1.LoadFile(op.FileName, RichTextBoxStreamType.PlainText);
      button1.Text = op.FileName;
      this.Text = op.FileName;
   }
Foi útil?

Solução

You'll want to check out the Path class. Remember to reference System.IO

OpenFileDialog op = new OpenFileDialog();
op.Title = "open";
op.Filter = "Text Document(*.txt)|*.txt|All Files(*.*)|*.*";
 if (op.ShowDialog() == DialogResult.OK)
   {
      richTextBox1.LoadFile(op.FileName, RichTextBoxStreamType.PlainText);
      button1.Text = Path.GetFileNameWithoutExtension(op.FileName);  
  }

Outras dicas

Path.GetFileName(op.FileName);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top