Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

How to get directory value as null when no filepath is set

(0) ShareShare
ReportReport
Posted on by

Hi experts,

I have  a table where i am capturing 2 filepath location.

The following code  is to ensure that error is thrown on an event when folder location is not set. This works fine for else loop  but not for if loop.

When setting the folder validated in if condition as blank I just get CLR object not initialized error message, "Object 'CLRObject' could not be created"  .

Please suggest a fix so that i can get it to work just like my else validation where i get full error message of else condition.

public void run()
{

    System.IO.DirectoryInfo     directory;
    System.IO.FileInfo[]        files;
    System.IO.FileInfo          file;
    InteropPermission           permission;
    counter                     filesCount;
    FilePath                    processedFilePath,processedFileCompletePath;
    Filename                    filename;
    Filename                    tmpFilePath = System.IO.Path::GetTempPath();
    Filename                    tmpFileNameShort;
    Filename                    tmpFileExt;
    int                         fromLine = infologLine();
    counter                     loop;
    str                         fileNameTemp,fileNameString;
    boolean                     skipZeroTrans;
    str                         cLRExceptionMsg;

    #FILE

    try
    {
      
        permission  = new InteropPermission(InteropKind::ClrInterop);
        permission.assert();

        select   myTable;

        directory   = new System.IO.DirectoryInfo(@myTable.1stFilepath);   //GET Object 'CLRObject' could not be created as soon as it hits this line in debugger
        processedFilePath = @myTable.2ndFilepath;                         // WHEN testing this condition it works as expected, i.e go to else loop get the error details go in catch call my log write message & write details in there.

        if ( directory == null || processedFilePath == '')
        {
            if (directory == null)
            {
               throw error  ('Error 1 details');
            }

            else
            {
                throw error  ('Error 2 details');
            }
        }



} // Try ends

    catch 
    {
        this.myWriteLogMethod(tmpFilePath,tmpFileNameShort,fromLine);
        throw error(strfmt("@SYS63093",funcName()));
    }
}//Run method ends


  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,091 on at
    RE: How to get directory value as null when no filepath is set

    Hi Mav,

    I think I just understood your issue :-)

    I guess the only way is to wrap DirectoryInfo into try-catch statement as construct method throws an error if it's filepath is not correct (but it doesn't check if the path exists) if you don't want to compare filepath with empty string 

    DirectoryInfo Class (System.IO) | Microsoft Docs

    ..
    
    try
    {
        directory   = new System.IO.DirectoryInfo(myTable.1stFilepath);
    }
    catch
    {
        
    }
    
    ...

  • Mav Profile Picture
    on at
    RE: How to get directory value as null when no filepath is set

    Hi

    X is directory  & check mark is process filepath.

    pastedimage1606151332226v1.png

    Debugger watch window 

    pastedimage1606151478037v2.png

    Error message when trying to set directory as blank , before reaching to info (directory.tostring()) code throws clr object error.

    pastedimage1606151586284v3.png

  • Sergei Minozhenko Profile Picture
    23,091 on at
    RE: How to get directory value as null when no filepath is set

    Hi Mav,

    Can you add info statements to show values from table fields to see if they are selected correctly before the directory object is created?  

    I tried both ways and it's working fine if the path is correct (i used c:\temp and folder inside temp)

  • Mav Profile Picture
    on at
    RE: How to get directory value as null when no filepath is set

    @Moderator please undo reported as abusive for this post , it was clicked accidentally.

    Also will be nice to give option to user to undo report post as abusive when clicked accidentaly.

    @Sergi Minozhenko

    I can fix this by using filepath instead of directory for filepath1, however intrigue to know & make it work as directory.

    Now when i debug after entring the values for fodler to check file path format  i see the values like this, ideally when no filepath is selected for filepath1 it should return null & set Directory as Null that would fix the issue.

    Directory = C:\Test

    Filepath = "C:\Test\2ndfilepath"

    Also please suggest how to use Uri class in Ax if it has to be used to get it working.

    Thanks

    Mav.

  • Sergei Minozhenko Profile Picture
    23,091 on at
    RE: How to get directory value as null when no filepath is set

    Hi Mav,

    Is 1stFilepath a well-formed path?

    You can try to check it with Uri class before using in DirectoryInfo

    docs.microsoft.com/.../system.uri.iswellformedoriginalstring

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,758 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans