objFso.MoveFile Gives Error: CTL_E_PERMISSIONDENIED

I can rename a .jpg file with the windows explorer but can't do it from my program:

    Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click
        Dim objFso As Object

        objFso = CreateObject("Scripting.FileSystemObject")

        If objFso.FileExists(ofdImage.FileName) Then
            objFso.MoveFile(ofdImage.FileName, txtFileName.Text)
        End If

        objFso = Nothing

    End Sub

The above gives the error:

   An unhandled exception of type 'System.Security.SecurityException' occurred in microsoft.visualbasic.dll

   Additional information: Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED).

However:

        objFso.CopyFile(ofdImage.FileName, txtFileName.Text) 'this copies the file, no problem

works fine!!!

Also doesn't work:

        objFso.DeleteFile(ofdImage.FileName, txtFileName.Text) 'this copies the file, no problem

...or otherwise I would copy, then delete the original.

Thanks,

Alex
amaximiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you possibly viewing the image in a PictureBox when you are attempting this operation?  There may be a file lock on the image preventing it from being renamed/deleted but allowing a copy to be made.

~IM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amaximiaAuthor Commented:
That's exactly what was happening.  Thank you!
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just FYI...you can use code like this to load an image at runtime and prevent a file lock on the image at the same time:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim tempImg As Image = Image.FromFile(OpenFileDialog1.FileName)
            PictureBox1.Image = New Bitmap(tempImg)
            tempImg.Dispose()
        End If
    End Sub

Without an OpenFileDialog:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileName As String = "c:\someImage.jpg"
        Dim tempImg As Image = Image.FromFile(fileName)
        PictureBox1.Image = New Bitmap(tempImg)
        tempImg.Dispose()
    End Sub

~IM
0
amaximiaAuthor Commented:
Thanks, I'll give that a try.  After trying many things I'd given up.  I new the problem just not how to fix it.  But I'll try your suggestion and let you know the results sometime later today.  Thanks again.
0
amaximiaAuthor Commented:
It worked!  Hurray!!!  Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.