如何在OpenFileDialog中显示完整路径和文件名
我使用openfiledialog打开文件calender.txt,当它打开时,它显示日历文件名和.txt文件夹,但不包含目录c:\如何在OpenFileDialog中显示完整路径和文件名
任何人都可以请告诉我如何让我得到C到代码的对话框:\压延机在对话框
private void openFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = (@"C:\");
ofd.Filter = ("*.txt| Text File");
ofd.FileName = "calender.txt";
ofd.CheckFileExists = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
if (CheckValidity(ofd.FileName))
{
try
{
streamWriter sw = new streamWriter(ofd.FileName);
}
catch (FileLoadException flEx)
{
MessageBox.Show(flEx.Message);
}
else
{
}
}
}
}
可以设置文件名称可以在对话框中随意使用。如果你想显示在开始的完整路径,你可以做到以下几点:
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = (@"C:\");
ofd.FileName = Path.Combine(ofd.InitialDirectory, "calendar.txt");
请记住,这不会留下这样一旦用户选择不同的文件,但它实际上并不会对你来说很重要,因为一旦他们击中Open
,FileName
将为你提供完整的路径。
感谢您的帮助。 – Tom 2014-10-06 23:28:47
你可以采取由用户选择的路径与
string path_selected = ofd.FileName;
例如“C://users/user/desktop/myfile.txt”
我不知道,返回文件名中的任何方法,但你可以使用字符串的方法来编辑路径和获取文件名
感谢您的建议,我的问题可能不是非常明确的抱歉。答案是TyCobb给出的d.FileName = Path.Combine(ofd.InitialDirectory,“calendar.txt”); – Tom 2014-10-06 23:25:19
你试过'ofd.FileName = Path.Combine(ofd.InitialDirectory,“calendar.txt”);'? – TyCobb 2014-10-06 23:06:43
我相信你必须在控制面板 – Steve 2014-10-06 23:06:52
下设置文件夹选项,感谢TyCobb对它进行排序 – Tom 2014-10-06 23:12:13