Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Error While Accessing a shared folder using x++

Posted on by 25

System.IO.IOException: The user name or password is incorrect.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.CommonInit()
at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
at System.IO.DirectoryInfo.InternalGetFiles(String searchPattern, SearchOption searchOption)
at Dynamics.Ax.Application.Loopdirectory() in a.xpp:line 22
at Dynamics.Ax.Application.Run() in a.run.xpp:line 6
at Dynamics.Ax.Application.BatchRun.runJobStaticCode(Int64 batchId) in BatchRun.runJobStaticCode.xpp:line 54
at Dynamics.Ax.Application.BatchRun.runJobStatic(Int64 batchId) in BatchRun.runJobStatic.xpp:line 13
at BatchRun::runJobStatic(Object[] )
at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)
at BatchIL.taskThreadEntry(Object threadArg)

Running a job in batch job i am getting this error 

directory = new System.IO.DirectoryInfo(@"XXXXXX");
files = directory.GetFiles();
filesCount = files.get_Length();

  • Suggested answer
    Fangyuan Hou Profile Picture
    Fangyuan Hou on at
    RE: Error While Accessing a shared folder using x++

    Hi Axbeginerss,

    You are saying that It is working fine when we run the batch job manually, are you implement this code in a class which extends RunBaseBatch? Which user is run the batch job?

    If yes, have you implement runsImpersonated method to return true?

    docs.microsoft.com/.../batch-processing-classes

    If still not working, maybe can you try to add below line before GetFiles() to see whether it works?

    #File

    new FileIOPermission(folder, #io_read).assert();

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Error While Accessing a shared folder using x++

    You're not using AX only. System.IO.DirectoryInfo class is defined in .NET Framework (see documentation). That you're using interoperability with something else and not just AX is also obvious from InteropPermission.

    By the way, please use Insert > Insert Code (int the rich-formatting view) to paste source code. It makes code easier to read and work with.

  • Axbeginerss Profile Picture
    Axbeginerss 25 on at
    RE: Error While Accessing a shared folder using x++

    It is working fine when we run the batch job manually

    but when running in batch it is throwing the above error.

  • Axbeginerss Profile Picture
    Axbeginerss 25 on at
    RE: Error While Accessing a shared folder using x++

    System.IO.DirectoryInfo     directory;

       System.IO.FileInfo[]        files;

       System.IO.FileInfo          file;

       InteropPermission           permission;

       Filename                    tmpFilePath;

       Filename                    tmpFileNameShort;

       Filename                    tmpFileExt;

       str         fileNameTemp;

       counter     filesCount;

       counter     loop;

       permission  = new InteropPermission(InteropKind::ClrInterop);

       permission.assert();

       directory   = new System.IO.DirectoryInfo(@"");

       files       = directory.GetFiles();

       filesCount  = files.get_Length();

       for (loop = 0; loop < filesCount; loop++)

       {

           file                                        = files.GetValue(loop);

           fileNameTemp                                = file.get_FullName();

           [tmpFilePath, tmpFileNameShort, tmpFileExt] = fileNameSplit(fileNameTemp);

           this.importExcel(tmpFileNameShort);

       }

       CodeAccessPermission::revertAssert();

    i am using in Ax only. Martin !!

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Error While Accessing a shared folder using x++

    Your question is about a .NET API, it doesn't seem specific to Dynamics AX, therefore you should look at .NET resources rather then AX resources. This even isn't the best forum.

    Look at C#: How to logon to a share when using DirectoryInfo (at StackOverflow), for example.

    Nevertheless a better approach would be putting the file somewhere where it can be read. If you run your code on server, it's the AOS service account that needs permissions.

  • Axbeginerss Profile Picture
    Axbeginerss 25 on at
    RE: Error While Accessing a shared folder using x++

    directory = new System.IO.DirectoryInfo(@"XXXXXX");

    files = directory.GetFiles();

    getting the error at files = directory.GetFiles();

    but every time i am accessing the file using Wind+R  it is asking username and password. how to pass username and password through code. ??

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans