简体   繁体   中英

Getting file name and changing a button to that name

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;
   }
Path.GetFileName(op.FileName);

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);  
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM