![]() |
#1 |
Участник
|
Hello,
We have had some partner suggestion for adding Unicode capabilities to the existing Microsoft Dynamics NAV File functions. What we recommend is to use .NET Interop to achieve this functionality. For example, you can use an instance of the System.IO.StreamWriter class to write to an OutStream or the System.IO.StreamReader class to read from an InStream. You control the encoding by using the System.Text.Encoding class where you select between Default, Unicode or UTF8 encoding.
Let’s start with a small example on writing some text to a file in Unicode format. Declare the following variables: NameDataTypeSubtypeoutFileFile outStreamOutStream streamWriterDotNetSystem.IO.StreamWriter.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' encodingDotNetSystem.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Then write this code, using a suitable path for the outFile: outFile.CREATE('c:tempOutFile.txt'); outFile.CREATEOUTSTREAM(outStream); streamWriter := streamWriter.StreamWriter(outStream, encoding.Unicode); streamWriter.WriteLine('Hello World'); streamWriter.Close(); outFile.CLOSE();
Please note that if you use C/AL to write directly to the outStream, like this: outStream.WRITETEXT('Hello World');this is still handled using MS-DOS encoding and is compatible with previous versions of Microsoft Dynamics NAV.
Let’s see how you can extend the above example with data from the Customer table: Add the customer variable: NameDataTypeSubtype customerRecord CustomerAnd write this code – you can set a filter if needed: outFile.CREATE('c:tempCustomers.txt'); outFile.CREATEOUTSTREAM(outStream); streamWriter := streamWriter.StreamWriter(outStream, encoding.Unicode); customer.FINDSET(); REPEAT streamWriter.WriteLine(customer.Name); UNTIL customer.NEXT = 0; streamWriter.Close(); outFile.CLOSE(); If you open the Customers.txt file with a Unicode file viewer it should contain all the Unicode details you have used for your customer names. Reading Unicode text files Similar to the above you can read Unicode text files by using the System.IO.StreamReader class to read from an InStream as you can see in the small example below: Declare the following variables: NameDataTypeSubtypeinFileFile inStreamInStream streamReaderDotNetSystem.IO.StreamReader.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'encodingDotNetSystem.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'txtText Then write this code, using a suitable path for the inFile: inFile.OPEN('c:tempInfile.txt'); inFile.CREATEINSTREAM(inStream); streamReader := streamReader.StreamReader(inStream,encoding.Unicode); txt := streamReader.ReadToEnd(); message(txt); Create the Infile.txt as a Unicode file and add some Unicode characters to it. You should see them in the message box when you run the C/AL code. I hope these simple examples can get you started with Unicode file handling in C/AL. Thanks and best regards! Hans Kierulff Источник: http://feedproxy.google.com/~r/Micro...-nav-2013.aspx
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|