已有56人关注
从字符串分离文件路径
发表在C#图书答疑 2021-01-09 悬赏:1 学分 《C#精彩编程200例》第1章 WinForm窗体开发 12页-13页
是否精华
版块置顶:

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(".") ));

两者虽然都能实现取扩展名的功能,但书里面写的取到了扩展名后面多余的一个空值。(我应该没有理解错吧)


分享到:
精彩评论 1
小禾斗
学分:7308 LV13
TA的每日心情
奋斗
2017-04-21 14:00:25
2021-01-19
沙发

对,这种方式其实有点麻烦,后面学了File之后,有更简单的方法。

首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照