AX 2009: System.IO.File::Move ClrObject static method invocation error.

This question is answered

Hi,

I encountered the following error when I'm trying to run a batch job with a process of Moving File:

 

ClrObject static method invocation error.

System.UnauthorizedAccessException: Access to the path is denied.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

   at System.IO.File.Move(String sourceFileName, String destFileName)

 

 

Here's my code and it's working fine only in client:

static client server boolean MoveFile(FileName _srcFile, FileName _destFile, boolean _overwrite = true)

{

    System.Exception ex;

    Set permSet = new Set(Types::Class);

    str                     errorMsg;

    ;


    try

    {        

        permSet.add(new FileIOPermission(_srcFile, 'rw'));

        permSet.add(new InteropPermission(InteropKind::ClrInterop));

        permSet.add(new FileIOPermission(_destFile, 'rw'));


        CodeAccessPermission::assertMultiple(permSet);


        if(System.IO.File::Exists(_destFile))

            System.IO.File::Delete(_destFile);


        System.IO.File::Move(strfmt(@"%1",_srcFile), strfmt(@"%1",_destFile));

        CodeAccessPermission::revertAssert();

    }

    catch (Exception::CLRError)

    {

        ex = ClrInterop::getLastException();

         if (ex != null)

         {

            ex = ex.get_InnerException();

            if (ex != null)

            {

                error(ex.ToString());

            }

        }

    }

    return true;

}

 

I wonder what I'm missing here. Any help will be appreciated! Thanks

 

Verified Answer
  • I guess the user running the AOS does not have write permission in this folder. That permission must be set in Windows, granting a FileIOPermission does not help if Windows does not allow access to the path.

All Replies
  • I guess the user running the AOS does not have write permission in this folder. That permission must be set in Windows, granting a FileIOPermission does not help if Windows does not allow access to the path.

  • Hi, Thanks for the quick reply. Yes, Granting Folder Permissions work.

  • I addition to what Jonas said:

    Please have in mind that server-side code is executed in context of windows account/domain account of the AOS service, while code on AX client acts on behalf of current user.

    This scenario can lead to some non-obvious errors like above.

    Regards

    Michal

    --

    http://aximprove.co.uk/

    Michal Kupczyk

    aximprove.co.uk   :   Triage,Code Scrambler, Licence Guard, Advanced Data Management for SQL Server

  • Hi,

    Is it possible to assign the file access credentials through code (if these are known)?  I have a similar situation, where the files that I am accessing are not on a server within the domain, but I have been provided the user name and password for this folder.  

    So for my class to run, i need to first log in to the folder, and then execute the class.  This can be done easily on the client, but all the functions that do this are client only, and fail when the class is run as a batch.

    Meherab

  • Hi Meherab,

    I think it's best if you just grant folder permission to Full Control to the account of your AOS. For example, the execution account of my AOS is NETWORK SERVICE, so I always see to it that my folder grants full control to NETWORK SERVICE.

    Lyka Tasis

  • Hi Lyka,

    The main issue for me is that I am not allowed to access the domain form the external server, (which is not on the same domain - actually it is not on any domain).  

    As a client process, i am using winAPI::ShellExecute to run a cmd file that will pass the credentials to the external server (using NET USE).

    The trouble is i cannot use winAPI::ShellExecute when I am running the process as a Batch task.

    Meherab