Writing file in Client directory using DotNet Assemblies

Question Status

Verified
Preetham Poojari asked a question on 17 Jul 2017 5:44 AM

Hi All,

There is a need for development for reading and writing the file in Client directory in NAV 2013 R2 and later will be required in other versions too. Since the 'File' variable File.CREATE and WRITE functions create file in NAV Server, I am using following DOTNET assemblies:

1. DotNet System.IO.File.'mscorlib'
2. DotNet System.IO.Directory.'mscorlib'
3. DotNet System.IO.FileStream.'mscorlib'

I've also set the RunOnClient property to 'Yes' which will create the files in local directory.

However the Dotnet assembly code 'File.Create' creates the file in local directory of client, when the code to File.Write is called NAV throws a lock by another user error. Please let me know the required code, if anybody have writting a code to create file in Local directory using dot net assemblies. 

Reply
Marcell Turi responded on 17 Jul 2017 8:45 AM

HI Preetham,

I would recommend you to use the File Management Codeunit 419 instead of dotnet.

Walkthrough: Transferring a File from Microsoft Dynamics NAV Server to the Client

Reply
Preetham Poojari responded on 18 Jul 2017 7:59 AM

Thanks, But I've resolved it by myself. I will post the solution later.

File management was not a solution to me, as it does not allow to write and read data from file.

In short, I've created all the .Net assembly as 'RunOnClient' and did all the write and read transactions using the mscorlib class file, fileInfo , Streamwriter and StreamReader.

Reply
Verified Answer
Preetham Poojari responded on 18 Jul 2017 8:06 AM

Hi,

The problem is resolved.

You only need the 'DotNet System.IO.File.'mscorlib' ' to create the file in the location. It must store the return value in 'System.IO.FileStream.'mscorlib'' and then close the filestream when created. Later on, use the stream writer to write the data into file.

In my case:

     

CASE ProcessFileL."Character Set" OF

       ProcessFileL."Character Set"::ASCII:

           StreamWriterL := StreamWriterL.StreamWriter(OutputFileNameL,AppendFileL,EncodingL.ASCII);

       ProcessFileL."Character Set"::ANSI:

           StreamWriterL := StreamWriterL.StreamWriter(OutputFileNameL,AppendFileL,EncodingL.Default);

       ELSE

           StreamWriterL := StreamWriterL.StreamWriter(OutputFileNameL,AppendFileL,EncodingL.UTF8);

     END;

     StreamWriterL.Write(BigTextL);

     StreamWriterL.Close;

where 

StreamWriterL = DotNet : System.IO.StreamWriter.'mscorlib'.

Do message me if anyone require further details on file handling. ;)

Reply
Verified Answer
Preetham Poojari responded on 18 Jul 2017 8:06 AM

Hi,

The problem is resolved.

You only need the 'DotNet System.IO.File.'mscorlib' ' to create the file in the location. It must store the return value in 'System.IO.FileStream.'mscorlib'' and then close the filestream when created. Later on, use the stream writer to write the data into file.

In my case:

     

CASE ProcessFileL."Character Set" OF

       ProcessFileL."Character Set"::ASCII:

           StreamWriterL := StreamWriterL.StreamWriter(OutputFileNameL,AppendFileL,EncodingL.ASCII);

       ProcessFileL."Character Set"::ANSI:

           StreamWriterL := StreamWriterL.StreamWriter(OutputFileNameL,AppendFileL,EncodingL.Default);

       ELSE

           StreamWriterL := StreamWriterL.StreamWriter(OutputFileNameL,AppendFileL,EncodingL.UTF8);

     END;

     StreamWriterL.Write(BigTextL);

     StreamWriterL.Close;

where 

StreamWriterL = DotNet : System.IO.StreamWriter.'mscorlib'.

Do message me if anyone require further details on file handling. ;)

Reply