web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Unable to move file using System.IO.File

(0) ShareShare
ReportReport
Posted on by

Am trying to move file from one folder to another using System.IO.File. Unfortunately, it does not work, but when I make use of the copy method, the file replicates itself on the specified directory. See code below for your reference. Thanks in advance.

for(rowNo = 1; rowNo <= conLen(pCountFiles) ; rowNo++)
        {
            fileDetails     = fileNameSplit(conPeek(pCountFiles, rowNo));
            pCountFileName  = conPeek(fileDetails, 2);
            newMoveFileDir  =   strFmt('%1/%2/%3', sharedFolder, pCountProcessFiles, pCountFileName);
            
            permissionSet   =  new Set(Types::Class);
            permissionSet.add(new FileIOPermission(fileName, 'rw'));
            permissionSet.add(new FileIOPermission(newMoveFileDir, 'rw'));
            permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
            CodeAccessPermission::assertMultiple(permissionSet);
            
            if(System.IO.File::Exists(newMoveFileDir))     
                System.IO.File::Delete(newMoveFileDir);
                
            System.IO.File::Move(filename, newMoveFileDir);
            
            CodeAccessPermission::revertAssert();
        }


*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    What exactly do you mean by "it does not work"?

  • Suggested answer
    Chaitanya Golla Profile Picture
    17,225 on at

    Hi,

    Your code looks fine to me, if System.IO.File is not working(for some reason) you can try with

    copyFile method of either Class WinAPI (or) WinAPIServer.

  • Community Member Profile Picture
    on at

    It does not prompt any error. But it works fine when I make use of the Copy method. Also, the Delete method does not also work. My form gets on not responding.

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    I guess the file can't be removed because it's locked and you don't see any error because you forgot to catch the CLR exception.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    You could check for locks and also for file access issues using the same .Net namespace, check the documentation on it.

    Also it might give you errors, you are just not handling it - I cannot see a try-catch block for catching it.

    Implement those and maybe that will reveal the true issue here. Like if file exists or not (trying to read it on client when code is bound to server and vice versa), or if the AX AOS account has no access to the folder, etc.

    Here is an example, you could find more, or just read the MSDN documentation for System.IO namespace.

    community.dynamics.com/.../ax-2012-file-existance-check

  • Community Member Profile Picture
    on at

    The code snippet below is from the SrsReportRunPrinter class toEmail method

    We have been experiencing intermittent failures in our Production environment and have not been able to reproduce it in Stage.

    We discovered that the target folders for the tmp files/directories were very large in size and we believe that our failures are due to folder system latency.

    So I agree with Vilmos that a try-catch block will help isolate the problem. While researching this issue I did see where sometimes File:Delete is not completed in the folder system before File:Move is called so it fails intermittently.

    Our problem actually appears to be with System.IO.Directory::CreateDirectory(tempSubDirectory) and our process is run in batch so I am currently adding a try-catch block and determining how best to get the exception in to an AX infolog.

    This should be completed and tested within the next week or so and I will post the results. Hope that helps and I am also open to suggestions for adding the try-catch blocks.

    if (files != null && files.lastIndex() > 0)
    {
    attachmentFileName = files.value(1);
    fileNameOnly = reportContract.parmReportName() + SRSPrintDestinationSettings::findFileNameType(fileFormat, imageFormat);

    // rename the temp file and move to a unique directory to prevent problems when
    // multiple threads are emailing reports using the same temp directory
    tempSubDirectory = System.IO.Path::Combine(System.IO.Path::GetDirectoryName(attachmentFileName), System.IO.Path::GetFileNameWithoutExtension(attachmentFileName));

    System.IO.Directory::CreateDirectory(tempSubDirectory);

    attachmentPath = System.IO.Path::Combine(tempSubDirectory, fileNameOnly);

    System.IO.File::Move(attachmentFileName, attachmentPath);


    if (this.parmReportRunMailer().emailReport(emailContract,attachmentPath))
    {
    // The report has been successfully sent as attachment to email.
    info("@SYS344685");
    }
    }

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    If it is a lock on a file due to the sequence of executing operations on directories or files around, and the IO subsystem is trying to catch up, you could do the following.

    int i;

    do

    {

       wait(1000);

       i++

    }

    while ((file locked / create directory failure checks) || i == 30)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans