C# 如何在表单标题中显示当前打开文件的名称?
我正在制作一个文本编辑器,我想在表单标题中显示当前打开的文件的名称(就像记事本一样,其中显示“Untitled-Notepad”或“file-Notepad”) 我假设使用SaveFileDialog和OpenFileDialog完成了这项工作,所以我将发布我的当前代码 OpenFile:C# 如何在表单标题中显示当前打开文件的名称?,c#,winforms,visual-studio-2012,openfiledialog,savefiledialog,C#,Winforms,Visual Studio 2012,Openfiledialog,Savefiledialog,我正在制作一个文本编辑器,我想在表单标题中显示当前打开的文件的名称(就像记事本一样,其中显示“Untitled-Notepad”或“file-Notepad”) 我假设使用SaveFileDialog和OpenFileDialog完成了这项工作,所以我将发布我的当前代码 OpenFile: private void OpenFile() { NewFile(); OpenFileDialog ofd = new OpenFile
private void OpenFile()
{
NewFile();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.FileName = "";
ofd.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
if (ofd.ShowDialog() != DialogResult.OK) return;
StreamReader sr = null;
try
{
sr = new StreamReader(ofd.FileName);
this.Text = string.Format("{0} - Basic Word Processor", ofd.FileName);
richTextBoxPrintCtrl1.Text = ofd.FileName;
richTextBoxPrintCtrl1.Text = sr.ReadToEnd();
filepath = ofd.FileName;
richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);
}
catch
{
}
finally
{
if (sr != null) sr.Close();
}
保存文件
private void SaveFileAs()
{
SaveFileDialog sfdSaveFile = new SaveFileDialog();
sfdSaveFile.Title = "Save File";
sfdSaveFile.FileName = "Untitled";
sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
if (sfdSaveFile.ShowDialog() == DialogResult.OK)
try
{
filepath = sfdSaveFile.FileName;
SaveFile();
this.Text = string.Format("{0} - Basic Word Processor", sfdSaveFile.FileName);
}
catch (Exception exc)
{
}
void SetWindowTitle(string fileName) {
this.Text = string.Format("{0} - Basic Text Editor", System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog.Filename));
如何获取文件名并将其放在表单标题中(就像记事本那样,文件名后跟文本编辑器名)。您可以将其包装在如下函数中:
void SetWindowTitle(string fileName) {
this.Text = string.Format("{0} - MyEditor", System.IO.Path.GetFileName(fileName));
}
..并传入对话框文件名
编辑:
你的问题是你没有调用我给你的函数。你有我上面给你的函数。但是你没有调用它
替换此项:
this.Text = string.Format("{0} - Basic Word Processor", sfdSaveFile.FileName);
this.Text = string.Format("{0} - Basic Word Processor", ofd.FileName);
为此:
SetWindowTitle(sfdSaveFile.FileName);
SetWindowTitle(ofd.FileName);
并将其替换为:
this.Text = string.Format("{0} - Basic Word Processor", sfdSaveFile.FileName);
this.Text = string.Format("{0} - Basic Word Processor", ofd.FileName);
为此:
SetWindowTitle(sfdSaveFile.FileName);
SetWindowTitle(ofd.FileName);
当打开
private void OpenFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.FileName = "";
ofd.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
ofd.ShowDialog();
try
{
// just one line is added
this.Text = string.Format("{0} - MyNotepad", Path.GetFileName(ofd.Filename));
richTextBoxPrintCtrl1.Text = ofd.FileName;
StreamReader stread = new StreamReader(richTextBoxPrintCtrl1.Text);
richTextBoxPrintCtrl1.Text = stread.ReadToEnd();
stread.Close();
richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);
}
catch { }
}
在保存时
private void SaveFileAs()
{
SaveFileDialog sfdSaveFile = new SaveFileDialog();
sfdSaveFile.Title = "Save File";
sfdSaveFile.FileName = "Untitled";
sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
if (sfdSaveFile.ShowDialog() == DialogResult.OK)
try
{
richTextBoxPrintCtrl1.SaveFile(sfdSaveFile.FileName, RichTextBoxStreamType.RichText);
filepath = sfdSaveFile.FileName;
// just one line is added
this.Text = string.Format("{0} - MyNotepad", Path.GetFileName(sfd.Filename));
}
catch (Exception exc)
{
}
}
只是更新一下
Toby-
需要空的catch块。如果用户取消没有catch块的ofd或sfd,程序将崩溃。这样可以防止程序崩溃
您不需要catch块来检查用户是否选择了OK/Cancel
OpenFileDialog&SaveFileDialog具有返回对话框结果的方法ShowDialog
DialogResult.OK的值表示用户已选择要打开/保存的文件,并且未取消该操作
以及OpenFile的示例
私有void OpenFile()
{
OpenFileDialog ofd=新建OpenFileDialog();
ofd.Title=“打开文件”;
ofd.FileName=“”;
ofd.Filter=“富文本文件(.rtf)|.rtf |文本文档(.txt)|.txt | Microsoft Word文档(.doc)|.doc |超文本标记语言文档(.html)|.html”
您尝试过什么?。鉴于您所拥有的,设置表单标题实际上是一个困难的倒退。我唯一尝试过的是在标签中显示文件名,但效果不太好(它只显示.rtf文件标题)…作为一个备用,空的catch块是一个坏主意-解决了它。问题:它显示完整的文件路径。我如何才能使它只显示文件名和文件扩展名?出于某种原因,它仍在执行相同的操作。我尝试在“GetFileName”的末尾添加“WithoutExtension”,但它不起作用..您将向函数传递什么?对话框.FileName?我使用的另一个答案是“this.Text=string.Format(“{0}-MyNotepad”,of d.FileName);”.这是如何实现的?如果这是一个愚蠢的问题,对不起..我是编程新手,这都是经验。干杯,伙计。这与Simon的贡献配合得很好。我现在只需要显示文件名,就可以了。@Toby-修复了,先生,不知道我是如何错过Path.GetFileName的!我想与GF分手真的会影响我的生活注意!几个月前我也遇到过同样的情况,伙计。我完全知道这是什么感觉。希望你能尽快好起来。:-)需要空的catch块。如果用户在没有catch块的情况下取消ofd或sfd,程序就会崩溃。它可以防止程序崩溃。@Toby-answer更新以避免尝试catch以进行确定/取消检测。