?
Solved

objFso.MoveFile Gives Error: CTL_E_PERMISSIONDENIED

Posted on 2004-11-16
5
Medium Priority
?
1,551 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:amaximia
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 12595890
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
 

Author Comment

by:amaximia
ID: 12596173
That's exactly what was happening.  Thank you!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12600661
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
 

Author Comment

by:amaximia
ID: 12603411
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
 

Author Comment

by:amaximia
ID: 12729818
It worked!  Hurray!!!  Thanks again.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question