ScriptSpot

Forum topic · General Scripting

Calling .Net functions

By WillyWonka · 2010-11-10

Description

I'm trying to write a script that does a SHA1 check on a file and I understand the .Net calls that I need to call but for the life of me I can't get it working in maxscript. Can anyone help me finish it?

The first problem is the file delme#.txt never gets closed. I'm pretty sure I need to call stream.Close() but I can't get it to work.

The second is running byte[] = hashClass.ComputeHash(fileStream, FileMode.Open)
How do I store a byte array in maxscript?


fn SHA filename =
(
	if doesFileExist filename then
	(
	
		fileMode = dotNetClass "System.IO.FileMode"
		print (fileMode.Open as string)
		
		stream = dotNetObject "System.IO.FileStream" filename fileMode.Open
		
		hashClass = dotNetClass "System.Security.Cryptography.SHA1CryptoServiceProvider" 
		hash = hashClass.ComputeHash stream
		
		-- Try unsucessfully to close the stream
		
		showMethods stream
		dotNetObject "System.Security.Cryptography.SHA1CryptoServiceProvider.Close"
		dotNetObject "System.IO.FileStream.Close"
		
	)
)

SHA "c:/DelMe6.txt"
Comments (2)

fastcg · 2015-06-09

fn GetSha filepath =
(
if doesFileExist filepath do
(
stream = dotNetObject "System.IO.FileStream" filepath (dotNetClass "System.IO.FileMode").Open
hashClass = (dotnetclass "System.Security.Cryptography.HashAlgorithm").Create()
hash = hashClass.ComputeHash(stream)
stream.Close()
return (dotnetclass "System.BitConverter").ToString(hash)
)
)

GetSha @"E:\readme_1.txt"

kogen · 2012-05-10

The problem is, you're creating a dot.Object with a close method... to me that looks totally weird.
Since you want to perform the close-method on the existing stream use "stream.close()" to close this class-instance.