Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. urgent copy image from hard Drive on local host

urgent copy image from hard Drive on local host

Scheduled Pinned Locked Moved C#
csharpasp-nethelpquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mavii
    wrote on last edited by
    #1

    am developing application in ASP.Net with C#. i am using button for FileOpenDialog to browse any image from hard drive as follows openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; problem is: i want the browsed image to be copied in local hosti.e,""C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg" i have the follwing idea in mind File.Copy("C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg", "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); or File.Copy(s.Substring(13,s.Length-13), "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); but i dont want to existing image to be overwrite, i want all images to ve saved in local host can anyone sugesst me what to do?

    G M 2 Replies Last reply
    0
    • M mavii

      am developing application in ASP.Net with C#. i am using button for FileOpenDialog to browse any image from hard drive as follows openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; problem is: i want the browsed image to be copied in local hosti.e,""C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg" i have the follwing idea in mind File.Copy("C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg", "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); or File.Copy(s.Substring(13,s.Length-13), "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); but i dont want to existing image to be overwrite, i want all images to ve saved in local host can anyone sugesst me what to do?

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      If you use File.Copy("C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg", "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); it will throw IOException if the destination already exists so you should check that before you start copying. What so you want to do if the file already exists? I suggest you give them all unique name using GUID.

      #region signature my articles #endregion

      M 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        If you use File.Copy("C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg", "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); it will throw IOException if the destination already exists so you should check that before you start copying. What so you want to do if the file already exists? I suggest you give them all unique name using GUID.

        #region signature my articles #endregion

        M Offline
        M Offline
        mavii
        wrote on last edited by
        #3

        will u plz make ur sugesstion some more clear that "give them all unique name using GUID" GUID?

        P 1 Reply Last reply
        0
        • M mavii

          will u plz make ur sugesstion some more clear that "give them all unique name using GUID" GUID?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          He means that the Guid type is guaranteed to generate a unique value, so there is no danger of overwriting files. For instance:

          string fileDest = Path.Combine(@"c:\Images\Copy", Guid.NewGuid().ToString() + ".gif");
          File.Copy(@"c:\Images\MyImage.gif", fileDest);
          

          Deja View - the feeling that you've seen this post before.

          M 1 Reply Last reply
          0
          • P Pete OHanlon

            He means that the Guid type is guaranteed to generate a unique value, so there is no danger of overwriting files. For instance:

            string fileDest = Path.Combine(@"c:\Images\Copy", Guid.NewGuid().ToString() + ".gif");
            File.Copy(@"c:\Images\MyImage.gif", fileDest);
            

            Deja View - the feeling that you've seen this post before.

            M Offline
            M Offline
            mavii
            wrote on last edited by
            #5

            can u suggest a solution for following problem public static int count = 0; private void button3_Click_1(object sender, EventArgs e) { openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; string st = "Pict" + (count++) + ".jpg"; File.Copy(s.Substring(13, s.Length - 13), "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\" + st);} i want the image to be saved in local host with the same name as as it was present in hard drive (from where it was browsed) i have applied ur GUID example but unfortunately its not working, can u apply this in my code?

            P P 2 Replies Last reply
            0
            • M mavii

              can u suggest a solution for following problem public static int count = 0; private void button3_Click_1(object sender, EventArgs e) { openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; string st = "Pict" + (count++) + ".jpg"; File.Copy(s.Substring(13, s.Length - 13), "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\" + st);} i want the image to be saved in local host with the same name as as it was present in hard drive (from where it was browsed) i have applied ur GUID example but unfortunately its not working, can u apply this in my code?

              P Offline
              P Offline
              pmarfleet
              wrote on last edited by
              #6

              Use the File.Exists method to check for the presence of a particular file. Paul

              1 Reply Last reply
              0
              • M mavii

                can u suggest a solution for following problem public static int count = 0; private void button3_Click_1(object sender, EventArgs e) { openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; string st = "Pict" + (count++) + ".jpg"; File.Copy(s.Substring(13, s.Length - 13), "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\" + st);} i want the image to be saved in local host with the same name as as it was present in hard drive (from where it was browsed) i have applied ur GUID example but unfortunately its not working, can u apply this in my code?

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                You can't keep the filenames the same - you are going to have to change them somehow. The Guid example would be:

                string s = System.IO.Path.GetFileName(openFileDialog1.FileName);
                string st = "Pict" + Guid.NewGuid().ToString() + ".jpg";
                File.Copy(openDialog1.Filename, Path.Combine(@"c:\inetpub\wwwroot\15 website\images", st));
                

                Deja View - the feeling that you've seen this post before.

                M 1 Reply Last reply
                0
                • M mavii

                  am developing application in ASP.Net with C#. i am using button for FileOpenDialog to browse any image from hard drive as follows openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; problem is: i want the browsed image to be copied in local hosti.e,""C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg" i have the follwing idea in mind File.Copy("C:\\Inetpub\\wwwroot\\15 WebSite\\images\\1.jpg", "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); or File.Copy(s.Substring(13,s.Length-13), "C:\\Inetpub\\wwwroot\\15 WebSite\\images\\xyz.jpg"); but i dont want to existing image to be overwrite, i want all images to ve saved in local host can anyone sugesst me what to do?

                  M Offline
                  M Offline
                  Malcolm Smart
                  wrote on last edited by
                  #8

                  If you get the solution can you please post it on all the forums your posted this question.

                  "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

                  "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    You can't keep the filenames the same - you are going to have to change them somehow. The Guid example would be:

                    string s = System.IO.Path.GetFileName(openFileDialog1.FileName);
                    string st = "Pict" + Guid.NewGuid().ToString() + ".jpg";
                    File.Copy(openDialog1.Filename, Path.Combine(@"c:\inetpub\wwwroot\15 website\images", st));
                    

                    Deja View - the feeling that you've seen this post before.

                    M Offline
                    M Offline
                    mavii
                    wrote on last edited by
                    #9

                    string strQuery = ""; openFileDialog1.ShowDialog(); string s = openFileDialog1.FileName; textBox6.Text = s; st2 = s; string st = "Pict" + Guid.NewGuid().ToString() + ".jpg"; File.Copy(openFileDialog1.FileName, Path.Combine(@"c:\inetpub\wwwroot\15 website\images", st)); strQuery = "INSERT INTO criminal(pic_name) VALUES('"+System.IO.Path.GetFileName(openFileDialog1.FileName)+"')"; SqlCommand com = new SqlCommand(strQuery, conn); com.ExecuteNonQuery(); PROBLEM: error comes on this the SQL Query , am i inserting rightly? or there is some other way Actually i want to insert only picture name of the picture to be browsed can u plz suggest any solution

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups