且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

分析文件路径、文件名、拓展名

更新时间:2022-08-15 14:11:40

        private void btnFile_Click(object sender, EventArgs e)
        {
            if (odlgFile.ShowDialog() == DialogResult.OK)
            {
                //
                //获取文件全路径
                //
                string strRoot = odlgFile.FileName;                  
                lblPath.Text = strRoot;
                //
                //获取文件名
                //
                int iBeginIndex = strRoot.LastIndexOf(@"\") + 1;
                int iEndIndex = strRoot.LastIndexOf(".");
                lblFileName.Text = strRoot.Substring(iBeginIndex,   
                    iEndIndex - iBeginIndex);
                //
                //获取文件拓展名
                //
                iBeginIndex = strRoot.LastIndexOf(".") + 1;
                iEndIndex = strRoot.Length;
                lblExtendName.Text = strRoot.Substring(iBeginIndex, 
                    iEndIndex - iBeginIndex);
            }
        }