문제

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;
   }
도움이 되었습니까?

해결책

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

다른 팁

Path.GetFileName(op.FileName);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top