Example Usage
Directory and File Manipulation
The .NET Namespace System.IO (in the mscorlib assembly) provides some useful facilities for manipulating files. For example, you can create a DirectoryInfo object associated with a particular directory on your computer, call its GetFiles method to obtain a list of files, and then get their Name and CreationTime properties:
⎕USING←,⊂'System.IO'
d←⎕NEW DirectoryInfo (⊂'C:\Dyalog') where d is an instance of the Directory class, corresponding to the directory [DYALOG].
Information
[DYALOG] refers to the directory in which Dyalog is installed; this example assumes [DYALOG] to be C:\Dyalog.
The GetFiles method returns a list of files (more precisely, FileInfo objects) that represent each of the files in the directory. Its optional argument specifies a filter. For example:
d.GetFiles ⊂'*.exe'
evalstub.exe exestub.exe dyalog.exe dyalogrt.exe
The Name property returns the name of the file associated with the File object:
(d.GetFiles ⊂'*.exe').Name
evalstub.exe exestub.exe dyalog.exe dyalogrt.exe and the CreationTime property returns its creation time, which is a DateTime object:
(d.GetFiles ⊂'*.exe').CreationTime
05/03/2020 10:23:40 05/03/2020 10:23:28 14/11/2019 ... Calling the GetFiles overload that does not take any arguments (from Dyalog by supplying an argument of ⍬) returns a complete list of files:
files←d.GetFiles ⍬
Taking advantage of namespace reference array expansion, an expression to display file names and their creation times is:
files,[1.5]files.CreationTime
relnotes.hlp 03/02/2004 11:47:02
relnotes.cnt 03/02/2004 11:47:02
def_uk.dse 22/03/2004 12:13:31
DIALOGS.HLP 22/03/2004 12:13:31
dyares32.dll 22/03/2004 12:13:40
... Sending an Email
The .NET namespace System.Web.Mail provides objects for handing email. You can create a new email message as an instance of the MailMessage class, set its various properties and then send it using the SmtpMail class.
Example
This example will only work if your computer is configured to allow you to send email in this way.
⎕USING←'System.Web.Mail,System.Web.dll'
m←⎕NEW MailMessage
m.From←'tony.blair@uk.gov'
m.To←'sales@dyalog.com'
m.Subject←'order'
m.Body←'Send me 100 copies of Dyalog APL now'
SmtpMail.Send m However, the Send method of the SmtpMail object is overloaded; it could be called with a single parameter of type System.Web.Mail.MailMessage as above, or four parameters of type System.String. An alternative is:
SmtpMail.Send 'tony.blair@uk.gov'
'sales@dyalog.com'
'order'
'Send me the goods' For more information on overloading, see Overload Constructors.
Web Scraping
The .NET Framework provides a range of classes for accessing the internet from a program. This section works through an example that shows how to read the contents of a web page. It is complicated, but realistic (for example, it includes code to cater for a firewall/proxy connection to the internet). It is only 9 lines of APL code, but each line requires careful explanation.
Start by defining ⎕USING so that it specifies all of the necessary .NET namespaces and assemblies:
⎕USING←'System,System.dll' 'System.Net' 'System.IO' The WebRequest class in the System.Net .NET namespace implements the .NET Framework's request/response model for accessing data from the internet. For this example, a WebRequest object needs to be associated with the URI http://www.dyalog.com (WebRequest is an example of a static class – its methods can be used without creating instances of it):
wrq←WebRequest.Create ⊂'http://www.dyalog.com' Potentially confusingly, if the URI specifies a protocol of "http://" or "https://", an object of type HttpWebRequest is returned rather than a simple WebRequest. The effect of this is that, at this stage, wrq is an HttpWebRequest object.
wrq
System.Net.HttpWebRequest This class has a Proxy property through which the proxy information is specified for a request made through a firewall. The value assigned to the Proxy property has to be an object of type System.Net.WebProxy. It might, therefore, be necessary to create a new WebProxy object specifying the hostname and port number for the firewall:
PX←⎕NEW WebProxy(⊂'http://dyagate.dyadic.com:8080')
PX
System.Net.WebProxy (This statement will need to be amended to suit each internet configuration, and might not be required).
Having set up the WebProxy object as required, the Proxy property of the HttpRequest object wrq is then assigned to it:
wrq.Proxy←PX The HttpRequest class has a GetResponse method that returns a response from an internet resource. Although it is not yet HTML, the result is an object of type System.Net.HttpWebResponse:
wr←wrq.GetResponse
wr
System.Net.HttpWebResponse The HttpWebResponse class has a GetResponseStream method whose result is of type System.Net.ConnectStream. This object, whose base class is System.IO.Stream, provides methods to read and write data both synchronously and asynchronously from a data source, which in this case is physically connected to a TCP/IP socket:
str←wr.GetResponseStream
str
System.Net.ConnectStream However, the Stream class is designed for byte input and output; what is needed in this example is a class that reads characters in a byte stream using a particular encoding. This is a job for the System.IO.StreamReader class. Given a Stream object, create a new instance of a StreamReader by passing it the Stream as a parameter:
rdr←⎕NEW StreamReader str
rdr
System.IO.StreamReader Use the ReadToEnd method of the StreamReader to get the contents of the page:
s←rdr.ReadToEnd
⍴s
45242 Finally, to avoid running out of connections, it is necessary to close the stream:
str.Close