<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Art Of Creation - Dynamics AX Blog &#187; System.IO</title>
	<atom:link href="http://www.artofcreation.be/tag/systemio/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artofcreation.be</link>
	<description>The everyday life of a Dynamics AX developer</description>
	<lastBuildDate>Fri, 23 Jul 2010 16:29:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Split big text files in smaller ones in X++</title>
		<link>http://www.artofcreation.be/2010/04/21/split-big-text-files-in-smaller-ones-in-x/</link>
		<comments>http://www.artofcreation.be/2010/04/21/split-big-text-files-in-smaller-ones-in-x/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 16:16:12 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[Job]]></category>
		<category><![CDATA[System.IO]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=703</guid>
		<description><![CDATA[Below is a job I found useful many times, so don&#8217;t want to loose it. Nothing special, but maybe you&#8217;ll find it useful too. It splits an input text file into smaller files. As there is one comment line for every line of code (almost), I&#8217;m sure you&#8217;ll figure it out :-). static void KlForSplitFile&#40;Args [...]]]></description>
			<content:encoded><![CDATA[<p>Below is a job I found useful many times, so don&#8217;t want to loose it.<br />
Nothing special, but maybe you&#8217;ll find it useful too. </p>
<p>It splits an input text file into smaller files.<br />
As there is one comment line for every line of code (almost), I&#8217;m sure you&#8217;ll figure it out :-).</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> KlForSplitFile<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; #File<br />
&nbsp; &nbsp; AsciiIO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inFile<span style="color: #00007f;">,</span> outFile;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">container</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rec;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt <span style="color: #00007f;">=</span> <span style="color: #000000;">0</span><span style="color: #00007f;">,</span> fileNum &nbsp; &nbsp;<span style="color: #00007f;">=</span> <span style="color: #000000;">0</span>;<br />
&nbsp; &nbsp; FileName &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outFileName;<br />
&nbsp; &nbsp; FileIOPermission &nbsp; &nbsp;filePermissionOrig<span style="color: #00007f;">,</span> outFilePermission;<br />
&nbsp; &nbsp; Set &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permissionSet <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> set<span style="color: #000000;">&#40;</span>types<span style="color: #00007f;">::</span><span style="color: #0000ff;">Class</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// settings begin</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// input file</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _inFileName <span style="color: #00007f;">=</span> @<span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\t</span>emp<span style="color: #000000;">\t</span>estsplit<span style="color: #000000;">\t</span>estsplit.csv&quot;</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">// max number of lines</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _maxrec <span style="color: #00007f;">=</span> <span style="color: #000000;">20000</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">//settings end</span><br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">//</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// first loop to create the permission set</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">//</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// assert permission for read</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">new</span> FileIOPermission<span style="color: #000000;">&#40;</span>_inFileName<span style="color: #00007f;">,</span> #io_read<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// create new asciiIo for the file we are reading in</span><br />
&nbsp; &nbsp; inFile &nbsp;<span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> AsciiIo<span style="color: #000000;">&#40;</span>_inFileName<span style="color: #00007f;">,</span>#io_read<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// read file to check how many files will be created</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>inFile.<span style="color: #000000;">read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>cnt <span style="color: #00007f;">==</span> _maxrec <span style="color: #00007f;">||</span> cnt <span style="color: #00007f;">==</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// 1 more file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filenum<span style="color: #00007f;">++</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// create filename</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outFileName <span style="color: #00007f;">=</span> <span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'%1_%2.csv'</span><span style="color: #00007f;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">substr</span><span style="color: #000000;">&#40;</span>_inFileName<span style="color: #00007f;">,</span><span style="color: #000000;">1</span><span style="color: #00007f;">,</span><span style="color: #0000ff;">strlen</span><span style="color: #000000;">&#40;</span>_inFileName<span style="color: #000000;">&#41;</span> <span style="color: #00007f;">-</span> <span style="color: #000000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span> fileNum<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// create permission</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outFilePermission <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> FileIOPermission<span style="color: #000000;">&#40;</span>outFileName<span style="color: #00007f;">,</span>#io_write<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// add permission to set</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permissionset.<span style="color: #000000;">add</span><span style="color: #000000;">&#40;</span>outFilePermission<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// reset line counter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt <span style="color: #00007f;">=</span> <span style="color: #000000;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// 1 more line</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cnt<span style="color: #00007f;">++</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// also add fileIn permission to set</span><br />
&nbsp; &nbsp; filePermissionOrig <span style="color: #00007f;">=</span> &nbsp;<span style="color: #0000ff;">new</span> FileIOPermission<span style="color: #000000;">&#40;</span>_inFileName<span style="color: #00007f;">,</span> #io_read<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; permissionset.<span style="color: #000000;">add</span><span style="color: #000000;">&#40;</span>filePermissionOrig<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">// revert permission assertion</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// assert permissions for set</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">assertMultiple</span><span style="color: #000000;">&#40;</span>permissionset<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">//</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// loop file again and split</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">//</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// reset counters</span><br />
&nbsp; &nbsp; fileNum <span style="color: #00007f;">=</span> <span style="color: #000000;">0</span>;<br />
&nbsp; &nbsp; cnt &nbsp; &nbsp; <span style="color: #00007f;">=</span> <span style="color: #000000;">0</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// create new asciiIo for the file we are reading in</span><br />
&nbsp; &nbsp; inFile &nbsp;<span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> AsciiIo<span style="color: #000000;">&#40;</span>_inFileName<span style="color: #00007f;">,</span>#io_read<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// read file to check how many files will be created</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// read ahead</span><br />
&nbsp; &nbsp; rec <span style="color: #00007f;">=</span> inFile.<span style="color: #000000;">read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>rec<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>cnt <span style="color: #00007f;">==</span> _maxrec <span style="color: #00007f;">||</span> cnt <span style="color: #00007f;">==</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// 1 more file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filenum<span style="color: #00007f;">++</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// create filename</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outFileName <span style="color: #00007f;">=</span> <span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'%1_%2.csv'</span><span style="color: #00007f;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">substr</span><span style="color: #000000;">&#40;</span>_inFileName<span style="color: #00007f;">,</span><span style="color: #000000;">1</span><span style="color: #00007f;">,</span><span style="color: #0000ff;">strlen</span><span style="color: #000000;">&#40;</span>_inFileName<span style="color: #000000;">&#41;</span> <span style="color: #00007f;">-</span> <span style="color: #000000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span> fileNum<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outFile <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> AsciiIO<span style="color: #000000;">&#40;</span>outFileName<span style="color: #00007f;">,</span>#io_write<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// reset line counter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt <span style="color: #00007f;">=</span> <span style="color: #000000;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// 1 more line</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cnt<span style="color: #00007f;">++</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// write to split file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; outFile.<span style="color: #000000;">writeExp</span><span style="color: #000000;">&#40;</span>rec<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// read next line</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rec <span style="color: #00007f;">=</span> inFile.<span style="color: #000000;">read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// revert permission assertion</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'done'</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/04/21/split-big-text-files-in-smaller-ones-in-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get folders and subfolders</title>
		<link>http://www.artofcreation.be/2010/01/08/get-folders-and-subfolders/</link>
		<comments>http://www.artofcreation.be/2010/01/08/get-folders-and-subfolders/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 18:34:13 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[System.IO]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=402</guid>
		<description><![CDATA[Someone on the Microsoft AX newsgroups asked how to get a list of directories and subdirectories given a folder. Here a job that does that: static void KlForLoopFoldersSystemIO&#40;Args _args&#41; &#123; &#160; &#160; int &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; k; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;// counter for result loop [...]]]></description>
			<content:encoded><![CDATA[<p>Someone on the Microsoft AX newsgroups asked how to get a list of directories and subdirectories given a folder. Here a job that does that:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> KlForLoopFoldersSystemIO<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #007f00;">// counter for result loop</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">container</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dirs; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// container for result</span><br />
&nbsp; &nbsp; filePath &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;path <span style="color: #00007f;">=</span> @<span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\t</span>emp&quot;</span>; &nbsp;<span style="color: #007f00;">// input path</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">container</span> getDirectories<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">str</span> _dir<span style="color: #00007f;">,</span> <span style="color: #0000ff;">boolean</span> _inclSubDirs <span style="color: #00007f;">=</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">container</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dList; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #007f00;">// container to cast array into</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #007f00;">// counter for array loop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; System.<span style="color: #000000;">Array</span> &nbsp; &nbsp; &nbsp; &nbsp;directories; &nbsp; &nbsp;<span style="color: #007f00;">// array for result from .NET call</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; #Define.<span style="color: #000000;">Pattern</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #007f00;">// input pattern: * = all</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// assert interoppermissions for .NET interop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">new</span> InteropPermission<span style="color: #000000;">&#40;</span>InteropKind<span style="color: #00007f;">::</span><span style="color: #000000;">ClrInterop</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// get directories using .NET interop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>_inclSubDirs<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// include subdirectories</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; directories <span style="color: #00007f;">=</span> System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">Directory</span><span style="color: #00007f;">::</span><span style="color: #000000;">GetDirectories</span><span style="color: #000000;">&#40;</span>_dir<span style="color: #00007f;">,</span> #Pattern<span style="color: #00007f;">,</span> System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">SearchOption</span><span style="color: #00007f;">::</span><span style="color: #000000;">AllDirectories</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// only top directory</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; directories <span style="color: #00007f;">=</span> System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">Directory</span><span style="color: #00007f;">::</span><span style="color: #000000;">GetDirectories</span><span style="color: #000000;">&#40;</span>_dir<span style="color: #00007f;">,</span> #Pattern<span style="color: #00007f;">,</span> System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">SearchOption</span><span style="color: #00007f;">::</span><span style="color: #000000;">TopDirectoryOnly</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// loog .NET array and put the values in a container for easier use in x++</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #000000;">&#40;</span> i<span style="color: #00007f;">=</span><span style="color: #000000;">0</span>;i<span style="color: #00007f;">&lt;</span>ClrInterop<span style="color: #00007f;">::</span><span style="color: #000000;">getAnyTypeForObject</span><span style="color: #000000;">&#40;</span>directories.<span style="color: #000000;">get_Length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; i<span style="color: #00007f;">++</span> <span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dList <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conins</span><span style="color: #000000;">&#40;</span>dList<span style="color: #00007f;">,</span> <span style="color: #0000ff;">conlen</span><span style="color: #000000;">&#40;</span>dList<span style="color: #000000;">&#41;</span><span style="color: #00007f;">+</span><span style="color: #000000;">1</span><span style="color: #00007f;">,</span> ClrInterop<span style="color: #00007f;">::</span><span style="color: #000000;">getAnyTypeForObject</span><span style="color: #000000;">&#40;</span>directories.<span style="color: #000000;">GetValue</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// revert assertion to prevent multiple calls to the assert() method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// return result container</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> dList;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// get directories</span><br />
&nbsp; &nbsp; dirs <span style="color: #00007f;">=</span> getDirectories<span style="color: #000000;">&#40;</span>path<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// use optional parameter to disable sub directories</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// dirs = getDirectories(path, false);</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// print the result</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #000000;">&#40;</span>k<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>;k<span style="color: #00007f;">&lt;=</span> <span style="color: #0000ff;">conlen</span><span style="color: #000000;">&#40;</span>dirs<span style="color: #000000;">&#41;</span>; k<span style="color: #00007f;">++</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// print the result to screen</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">conpeek</span><span style="color: #000000;">&#40;</span>dirs<span style="color: #00007f;">,</span> k<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// we're done</span><br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;done&quot;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Also check my post about <a href="http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/">how to loop files in a folder</a> (method loopfilesSystemIO)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/01/08/get-folders-and-subfolders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Append text from one file to an other</title>
		<link>http://www.artofcreation.be/2009/10/31/append-text-from-one-file-to-an-other/</link>
		<comments>http://www.artofcreation.be/2009/10/31/append-text-from-one-file-to-an-other/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 07:28:24 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[System.IO]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=310</guid>
		<description><![CDATA[This method will append all text from the original file to the destination file. While this is a very easy task, the method shows many of the things you come across when you are working with files in AX, like: - Using the #File macro - Asserting FileIOPermission to be able to access files - [...]]]></description>
			<content:encoded><![CDATA[<p>This method will append all text from the original file to the destination file.<br />
While this is a very easy task, the method shows many of the things you come across when you are working with files in AX, like:</p>
<p>- Using the #File macro<br />
- Asserting FileIOPermission to be able to access files<br />
- Asserting InteropPermission to be able to use .NET Interop<br />
- Using a set to assert multiple permissions at once<br />
- Using .NET Clr Interop in AX (better than winapi and winapiserver)<br />
- Optional cleaning up after you&#8217;re done using reverAssert()</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">void</span> AppendFileToFile<span style="color: #000000;">&#40;</span>FileName original<span style="color: #00007f;">,</span> FileName distination<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; #File<br />
&nbsp; &nbsp; FileIOPermission &nbsp; &nbsp;FileIOPermissionA &nbsp; <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> FileIOPermission<span style="color: #000000;">&#40;</span>distination<span style="color: #00007f;">,</span> #io_append<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; FileIOPermission &nbsp; &nbsp;FileIOPermissionR &nbsp; <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> FileIOPermission<span style="color: #000000;">&#40;</span>original<span style="color: #00007f;">,</span> #io_read<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; InteropPermission &nbsp; InteropPermission &nbsp; <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> InteropPermission<span style="color: #000000;">&#40;</span>InteropKind<span style="color: #00007f;">::</span><span style="color: #000000;">ClrInterop</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; Set &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permissionset &nbsp; &nbsp; &nbsp; <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> set<span style="color: #000000;">&#40;</span>types<span style="color: #00007f;">::</span><span style="color: #0000ff;">Class</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// create permissionset</span><br />
&nbsp; &nbsp; permissionset.<span style="color: #000000;">add</span><span style="color: #000000;">&#40;</span>FileIOPermissionA<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; permissionset.<span style="color: #000000;">add</span><span style="color: #000000;">&#40;</span>FileIOPermissionR<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; permissionset.<span style="color: #000000;">add</span><span style="color: #000000;">&#40;</span>InteropPermission<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">// assert permissions</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">assertMultiple</span><span style="color: #000000;">&#40;</span>permissionset<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">// append text from source file to destination file</span><br />
&nbsp; &nbsp; System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">File</span><span style="color: #00007f;">::</span><span style="color: #000000;">AppendAllText</span><span style="color: #000000;">&#40;</span>distination<span style="color: #00007f;">,</span> System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">File</span><span style="color: #00007f;">::</span><span style="color: #000000;">ReadAllText</span><span style="color: #000000;">&#40;</span>original<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// limit the scope of the assert </span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Because all permissions are taken care of, this code will run client side as well as server side.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2009/10/31/append-text-from-one-file-to-an-other/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WinAPI, RPC 1702 and FindFirstFileW</title>
		<link>http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/</link>
		<comments>http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 21:39:50 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[RPC]]></category>
		<category><![CDATA[System.IO]]></category>
		<category><![CDATA[WinAPI]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=46</guid>
		<description><![CDATA[A very common scenario when doing interfaces with other applications, is to have a directory where one application places files, and the other one picks those up and processes them. Let&#8217;s say some application want to interface with Dynamics AX, one way, from that application to AX. That application will drop files of a specific [...]]]></description>
			<content:encoded><![CDATA[<p>A very common scenario when doing interfaces with other applications, is to have a directory where one application places files, and the other one picks those up and processes them.<br />
Let&#8217;s say some application want to interface with Dynamics AX, one way, from that application to AX. That application will drop files of a specific format, say *.txt, in a directory. You&#8217;ll want to have a batch job in AX that checks that directory every 5 minutes for those files. When your batch job finds those files, it will open them and do something with that data, then move them to an other directory, so you don&#8217;t process them twice.</p>
<p>Now the first thing you&#8217;ll probably think of (well, I did&#8230;), is to use the class <a href="http://msdn.microsoft.com/en-us/library/aa678136(AX.10).aspx">WinAPI</a>. This class has some useful static methods for checking if folders or files exist, finding files with a filename of a certain format. Just the kind of thing we&#8217;re looking for. Your code might look like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> loopfilesWinApi<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> fileName;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> handle;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> format <span style="color: #00007f;">=</span> <span style="color: #ff0000;">&quot;*.txt&quot;</span>;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span>WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">folderExists</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\\</span>Temp<span style="color: #000000;">\\</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span>handle<span style="color: #00007f;">,</span>fileName<span style="color: #000000;">&#93;</span> <span style="color: #00007f;">=</span> WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">findFirstFile</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\\</span>Temp<span style="color: #000000;">\\</span>&quot;</span> <span style="color: #00007f;">+</span> format<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">//currentfileName = fileName;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>filename<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename <span style="color: #00007f;">=</span> WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">findNextFile</span><span style="color: #000000;">&#40;</span>handle<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; WinApi<span style="color: #00007f;">::</span><span style="color: #000000;">closeHandle</span><span style="color: #000000;">&#40;</span>handle<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>That&#8217;s perfect. It checks a directory, in this cas C:\temp, for files with the extension txt&#8230; <strong>until</strong> you try to run this in batch. Your batch will have the status error, and there will be an entry in the event log saying:</p>
<blockquote><p>RPC error: RPC exception 1702 occurred in session xxx</p></blockquote>
<p>That error message is very misleading, but <a href="http://blogs.msdn.com/floditt/archive/2009/01/29/rpc-error-1702-when-calling-a-winapi-method-from-a-batch-job.aspx">Florian Dittgen has a nice blog entry about this</a>, that explains why this goes wrong: WinAPI methods a static and run on client. In batch, you can not use these client methods.<br />
But hey! There is a class called WinAPIServer, surely this runs on server, right? Correct, but a lot of the methods available in WinAPI are missing in WinAPIServer. You can however extend the WinAPI server class by copying methods from the WinAPI class and modifying them a bit. Let&#8217;s try that.</p>
<p>This is what the WinAPI::FindFirstFile() method looks like:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">client</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">container</span> findFirstFile<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">str</span> filename<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Binary data <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> Binary<span style="color: #000000;">&#40;</span><span style="color: #000000;">592</span><span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// size of WIN32_FIND_DATA when sizeof(TCHAR)==2</span><br />
&nbsp; &nbsp; DLL _winApiDLL <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLL<span style="color: #000000;">&#40;</span>#KernelDLL<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; DLLFunction _findFirstFile <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLLFunction<span style="color: #000000;">&#40;</span>_winApiDLL<span style="color: #00007f;">,</span> <span style="color: #ff0000;">'FindFirstFileW'</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">returns</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">DWord</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">arg</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">WString</span><span style="color: #00007f;">,</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">Pointer</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">&#91;</span>_findFirstFile.<span style="color: #000000;">call</span><span style="color: #000000;">&#40;</span>filename<span style="color: #00007f;">,</span> data<span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span>data.<span style="color: #000000;">wString</span><span style="color: #000000;">&#40;</span>#offset44<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>As you can see, this method uses the function FindFirstFileW from the Kernel32 dll located in the folder C:\WINDOWS\system32. Let&#8217;s copy this method to WinAPIServer and modify it to look like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">server</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">container</span> findFirstFile<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">str</span> filename<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; #define.<span style="color: #000000;">KernelDLL</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'KERNEL32'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; #define.<span style="color: #000000;">offset44</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">44</span><span style="color: #000000;">&#41;</span><br />
<br />
&nbsp; &nbsp; InteropPermission interopPerm;<br />
&nbsp; &nbsp; Binary data;<br />
&nbsp; &nbsp; DLL _winApiDLL;<br />
&nbsp; &nbsp; DLLFunction _findFirstFile;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; interopPerm <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> InteropPermission<span style="color: #000000;">&#40;</span>InteropKind<span style="color: #00007f;">::</span><span style="color: #000000;">DllInterop</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; interopPerm.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; data <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> Binary<span style="color: #000000;">&#40;</span><span style="color: #000000;">592</span><span style="color: #000000;">&#41;</span>; <span style="color: #007f00;">// size of WIN32_FIND_DATA when sizeof(TCHAR)==2</span><br />
&nbsp; &nbsp; _winApiDLL <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLL<span style="color: #000000;">&#40;</span>#KernelDLL<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; _findFirstFile <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> DLLFunction<span style="color: #000000;">&#40;</span>_winApiDLL<span style="color: #00007f;">,</span> <span style="color: #ff0000;">'FindFirstFileW'</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">returns</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">DWord</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; _findFirstFile.<span style="color: #000000;">arg</span><span style="color: #000000;">&#40;</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">WString</span><span style="color: #00007f;">,</span>ExtTypes<span style="color: #00007f;">::</span><span style="color: #000000;">Pointer</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">&#91;</span>_findFirstFile.<span style="color: #000000;">call</span><span style="color: #000000;">&#40;</span>filename<span style="color: #00007f;">,</span> data<span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span>data.<span style="color: #000000;">wString</span><span style="color: #000000;">&#40;</span>#offset44<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Three things have changed here:</p>
<ol>
<li>&#8220;Client&#8221; has been change to &#8220;Server&#8221; so it can run on server;</li>
<li>The code has been changed to use the InteropPermission class; this is required because the code runs on server;</li>
<li>Initialization of the variables is moved to after assertion of the InteropPermission.</li>
</ol>
<p>When you test this in batch (running on server), this will work, and you can do this for all WinAPI methods in the examples above&#8230; <strong>until</strong> you try this on a x64 architecture. You&#8217;ll receive a message saying that an error occurred when calling the FindFirstFileW function in the Kernel32 DLL (the exact message slipped my mind).<br />
The problem is described on the <a href="http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.axapta.programming&amp;tid=c5b2f7c6-0dc3-49f3-a49e-dcd836625563&amp;cat=&amp;lang=&amp;cr=&amp;sloc=&amp;p=1">Axapta Programming Newsgroup</a>. You can code your way around this problem, but I think the message is clear: <strong>don&#8217;t use WinAPI</strong>.</p>
<p>Instead, use the .NET Framework <strong>System.IO</strong> namespace (watch out for lowercase/uppercase here), in our case in specific, System.IO.File and System.IO.Directory.<br />
I get this nice code example from <a href="http://greg.agiletortoise.com/2007/04/02/dynamics-ax-making-net-calls-from-inside-ax/">Greg Pierce’s blog</a> (modified it a bit):</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> loopfilesSystemIO<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// loop all files that fit the required format</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> fileName;<br />
&nbsp; &nbsp; InteropPermission interopPerm;<br />
<br />
&nbsp; &nbsp; System.<span style="color: #000000;">Array</span> files;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> i<span style="color: #00007f;">,</span> j;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">container</span> fList;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; interopPerm <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> InteropPermission<span style="color: #000000;">&#40;</span>InteropKind<span style="color: #00007f;">::</span><span style="color: #000000;">ClrInterop</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; interopPerm.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; files <span style="color: #00007f;">=</span> System.<span style="color: #000000;">IO</span>.<span style="color: #000000;">Directory</span><span style="color: #00007f;">::</span><span style="color: #000000;">GetFiles</span><span style="color: #000000;">&#40;</span>@<span style="color: #ff0000;">&quot;C:<span style="color: #000000;">\t</span>emp&quot;</span><span style="color: #00007f;">,</span> <span style="color: #ff0000;">&quot;*.txt&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #000000;">&#40;</span> i<span style="color: #00007f;">=</span><span style="color: #000000;">0</span>; i<span style="color: #00007f;">&lt;</span>ClrInterop<span style="color: #00007f;">::</span><span style="color: #000000;">getAnyTypeForObject</span><span style="color: #000000;">&#40;</span>files.<span style="color: #000000;">get_Length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; i<span style="color: #00007f;">++</span> <span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fList <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conins</span><span style="color: #000000;">&#40;</span>fList<span style="color: #00007f;">,</span> <span style="color: #0000ff;">conlen</span><span style="color: #000000;">&#40;</span>fList<span style="color: #000000;">&#41;</span><span style="color: #00007f;">+</span><span style="color: #000000;">1</span><span style="color: #00007f;">,</span> ClrInterop<span style="color: #00007f;">::</span><span style="color: #000000;">getAnyTypeForObject</span><span style="color: #000000;">&#40;</span>files.<span style="color: #000000;">GetValue</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #000000;">&#40;</span>j<span style="color: #00007f;">=</span><span style="color: #000000;">1</span>;j<span style="color: #00007f;">&amp;</span>lt;<span style="color: #00007f;">=</span> <span style="color: #0000ff;">conlen</span><span style="color: #000000;">&#40;</span>fList<span style="color: #000000;">&#41;</span>; j<span style="color: #00007f;">++</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fileName <span style="color: #00007f;">=</span> <span style="color: #0000ff;">conpeek</span><span style="color: #000000;">&#40;</span>fList<span style="color: #00007f;">,</span> j<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>This will run on client and on server without any problems. To have clean code, you could create your own class, just like WinAPI, but using the System.IO namespace. That way, you can reuse this code and save time.</p>
<p>Conclusion: abandon WinAPI, and use System.IO instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
