CLR FileStream Object

Started by stanl, April 12, 2020, 07:11:44 AM

Previous topic - Next topic

stanl

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.

JTaylor

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

JTaylor

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


stanl

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.


JTaylor

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")



stanl

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.

JTaylor

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)

stanl

Yep. Got that. Thought I needed ObjectClrOption("useany", "System.Text") before Encoding and that failed.