Coding File Delete

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 8532 Views - Last Post: 21 June 2010 - 03:02 PM Rate Topic: -----

#1 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Coding File Delete

Posted 17 June 2010 - 01:37 PM

Hello all,

I am running into some trouble overwriting an image file (A generic error occurred in GDI+).

I am now trying to delete the file before saving (since I can't seem to overwrite it). I am using this code to delete the file:

 
   Dim PartImgFile As FileInfo = New FileInfo(ImagePathOpened.Text & "\PartImage.jpg")
                If PartImgFile.Exists Then
                    File.Delete(ImagePathOpened.Text & "\PartImage.jpg")
                End If



With this I am getting an error that the file cannot be deleted because it is being used.

Can someone please give me an idea on how to delete this file or overwrite it?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Coding File Delete

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1061
  • View blog
  • Posts: 4,131
  • Joined: 02-July 08

Re: Coding File Delete

Posted 17 June 2010 - 01:46 PM

This filemode either creates or overwrites it.
Using fs As New FileStream(<filepath>, FileMode.Create)
...
End Using

Was This Post Helpful? 0
  • +
  • -

#3 Goethals  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 145
  • Joined: 14-April 09

Re: Coding File Delete

Posted 17 June 2010 - 01:46 PM

What are u using the file for?
Who's building it?

Your exception points another application
is still working with that file.
So or you have not closed your stream after
building it, and your own application is still
in 'write' mode, or your still reading the file.

The reason you cant overwrite it is the same
as the reason you cant delete it.
Was This Post Helpful? 0
  • +
  • -

#4 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Coding File Delete

Posted 17 June 2010 - 02:01 PM

It basically means that another program has the file open, so you cannot delete / modify it. If you have made the file, check that you have disposed & closed anything accesing it (such as a streamreader / writer). Once there is nothing using it it may be deleted.
Was This Post Helpful? 0
  • +
  • -

#5 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Coding File Delete

Posted 17 June 2010 - 02:04 PM

Thanks hawkvalley1, but I keep getting the "A generic error occurred in GDI+" problem

Goethals, I have an application that at one point allows a user to select an image to display in an PictureBox. When the document is saved, that choosen image is also saved. If at somepoint the image is changed and the document saved again that is when I begin getting these errors since I can't seem to overwrite the file.

As you mention my application may still be reading the file. Is there a way stop this or "close" the file?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Coding File Delete

Posted 17 June 2010 - 02:10 PM

You will need to determine which program is using your file before deleting it. You can do so using Unlocker
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Coding File Delete

Posted 17 June 2010 - 02:10 PM

Ok when you do this

Dim PartImgFile As FileInfo = New FileInfo(ImagePathOpened.Text & "\PartImage.jpg")



You're essentially opening the file (so you can get the FileInfo) then you're trying to delete it while you currently have it open. Instead of checking if a file exists with the FileInfo class just use a string like this

Dim PartImgFile As String = ImagePathOpened.Text & "\PartImage.jpg"



Then you can check if it exists

If File.Exists(PartImgFile) Then
   File.Delete(PartImgFile)
End If


Was This Post Helpful? 0
  • +
  • -

#8 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Coding File Delete

Posted 17 June 2010 - 02:28 PM

Psycho Coder, I gave your code a try and I get the same error that the file is in use.

If I limit it to only loading the image I still have the same problem. The code I am using to load the image is:

  Dim file_name As String
  file_name = OpenImgDir.Text & "\PartImage.jpg"
  Dim bm1 As New Bitmap(file_name)
  MainForm.PartImg.Image = bm1



Could this still be using the image file?
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1061
  • View blog
  • Posts: 4,131
  • Joined: 02-July 08

Re: Coding File Delete

Posted 17 June 2010 - 02:34 PM

Are you using any other file streams where you have forgot to Close them?
Was This Post Helpful? 0
  • +
  • -

#10 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Coding File Delete

Posted 17 June 2010 - 02:43 PM

I can't find any file streams that haven't been closed.
Was This Post Helpful? 0
  • +
  • -

#11 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Coding File Delete

Posted 17 June 2010 - 02:46 PM

INSTALL UNLOCKER HERE

1: Run your program
2: Do everything that you normally do, but wait before carrying out the action which usually causes an error
3: Navigate to the file in windows explorer
4: Right click the file in windows explorer
5: Select the unlocker icon in windows explorer
6: It will tell you what is accessing the file.
7: If it your program, then close anything in your program which is accessing the file
8: If it is another program, just kill the program before trying to delete the file.



Also just out of interest, what happens when you try:
My.Computer.FileSystem.DeleteFile(ImagePathOpened.Text & "\PartImage.jpg")

This post has been edited by Jack Eagles1: 17 June 2010 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#12 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Coding File Delete

Posted 17 June 2010 - 02:56 PM

Jack Eagles1, when I use unlocker it shows that it is indeed my application that is using the file.

If I remove the bit of code I use to load the image (a couple of posts back) everything works fine. I can delete and overwrite the file.

This bit of code must still be reading the file, but I cannot find any options to close it???
Was This Post Helpful? 0
  • +
  • -

#13 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Coding File Delete

Posted 17 June 2010 - 03:06 PM

Can you post your whole code for us to look at please. There must be something there which is still accessing the file.
Was This Post Helpful? 0
  • +
  • -

#14 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Coding File Delete

Posted 17 June 2010 - 03:13 PM

If I only remove

  Dim file_name As String
  file_name = OpenImgDir.Text & "\PartImage.jpg"
  Dim bm1 As New Bitmap(file_name)
  MainForm.PartImg.Image = bm1



The problem goes away.


Here is the code.

This is the part that opens and creates project directories:

Public Class Project_Management

    'Open Existing Project
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenExisiting.Click
        Dim OpenFile As New OpenFileDialog
        OpenFile.FileName = ""
        OpenFile.Filter = "Text Files (*.txt)|*.txt"
        OpenFile.Title = "Open"
        OpenFile.ShowDialog()


        'Was a File Opened?
        FileOpened.Text = OpenFile.SafeFileName
        If FileOpened.Text = "" Then
            ok4.Text = "0"
        Else
            ok4.Text = "1"
        End If

        'Directory Path
        If ok4.Text = "1" Then
            'Identify Opening Directory Complete
            Dim str As String = OpenFile.FileName.Replace(OpenFile.SafeFileName, "")
            OpenDir.Text = (str)
            'Identify Opening Image Directory
            OpenImgDir.Text = OpenDir.Text & "Images"

            projectName.Text = OpenFile.SafeFileName
            MainForm.ImagePathOpened.Text = OpenImgDir.Text
            MainForm.OpenSave.Text = "Opened"


        End If


        'Load Text Items
        Try
            Dim Read As New System.IO.StreamReader(OpenFile.FileName)
            'Open Configuration Settings
            Configuration.ComboBox1.Text = Read.ReadLine
            Configuration.ComboBox2.Text = Read.ReadLine
            Configuration.ComboBox3.Text = Read.ReadLine
            Configuration.ComboBox4.Text = Read.ReadLine
            Configuration.ComboBox5.Text = Read.ReadLine
            Configuration.ComboBox6.Text = Read.ReadLine


            'Open InfoForm Items
            MainForm.TextBox1.Text = Read.ReadLine
            MainForm.TextBox2.Text = Read.ReadLine
            MainForm.DateTimePicker1.Text = Read.ReadLine
            MainForm.TextBox3.Text = Read.ReadLine
            MainForm.TextBox4.Text = Read.ReadLine
            MainForm.TextBox5.Text = Read.ReadLine
            MainForm.TextBox6.Text = Read.ReadLine
            MainForm.TextBox7.Text = Read.ReadLine
            MainForm.TextBox8.Text = Read.ReadLine
            MainForm.TextBox9.Text = Read.ReadLine
            MainForm.TextBox10.Text = Read.ReadLine
            'Open MoldInfo Items
            MainForm.TextBox12.Text = Read.ReadLine
            MainForm.TextBox13.Text = Read.ReadLine
            MainForm.TextBox17.Text = Read.ReadLine
            MainForm.TextBox18.Text = Read.ReadLine
            MainForm.ComboBox3.Text = Read.ReadLine
            MainForm.ComboBox4.Text = Read.ReadLine
            MainForm.ComboBox5.Text = Read.ReadLine
            MainForm.ComboBox6.Text = Read.ReadLine
            MainForm.TextBox19.Text = Read.ReadLine
            MainForm.TextBox20.Text = Read.ReadLine
            MainForm.TextBox21.Text = Read.ReadLine
            MainForm.ComboBox1.Text = Read.ReadLine
            MainForm.ComboBox2.Text = Read.ReadLine
            MainForm.ComboBox8.Text = Read.ReadLine
            MainForm.ComboBox7.Text = Read.ReadLine
            MainForm.TextBox14.Text = Read.ReadLine
            MainForm.TextBox15.Text = Read.ReadLine
            MainForm.TextBox16.Text = Read.ReadLine
            MainForm.TextBox22.Text = Read.ReadLine






            'Open Note Pad Comments
            MainForm.RichTextBoxNotes.Text = Read.ReadToEnd


            'Load Images
            If ok4.Text = "1" Then

                Dim file_name As String
                file_name = OpenImgDir.Text & "\PartImage.jpg"
                Dim bm1 As New Bitmap(file_name)
                MainForm.PartImg.Image = bm1


                Close()

            End If

            Read.Close()
        Catch ex As Exception
        End Try


     



    End Sub



This is the part that allows for overwriting (and some other things):

Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Project_Management.Show()
    End Sub

    'Menu Strip Commands

    'Open Configuration Settings
    Private Sub ConfigurationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigurationToolStripMenuItem.Click
        Configuration.Show()
    End Sub
    'Open Calculator
    Private Sub CalculatorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculatorToolStripMenuItem.Click
        Calculator.Show()
    End Sub
    'Open Coverter
    Private Sub UnitConverterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnitConverterToolStripMenuItem.Click
        Convert.Show()
    End Sub
    'Open About
    Private Sub AboutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        About.Show()
    End Sub
    'Save As
    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        Project_Management.Show()
    End Sub
    'Open Project
    Private Sub OpenProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProjectToolStripMenuItem.Click
        Project_Management.Show()
    End Sub
    'Save
    Private Sub SaveProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveProjectToolStripMenuItem.Click
        'Verify Project

        'Save Txt File
        Try
            Dim Write As New System.IO.StreamWriter(Project_Management.fullpath.Text & "\" & Project_Management.projectName.Text & ".txt")
            'Save Configuration Settings
            Write.WriteLine(Configuration.ComboBox1.Text)
            Write.WriteLine(Configuration.ComboBox2.Text)
            Write.WriteLine(Configuration.ComboBox3.Text)
            Write.WriteLine(Configuration.ComboBox4.Text)
            Write.WriteLine(Configuration.ComboBox5.Text)
            Write.WriteLine(Configuration.ComboBox6.Text)


            'Save InfoForm Items
            Write.WriteLine(TextBox1.Text)
            Write.WriteLine(TextBox2.Text)
            Write.WriteLine(DateTimePicker1.Text)
            Write.WriteLine(TextBox3.Text)
            Write.WriteLine(TextBox4.Text)
            Write.WriteLine(TextBox5.Text)
            Write.WriteLine(TextBox6.Text)
            Write.WriteLine(TextBox7.Text)
            Write.WriteLine(TextBox8.Text)
            Write.WriteLine(TextBox9.Text)
            Write.WriteLine(TextBox10.Text)
            'Save Mold Info Items
            Write.WriteLine(TextBox12.Text)
            Write.WriteLine(TextBox13.Text)
            Write.WriteLine(TextBox17.Text)
            Write.WriteLine(TextBox18.Text)
            Write.WriteLine(ComboBox3.Text)
            Write.WriteLine(ComboBox4.Text)
            Write.WriteLine(ComboBox5.Text)
            Write.WriteLine(ComboBox6.Text)
            Write.WriteLine(TextBox19.Text)
            Write.WriteLine(TextBox20.Text)
            Write.WriteLine(TextBox21.Text)
            Write.WriteLine(ComboBox1.Text)
            Write.WriteLine(ComboBox2.Text)
            Write.WriteLine(ComboBox8.Text)
            Write.WriteLine(ComboBox7.Text)
            Write.WriteLine(TextBox14.Text)
            Write.WriteLine(TextBox15.Text)
            Write.WriteLine(TextBox16.Text)
            Write.WriteLine(TextBox22.Text)








            'Save Note Pad
            Write.Write(RichTextBoxNotes.Text)
            Write.Close()
        Catch ex As Exception
        End Try


        'Save Images
        If PartImg.Image Is Nothing Then

        Else
            If OpenSave.Text = "Opened" Then

                'Dim PartImgFile As String = ImagePathOpened.Text & "\PartImage.jpg"

                'If File.Exists(PartImgFile) Then
                'File.Delete(PartImgFile)
                'End If


                PartImg.Image.Save(ImagePathOpened.Text & "\PartImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)





            Else
                PartImg.Image.Save(ImagePathSaved.Text & "\PartImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)


            End If





        End If




    End Sub

    'New
    Private Sub NewProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewProjectToolStripMenuItem.Click
        'Clear Text Boxes
        'Set Configuration Settings
        ComboBox1.Text = "Inches"
        ComboBox2.Text = "Square Inch"
        ComboBox3.Text = "Cubic Inch"
        ComboBox4.Text = "Ounce"
        ComboBox5.Text = "Fahrenheit"
        ComboBox6.Text = "GPM"
        'Clear GeneralInfo Items
        TextBox1.Text = ""
        TextBox2.Text = ""
        DateTimePicker1.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
        RichTextBox1.Text = ""
        TextBox10.Text = ""
        'Clear MoldInfo Items
        TextBox12.Text = ""
        TextBox13.Text = ""
        TextBox17.Text = ""
        TextBox18.Text = ""
        ComboBox3.Text = ""
        ComboBox4.Text = ""
        ComboBox5.Text = ""
        ComboBox6.Text = ""
        TextBox19.Text = ""
        TextBox20.Text = ""
        TextBox21.Text = ""
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        ComboBox8.Text = ""
        ComboBox7.Text = ""
        TextBox14.Text = ""
        TextBox15.Text = ""
        TextBox16.Text = ""
        TextBox22.Text = ""
        RichTextBox2.Text = ""





        'Clear Images
        PartImg.Image = Nothing

    End Sub

    'General Information Sheet
    'Load Part Image
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Part_Image.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*"
        Part_Image.InitialDirectory = "C:\"
        Part_Image.ShowDialog()
        PartImg.ImageLocation = Part_Image.FileName
        Me.Text = Part_Image.SafeFileName
    End Sub





    'Load Notes To Note Pad
    'General Info Change
    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        'Dims
        Dim GenNotes As String
        GenNotes = "General Project Information:" + Environment.NewLine + Environment.NewLine + RichTextBox1.Text
        Dim MoldNotes As String
        MoldNotes = "Mold Information:" + Environment.NewLine + Environment.NewLine + RichTextBox2.Text
        Dim SingSpace As String
        SingSpace = Environment.NewLine + Environment.NewLine
        Dim DoubSpace As String
        DoubSpace = Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine
        'Text Load
        RichTextBoxNotes.Text = GenNotes + DoubSpace + MoldNotes
    End Sub
    'Mold Info Change
    Private Sub RichTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox2.TextChanged
        'Dims
        Dim GenNotes As String
        GenNotes = "General Project Information:" + Environment.NewLine + Environment.NewLine + RichTextBox1.Text
        Dim MoldNotes As String
        MoldNotes = "Mold Information:" + Environment.NewLine + Environment.NewLine + RichTextBox2.Text
        Dim SingSpace As String
        SingSpace = Environment.NewLine + Environment.NewLine
        Dim DoubSpace As String
        DoubSpace = Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine
        'Text Load
        RichTextBoxNotes.Text = GenNotes + DoubSpace + MoldNotes
    End Sub


    'Mold Information Sheet

    'Set Mold Number
    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        TextBox11.Text = TextBox5.Text
    End Sub
    'Set Cavity Number and Configuration
    Private Sub ComboBox6_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox6.SelectedIndexChanged
        If ComboBox6.Text = "Standard" Then
            Label22.Visible = True
            Label24.Visible = False
            Label25.Visible = False
            TextBox19.Visible = True
            TextBox20.Visible = False
            TextBox21.Visible = False
        End If
        If ComboBox6.Text = "Shuttle" Then
            Label22.Visible = True
            Label24.Visible = False
            Label25.Visible = True
            TextBox19.Visible = True
            TextBox20.Visible = False
            TextBox21.Visible = True
        End If
        If ComboBox6.Text = "Horizontal Multi Shot" Then
            Label22.Visible = True
            Label24.Visible = False
            Label25.Visible = True
            TextBox19.Visible = True
            TextBox20.Visible = False
            TextBox21.Visible = True
        End If
        If ComboBox6.Text = "Spin Stack Multi Shot" Then
            Label22.Visible = True
            Label24.Visible = False
            Label25.Visible = True
            TextBox19.Visible = True
            TextBox20.Visible = False
            TextBox21.Visible = True
        End If
        If ComboBox6.Text = "Family" Then
            Label22.Visible = False
            Label24.Visible = True
            Label25.Visible = False
            TextBox19.Visible = False
            TextBox20.Visible = True
            TextBox21.Visible = False
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.Text = "Other" Then
            Label30.Visible = True
        Else
            Label30.Visible = False
        End If
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox2.Text = "Other" Then
            Label31.Visible = True
        Else
            Label31.Visible = False
        End If
    End Sub
    Private Sub ComboBox7_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox7.SelectedIndexChanged
        If ComboBox7.Text = "Yes" Then
            Label32.Visible = True
        Else
            Label32.Visible = False
        End If
    End Sub
    Private Sub ComboBox8_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox8.SelectedIndexChanged
        If ComboBox8.Text = "Other" Then
            Label34.Visible = True
        Else
            Label34.Visible = False
        End If
    End Sub



End Class


Was This Post Helpful? 0
  • +
  • -

#15 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Coding File Delete

Posted 17 June 2010 - 03:21 PM

If you dispose bm1 after setting the MainForm.PartImg.Image to bm1, then it should all work out.

  Dim file_name As String
  file_name = OpenImgDir.Text & "\PartImage.jpg"
  Dim bm1 As New Bitmap(file_name)
  MainForm.PartImg.Image = bm1
  bm1.Dispose


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2