Author Topic: CLR FileStream Object  (Read 173 times)

stanl

  • Pundit
  • *****
  • Posts: 1164
CLR FileStream Object
« 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
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

  • Pundit
  • *****
  • Posts: 1361
    • Data & Stuff Inc.
Re: CLR FileStream Object
« Reply #1 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

JTaylor

  • Pundit
  • *****
  • Posts: 1361
    • Data & Stuff Inc.
Re: CLR FileStream Object
« Reply #2 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

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

  • Pundit
  • *****
  • Posts: 1164
Re: CLR FileStream Object
« Reply #3 on: April 13, 2020, 03:23:35 am »
Yeah, I tried several options for example
Code: Winbatch

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

  • Pundit
  • *****
  • Posts: 1361
    • Data & Stuff Inc.
Re: CLR FileStream Object
« Reply #4 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
ObjectClrOption("useany", "System.Data")
oFile   = ObjectClrNew('System.IO.File')

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


 

stanl

  • Pundit
  • *****
  • Posts: 1164
Re: CLR FileStream Object
« Reply #5 on: April 13, 2020, 05:39:44 pm »
Guess we can beat this dead horse....
Code: Winbatch

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

  • Pundit
  • *****
  • Posts: 1361
    • Data & Stuff Inc.
Re: CLR FileStream Object
« Reply #6 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)

stanl

  • Pundit
  • *****
  • Posts: 1164
Re: CLR FileStream Object
« Reply #7 on: April 14, 2020, 02:30:07 am »
Yep. Got that. Thought I needed ObjectClrOption("useany", "System.Text") before Encoding and that failed.