private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string str_all = openFileDialog1.FileName;
string str_path = str_all.Substring(0, str_all.LastIndexOf("\\")+1);
string str_name = str_all.Substring(str_all.LastIndexOf("\\") + 1,
str_all.LastIndexOf(".")-(str_all.LastIndexOf("\\")+1));
string str_exc = str_all.Substring(str_all.LastIndexOf(".") + 1,
str_all.Length - (str_all.LastIndexOf(".") -1));
label1.Text = "文件路径:"+str_path;
label3.Text = "文件名称:" + str_name;
label4.Text = "文件全部信息" + str_all;
label2.Text = "文件后缀名:" + str_exc;
}
建议将文中的
string str_exc = str_all.Substring(str_all.LastIndexOf(".") + 1,
str_all.Length - (str_all.LastIndexOf(".") -1));
改为
string str_exc = str_all.Substring(str_all.LastIndexOf(".") + 1,
str_all.Length - (str_all.LastIndexOf(".") ));
两者虽然都能实现取扩展名的功能,但书里面写的取到了扩展名后面多余的一个空值。(我应该没有理解错吧)