[VB .NET] how to rename a file correctly?

Discussion in 'Mixed Languages' started by Paky89, Jan 19, 2015.

  1. Paky89

    Paky89 MDL Novice

    Aug 27, 2012
    12
    10
    0
    #1 Paky89, Jan 19, 2015
    Last edited by a moderator: Apr 20, 2017
    Hello everyone,
    I tried various ways to be able to rename the file "%windir%\system32\oemlogo.bmp" in Windows XP, but all failed.
    I do not know what depends so I ask you for help.


    this is my code

    Code:
    
    
        Private Sub DelFile()
            Dim FilePath As String = Environment.GetEnvironmentVariable("systemroot").ToCharArray(0, 3) & "Windows\System32\oemlogo.bmp"
            If My.Computer.FileSystem.FileExists(FilePath) Then
                System.IO.File.SetAttributes(FilePath, IO.FileAttributes.Normal)
                PermessoFile(FilePath)
                My.Computer.FileSystem.RenameFile(FilePath, FilePath & ".bak")
            End If
        End Sub
    
        Private Sub PermessoFile(ByVal FilePath As String)
            Dim sUserAccount As String = Environment.UserDomainName & "\" & Environment.UserName
            Dim oFileInfo As IO.FileInfo = New IO.FileInfo(FilePath)
            Dim oFileAcl As New System.Security.AccessControl.FileSecurity(FilePath, System.Security.AccessControl.AccessControlSections.Access)
            oFileAcl.AddAccessRule(New System.Security.AccessControl.FileSystemAccessRule(sUserAccount, _
                                                              System.Security.AccessControl.FileSystemRights.FullControl, _
                                                              System.Security.AccessControl.AccessControlType.Allow))
            oFileInfo.SetAccessControl(oFileAcl)
            oFileAcl = Nothing
            oFileInfo = Nothing
        End Sub
    
    
    Thanks to all.
     
  2. jellybelly

    jellybelly MDL Member

    Oct 30, 2009
    159
    29
    10
    If you looked closely at your debug output you would most likely see

    This error is not thrown by default so it just fails silently, if you go to Debug > Exceptions > Search > System.ArgumentException and put a check mark in Thrown then run the code again it will exception out on that line and give you the relevant info you need to fix it


    So your problem is that you can't use FilePath & ".bak" as the argument.
     
  3. Paky89

    Paky89 MDL Novice

    Aug 27, 2012
    12
    10
    0
    i apologize. I solved.
    I was unable to rename the file because it was using to display it in a picturebox.
    thanks for your help.
    you are very kind.