How to Do a System.IO.File.WriteAllBytes ?

Using (or providing) Microsoft.NET Classes

How to Do a System.IO.File.WriteAllBytes ?

Postby PGilbert on Sat Aug 14, 2010 12:11 am

I can do a ReadAllBytes of a file like this:

bytes ← System.IO.File.ReadAllBytes (⊂file_name)

But if I want to rewrite those bytes to a file it does not work:

System.IO.File.WriteAllBytes (⊂file_name2) (⊂bytes)

I get a DOMAIN ERROR because bytes is not of the type Byte[]

What is the proper way to call this method ?

Thanks in advance,

Pierre Gilbert
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to Do a System.IO.File.WriteAllBytes ?

Postby Vince|Dyalog on Mon Aug 16, 2010 10:01 am

Hi Pierre,

This works to do what you are asking:

Code: Select all
rallbytes;⎕USING
 ⎕USING←'System' 'System.IO'
 bptr←File.ReadAllBytes ⊂'c:\ifo\svn.tips'
 IO.File.WriteAllBytes 'c:\ifo\svn2'  bptr


You don't need to and should not enclose the character vector or the array of bytes in the WriteAllBytes line.

Regards,

Vince
Vince|Dyalog
 
Posts: 413
Joined: Wed Oct 01, 2008 9:39 am

Re: How to Do a System.IO.File.WriteAllBytes ?

Postby PGilbert on Mon Aug 16, 2010 3:48 pm

Many thanks for your answer Vince. I was under the impression that I needed to use enclose for each argument, but I need enclose only if there is only one argument. Also, looks like APL is coercing 16 bits APL number to a .Net Byte that is 8 bits, so I don't need to do a Convert.ToByte, and that is a good surprise.

So for one argument I need to do this:

System.IO.Directory.GetFiles ⊂'c:\windows' ⍝ This is working
System.IO.Directory.GetFiles  'c:\windows' ⍝ This is not working

And with 2 arguments this is working:

System.IO.Directory.GetFiles 'c:\windows' '*.*'

but the following line is not working with 2 arguments:

System.IO.Directory.GetFiles 'c:\windows' '*'

I need to do the following instead:

System.IO.Directory.GetFiles 'c:\windows' (,'*')

Could you explain the logic behind that parsing business because it is not obvious what we need to do.

Regards,

Pierre gilbert
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to Do a System.IO.File.WriteAllBytes ?

Postby Morten|Dyalog on Tue Aug 17, 2010 10:04 am

When calling a .NET function, Dyalog APL attempts to pick the correct "overload", mapping each element of the array given on the right to an argument of the method. We attempt to do "sensible" mappings wherever possible, but as you have noticed, we do get into trouble with types like System.Byte and System.Boolean where the .Net framework thinks that these are fundamentally different from Integers, while APL does not (I will respond in more detail in the tread where you raised this issue).

In versions prior to 11.0, we were "helpful" and mapped a simple character vector or scalar argument to a single String argument if there was such an overload. However, users ran into some methods where this caused ambiguities. For example, if you look at a call like (Foo 'ab'), which of the overloads

Foo (System.String)
Foo (System.String, System.String)

... should it match? If we cast character scalars to strings, it could be the 2nd one. In particular, I don't think it would be nice if Foo 'ab' picked the 2nd one and Foo 'abc' the first. From v11, we decided to take a stricter view. To call an overload with a single argument, you must provide a 1-element array, and we don't cast character scalars to strings.
User avatar
Morten|Dyalog
 
Posts: 453
Joined: Tue Sep 09, 2008 3:52 pm

Re: How to Do a System.IO.File.WriteAllBytes ?

Postby PGilbert on Tue Aug 17, 2010 3:01 pm

Thanks Morten for your explanations. Personnally I really don't see the problem in sorting the number of arguments using depth(≡) and shape(⍴) but I must be missing the bigger picture. For me Foo 'ab' is a one string argument and Foo 'a' 'b' is a two strings argument and Foo 'abc' is a one string argument.

Regards,

Pierre Gilbert
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to Do a System.IO.File.WriteAllBytes ?

Postby Morten|Dyalog on Tue Aug 17, 2010 3:25 pm

Problem is, in any "APL2 variant" (APL2, APLX, APL+Win, DyalogAPL, probably also VisualAPL), the APL interpreter cannot tell the difference between (Foo 'ab') and (Foo 'a' 'b'):
Code: Select all
      'ab'≡'a' 'b'
1
User avatar
Morten|Dyalog
 
Posts: 453
Joined: Tue Sep 09, 2008 3:52 pm

Re: How to Do a System.IO.File.WriteAllBytes ?

Postby PGilbert on Tue Aug 17, 2010 4:14 pm

Thanks Morten for the education, you seem to be correct, I did not known about this and it explains all the trouble of specifying the arguments of a .Net method.

Pierre Gilbert
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada


Return to Microsoft.NET

Who is online

Users browsing this forum: No registered users and 1 guest