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. Plz, des'nt File Delete ,, [modified]

Plz, des'nt File Delete ,, [modified]

Scheduled Pinned Locked Moved C#
csharpwpfhelpworkspace
9 Posts 7 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.
  • S Offline
    S Offline
    sonic747
    wrote on last edited by
    #1

    i want delete image file. very well insert image file to listbox. but, does'nt delete image file , plz, take me a error,, --- c#, wpf, .net3.5 /.... source code attched project files, source file download //////////error code//// //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message..

    modified on Tuesday, May 18, 2010 7:31 AM

    D L S S 4 Replies Last reply
    0
    • S sonic747

      i want delete image file. very well insert image file to listbox. but, does'nt delete image file , plz, take me a error,, --- c#, wpf, .net3.5 /.... source code attched project files, source file download //////////error code//// //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message..

      modified on Tuesday, May 18, 2010 7:31 AM

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      I understand that English isn't your native language, but your question is rather difficult to understand. This is not helped by the use of txtspk abreviations such as Plz instead of please! Please provide the specific code where you are attepting to delete the file along with any exceptions you are receiving. - now included :thumbsup:

      Dave

      If this helped, please vote & accept answer!

      Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      modified on Tuesday, May 18, 2010 8:09 AM

      J 1 Reply Last reply
      0
      • S sonic747

        i want delete image file. very well insert image file to listbox. but, does'nt delete image file , plz, take me a error,, --- c#, wpf, .net3.5 /.... source code attched project files, source file download //////////error code//// //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message..

        modified on Tuesday, May 18, 2010 7:31 AM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        If you have loaded an image from a file, the file will be locked as long as your image hasn't been disposed of. Maybe that is your trouble, I can't be bothered to download and investigate your code... :|

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        1 Reply Last reply
        0
        • D DaveyM69

          I understand that English isn't your native language, but your question is rather difficult to understand. This is not helped by the use of txtspk abreviations such as Plz instead of please! Please provide the specific code where you are attepting to delete the file along with any exceptions you are receiving. - now included :thumbsup:

          Dave

          If this helped, please vote & accept answer!

          Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          modified on Tuesday, May 18, 2010 8:09 AM

          J Offline
          J Offline
          Johnny J
          wrote on last edited by
          #4

          He did supply the entire code in a zip file, I think... :confused:

          D 1 Reply Last reply
          0
          • S sonic747

            i want delete image file. very well insert image file to listbox. but, does'nt delete image file , plz, take me a error,, --- c#, wpf, .net3.5 /.... source code attched project files, source file download //////////error code//// //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message..

            modified on Tuesday, May 18, 2010 7:31 AM

            S Offline
            S Offline
            sonic747
            wrote on last edited by
            #5

            //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message.. do not access ! becouse using of other process

            N W 2 Replies Last reply
            0
            • J Johnny J

              He did supply the entire code in a zip file, I think... :confused:

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              He edited the post to include it - fair enough

              Dave

              If this helped, please vote & accept answer!

              Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              1 Reply Last reply
              0
              • S sonic747

                //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message.. do not access ! becouse using of other process

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                As Luc has suggested the images are still in use by the listbox. You can remove them from the listbox an mark them for deletions by some background process.


                I know the language. I've read a book. - _Madmatt

                1 Reply Last reply
                0
                • S sonic747

                  i want delete image file. very well insert image file to listbox. but, does'nt delete image file , plz, take me a error,, --- c#, wpf, .net3.5 /.... source code attched project files, source file download //////////error code//// //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message..

                  modified on Tuesday, May 18, 2010 7:31 AM

                  S Offline
                  S Offline
                  Seishin
                  wrote on last edited by
                  #8

                  read the images with streams - no lock is set on the file. btw. I'm not sure you want to use 'System.Environment.CurrentDirectory' as it is relative to the point of application launch (shortcuts) and may change easily.. if you meant the application directory use Application.StartupPath

                  life is study!!!

                  1 Reply Last reply
                  0
                  • S sonic747

                    //image insert to listbox/// private void button1_Click(object sender, RoutedEventArgs e) { savepath = System.Environment.CurrentDirectory.ToString(); foreach (string f in Directory.GetFiles(savepath + "/image", "*.jpg")) { BitmapImage my; my= new BitmapImage(); my.BeginInit(); my.CacheOption = BitmapCacheOption.OnLoad; my.UriSource = new Uri(f, UriKind.RelativeOrAbsolute); my.EndInit(); listBox1.Items.Add(my); } } /////////delete image file /// private void button2_Click(object sender, RoutedEventArgs e) { string makestr1, item1; item1 = listBox1.Items[0].ToString(); makestr1 = item1.Substring(8, item1.Length - 8); listBox1.Items.Remove(listBox1.Items[0]); if (System.IO.File.Exists(makestr1)) { System.IO.File.Delete(makestr1); //<--error point } else MessageBox.Show("Nothingfile"); } //error message.. do not access ! becouse using of other process

                    W Offline
                    W Offline
                    William Winner
                    wrote on last edited by
                    #9

                    As they've said your program still has a lock on the files. I would guess that when the image is loaded into the BitmapImage, it locks the file. See this blog: “Memory leak” with BitmapImage and MemoryStream[^] It doesn't look like it has a dispose method, so you're best bet is to do what others have suggested and use a stream to read it in yourself.

                    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