我使用openfiledialog打开文件calender.txt,当它打开时,它在筛选框中显示日历作为文件名,而不显示目录c:\。
有谁能告诉我如何对对话框进行编码,以便在对话框中得到C:\calender
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
{
}
}
}
}发布于 2014-10-06 23:23:48
可以将文件名设置为对话框中您喜欢的任何内容。如果要在开始时显示完整路径,可以执行以下操作:
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = (@"C:\");
ofd.FileName = Path.Combine(ofd.InitialDirectory, "calendar.txt");请记住,一旦用户选择了不同的文件,情况就不会这样了,但这对您并不重要,因为一旦他们访问了Open,FileName将为您提供完整的路径。
发布于 2014-10-06 23:14:00
可以选择用户选择的路径。
string path_selected = ofd.FileName; 例如“c://user/user/桌面/myfile.txt”
我不知道任何返回文件名的方法,但是您可以使用字符串方法编辑路径并获取文件名。
https://stackoverflow.com/questions/26226139
复制相似问题