tags:

views:

43

answers:

2

Hi, when i use MoveFileEx to move files in C drive, but i am getting the ERROR that ACCESS DENIED. Any solutions

int i ; DWORD dw ; String^ Source = "C:\Folder\Program\test.exe" ; String^ Destination = "C:\test.exe"; // move to program Files Folder

    pin_ptr<const wchar_t> WSource = PtrToStringChars(Source);
  pin_ptr<const wchar_t> WDestination = PtrToStringChars(Destination);

i = MoveFileEx( WSource, WDestination ,MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED ) ; dw = GetLastError() ;

A: 

You need to make sure that the user account your process runs under has read access to the file being moved and write access to where it's being written to. And that the file being moved isn't locked by another process and that there isn't a file with the same name in the destination directory that's locked by another process.

Try moving the same file manually in Windows Explorer and see what errors you get, when you can do that without problems your app will probably work too (assuming they're running under the same account).

ho1
I tried moving manually, i got the error that i got to have the admin permission to move this folder. But i am logged in as admin, and i am using win7.The file i am moving is a exe file. it's not involved in any process.
rajivpradeep
Are you trying to move the file to the root of `c:\`? This is disabled by default in Win7 I think, and you should reconsider and put it somewhere else. If you do really want to move it there, you can start explorer with "Run as Admin" or what it's called and should then be able to do it (and you could then give permissions to yourself to move files there so that your app would work).
ho1
I am trying to move from here "C:\Folder\Program\test.exe" to here "C:\test.exe".
rajivpradeep
Yep, that's the problem, you're not really supposed to do that, you shouldn't put programs in `C:\`. I suggest creating a Temp or Test folder in `C:\` and use that instead? Otherwise do as I said in my previous comment and you can probably do it.
ho1
Ok, i'll try that.. n i'll come back
rajivpradeep
you are are simply awesome, it worked. i can move it to temp folder but not in that guarded Program Files folder. Thank you very much.
rajivpradeep
A: 

is the code posted in your question the real used code ???

if it is so, you are having a problem with your filenames. \ is the escape character in C and C++ strings, it should be doubled if you want a real \ character in the resulting string.

so your pathes should be:

String source = "C:\\Folder\\Program\\test.exe";
String Destination = "C:\\test.exe";

also, ^ is not a valid character in C and C++, it is only valid for defining pointers in Pascal. i suspect your code is really written in Pascal, but then i am unsure if the above remark about the escape character in string is valid in Pascal...

Adrien Plisson
^ is a valid character in C++/CLI. I see pin_ptr so I am confident this is C++/CLI code. Your \ comment is valid.
Kate Gregory
ha sorry, i forgot that awful beast called C++/CLI (which really has not much in common with real C++)... you are right, this is definitely not Pascal.
Adrien Plisson