Permission Denied When Trying To Delete A File
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Permission Denied When Trying To Delete A File

  1. #1
    Join Date
    Jan 2009
    Posts
    9

    Permission Denied When Trying To Delete A File

    I'm using the following code to copy some lines from file into a new file. After all the correct lines are copied, I delete the old file. This was working just fine last week. But suddenly it's not working on several computers and I don't know why it's not working.

    Code:
    Public Function DeleteLines(ByVal from_name As String, ByVal to_name As String, ByVal target As String) As Long
        Dim strlen As Integer
        Dim from_file As Integer
        Dim to_file As Integer
        Dim one_line As String
        Dim deleted As Integer
    
        ' Open the input file.
        from_file = FreeFile
        Open from_name For Input As from_file
    
        ' Open the output file.
        to_file = FreeFile
        Open to_name For Output As to_file
    
        ' Copy the file skipping lines containing the
        ' target.
        deleted = 0
        
        Do While Not EOF(from_file)
            Line Input #from_file, one_line
            If InStr(one_line, target) > 0 Then
                Print #to_file, one_line
            Else
                deleted = deleted + 1
            End If
        Loop
    
        ' Close the files.
        Close from_file
        Close to_file
        
        MsgBox ("before kill")
        Kill (from_name)
          MsgBox ("after kill")
        Name to_name As from_name
        
        DeleteLines = deleted
    End Function
    It fails on the Kill (from_name) line. It says that it doesn't have permission. I'l closing the file before killing, so I don't know what the issue is.

    Anyone have any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Permission Denied When Trying To Delete A File

    Try running the EXE as Administrator. (You didn't say which OS you're targeting)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Permission Denied When Trying To Delete A File

    Is the computer on a Domain ??? if the domain admin decided to change some of the access policy's this could cause the sudden failure..

    Is the user using the same account as before ??
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #4
    Join Date
    Jan 2009
    Posts
    9

    Re: Permission Denied When Trying To Delete A File

    Quote Originally Posted by dglienna View Post
    Try running the EXE as Administrator. (You didn't say which OS you're targeting)
    UAC is turned off.

    Quote Originally Posted by GremlinSA View Post
    Is the computer on a Domain ??? if the domain admin decided to change some of the access policy's this could cause the sudden failure..

    Is the user using the same account as before ??
    Security settings are valid.

    I can delete the file on form_load, but I can't delete it inside of the method. Even If I put it as the first line in the method, it still won't delete.

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Permission Denied When Trying To Delete A File

    UAC is turned off.
    Narrows it down to the last 4. Or not counting XP? Brilliant.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Permission Denied When Trying To Delete A File

    Quote Originally Posted by weirddemon View Post
    Security settings are valid.

    I can delete the file on form_load, but I can't delete it inside of the method. Even If I put it as the first line in the method, it still won't delete.
    Then somewhere in the code the file is still held open (or the file is open in another proggy)
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Permission Denied When Trying To Delete A File

    I'm going to get shot for saying this, have you considered using DoEvents just after you closed your file, then, after DoEvents use kill?

  8. #8
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,853

    Re: Permission Denied When Trying To Delete A File

    I was posting in this thread on the other forum, turns out he had some other code that is not shown here which opened the file but had not yet closed it when the code here was called. We missed that because the problem code was never shown so all we could do is guess at drive permissions and such.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center