WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: stanl on April 12, 2020, 07:11:44 AM

Title: CLR FileStream Object
Post by: stanl on April 12, 2020, 07:11:44 AM
This is related to Jim's post on HTMLAgilityPack. Several of the Methods in that assembly reference a FileStream Object.  I know you can issue
Code (WINBATCH) Select

oStream = ObjectClrNew('System.IO.FileStream')



but C# code is generally: FileStream sw = new FileStream("FileStream.html", FileMode.Create)
But there is no oStream.Create() method, so the question is how to replicate the above in the WB CLR.
Title: Re: CLR FileStream Object
Post by: JTaylor on April 12, 2020, 02:50:42 PM
Don't know if this will do what you need but

oStream = ObjectClrNew('System.IO.File')


and

sw = oStream.Create("FileStream.html")


Will get a File Opened.   Not sure about the Stream needs.

jim
Title: Re: CLR FileStream Object
Post by: JTaylor on April 12, 2020, 03:02:25 PM
Seems to initialize objects but need something different on the parameters.  Guessing I am way off though.

Code (winbatch) Select


IntControl(73,1,0,0,0)
gosub udfs
html=""

ObjectClrOption("useany", "System.Data")
oFile   = ObjectClrNew('System.IO.File')
oStream = ObjectClrNew('System.IO.FileStream')
;avoid secure channel errors

sw = oFile.Create("FileStream.html")
fs = oStream(sw,1|1)


oWeb=0
oHTML=0
Exit


:WBERRORHANDLER
oWeb=0
oHTML=0
geterror()
Message("Error Encountered",errmsg)
Exit


:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   ClipPut(errmsg)
   Return(errmsg)
#EndSubRoutine


Return


Exit

Title: Re: CLR FileStream Object
Post by: stanl on April 13, 2020, 03:23:35 AM
Yeah, I tried several options for example
Code (WINBATCH) Select


cOutfile = "c:\temp\test.html"
If FileExist(cOutfile) Then FileDelete(cOutfile)

ObjectClrOption("useany", "System.IO")
oStream = ObjectClrNew('System.IO.FileStream',cOutfile,2) ; 2= Create



which errors with 'constructor on type System.IO.FileStream not found.

Title: Re: CLR FileStream Object
Post by: JTaylor on April 13, 2020, 09:08:38 AM
Would it just be the following?   If I read things correctly sw should be a filestream.  May need the other assembly for additional stuff you want to do but maybe this gives you the file object you need to get started?

Code (winbatch) Select

ObjectClrOption("useany", "System.Data")
oFile   = ObjectClrNew('System.IO.File')

sw = oFile.Create("FileStream.html")


Title: Re: CLR FileStream Object
Post by: stanl on April 13, 2020, 05:39:44 PM
Guess we can beat this dead horse....
Code (WINBATCH) Select


cOutfile = "c:\temp\test.html"
If FileExist(cOutfile) Then FileDelete(cOutfile)
oFile   = ObjectClrNew('System.IO.File')

oFile.AppendAllText(cOutfile,html)
;oHTML.Save(cOutfile)



html is the URL data returned with my Agility CLR script. And either of the options above to save the html work. The problem is both methods suggest an optional encoding ObjectCltType() which would imply an encoding enum.
Title: Re: CLR FileStream Object
Post by: JTaylor on April 13, 2020, 06:10:55 PM
Yes.   This what you need?

ObjectClrOption("useany", "System.Data")
oEncode = ObjectClrNew("System.Text.Encoding")
oFile   = ObjectClrNew('System.IO.File')

;avoid secure channel errors

html = FileGet(DirScript():"hap_test.html")
cOutfile = DirScript():"test.html"
If FileExist(cOutfile) Then FileDelete(cOutfile)

oFile.AppendAllText(cOutfile,html,oEncode.UTF8)
Title: Re: CLR FileStream Object
Post by: stanl on April 14, 2020, 02:30:07 AM
Yep. Got that. Thought I needed ObjectClrOption("useany", "System.Text") before Encoding and that failed.