Compare uploaded file name without extension using C# and VB.Net in ASP.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

Hi

i have 2 fileuploads. i need to compare between two for same filename.

my problem is how to compare only the filename without extension of file.

Please help

        protected void Upload_Click(object sender, EventArgs e)
        {
            string shtml;
            string sxml;
            byte[] vcBytes = null, rawBytes = null;
            foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles)
            {
                string filename = Path.GetFileName(postedFile.FileName);
                string fileExtension = Path.GetExtension(postedFile.FileName);
                byte[] bytes = null;
                using (Stream fs = postedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        bytes = br.ReadBytes((Int32)fs.Length);
                    }
                }
                if (fileExtension.ToLower() == ".htm")
                {
                    vcBytes = bytes;
                    shtml = System.IO.Path.GetFileNameWithoutExtension("vcBytes");
                }
                else
                {
                    Response.Write("<script>alert('Please select only Html file')</script>");
                }

            }
            foreach (HttpPostedFile postedFile in FileUpload3.PostedFiles)
            {
                string filename = Path.GetFileName(postedFile.FileName);
                string fileExtension = Path.GetExtension(postedFile.FileName);
                byte[] bytes = null;
                using (Stream fs = postedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        bytes = br.ReadBytes((Int32)fs.Length);
                    }
                }
                if (fileExtension.ToLower() == ".xml")
                {
                    rawBytes = bytes;
                    sxml = System.IO.Path.GetFileNameWithoutExtension("rawBytes");

                }
                else
                {
                    Response.Write("<script>alert('Please select only XML file')</script>");
                }

            }
            if (FileUpload2.FileName==FileUpload3.FileName)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
                string query = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = conn;
                    cmd.Parameters.AddWithValue("@vcFilePath", vcBytes);
                    cmd.Parameters.AddWithValue("@Rawdata", rawBytes);
                    cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    Response.Write("<script>alert('File Upload Sucessfull')</script>");
                    RefreshPage();
                    DropDownList1.ClearSelection();
                    DropDownList2.ClearSelection();
                }
            }
            else
            {
                Response.Write("<script>alert('Please select same file')</script>");
                RefreshPage();
                DropDownList1.ClearSelection();
                DropDownList2.ClearSelection();
            }            
        }
This question does not have replies that have been liked.