convert this vb.net code to c#

hi, i found this code on the net, but it's in VB.net, and I have problem converting it to C#, especially because C# doesn't have the Friend Function, right?

here's the code:

#Region "Class specific code"
Friend Function OpenFileForSecureOverwrite(ByVal path As String) As FileStream
If Not _openHandle.Equals(IntPtr.Zero) AndAlso Not _openHandle.ToInt32 = Me.INVALID_HANDLE_VALUE Then
Me.CloseHandle(_openHandle)
_openHandle = IntPtr.Zero
End If

_openHandle = Me.CreateFile(path, FileAccess.GENERIC_WRITE, FileShare.FILE_SHARE_READ Or FileShare.FILE_SHARE_WRITE, Nothing, CreationDisposition.OPEN_EXISTING, FlagsAndAttributes.FILE_FLAG_WRITE_THROUGH, Nothing)

If _openHandle.ToInt32 = Me.INVALID_HANDLE_VALUE Then
Return Nothing
Else
Return New FileStream(_openHandle, IO.FileAccess.ReadWrite)
End If
End Function
#End Region
[946 byte] By [imin] at [2007-11-20 11:46:33]
# 1 Re: convert this vb.net code to c#
hi, i found this code on the net, but it's in VB.net, and I have problem converting it to C#, especially because C# doesn't have the Friend Function, right?The problem with 'friend' I would need to know why you need it. We have 'internal' keyword in C# maybe this helps

here's the translated code:

#region "Class specific code"
internal FileStream OpenFileForSecureOverwrite(string path ) {
if ( (! _openHandle.Equals(IntPtr.Zero)) && (! _openHandle.ToInt32 == this.INVALID_HANDLE_VALUE) ){
this.CloseHandle(_openHandle);
_openHandle = IntPtr.Zero;
}
_openHandle = this.CreateFile(path, FileAccess.GENERIC_WRITE, FileShare.FILE_SHARE_READ || FileShare.FILE_SHARE_WRITE, null, CreationDisposition.OPEN_EXISTING, FlagsAndAttributes.FILE_FLAG_WRITE_THROUGH, null);

if (_openHandle.ToInt32 == this.INVALID_HANDLE_VALUE ){
return null;
}else{
return new FileStream(_openHandle, IO.FileAccess.ReadWrite);
}
}
#endregion

I have translated what could be translated without knowing all that VB-net classes and without knowing the class where it is part from.
Things like this.INVALID_HANDLE_VALUE are obviously part of the vb class where the code is from so you need the full class to know how this fields are defined. You also will need to add some using statements for the FileStream. So why not simple solve your problem writing your own methode which fits exact to your needs. So the question is always the same: What do you want to achieve using that code, whats do you need to do in your code?
JonnyPoet at 2007-11-9 11:37:04 >
# 2 Re: convert this vb.net code to c#
You may also want to check out this Online code translator (http://www.carlosag.net/Tools/CodeTranslator/Default.aspx). I used it with basic code snippets ( in both C# and VB ), and it seem to have produced the correct results.

This is what it produced :
internal FileStream OpenFileForSecureOverwrite(string path) {
if ((!_openHandle.Equals(IntPtr.Zero)
&& !(_openHandle.ToInt32 == this.INVALID_HANDLE_VALUE))) {
this.CloseHandle(_openHandle);
_openHandle = IntPtr.Zero;
}
_openHandle = this.CreateFile(path, FileAccess.GENERIC_WRITE, (FileShare.FILE_SHARE_READ | FileShare.FILE_SHARE_WRITE), null, CreationDisposition.OPEN_EXISTING, FlagsAndAttributes.FILE_FLAG_WRITE_THROUGH, null);
if ((_openHandle.ToInt32 == this.INVALID_HANDLE_VALUE)) {
return null;
}
else {
return new FileStream(_openHandle, IO.FileAccess.ReadWrite);
}
}

Basically the same as Jonny's great code.
HanneSThEGreaT at 2007-11-9 11:38:05 >
# 3 Re: convert this vb.net code to c#
ouh, thanks a lot both of u...
imin at 2007-11-9 11:39:04 >
# 4 Re: convert this vb.net code to c#
I have translated what could be translated without knowing all that VB-net classes and without knowing the class where it is part from.
Things like this.INVALID_HANDLE_VALUE are obviously part of the vb class where the code is from so you need the full class to know how this fields are defined. You also will need to add some using statements for the FileStream. So why not simple solve your problem writing your own methode which fits exact to your needs. So the question is always the same: What do you want to achieve using that code, whats do you need to do in your code?

Thanks for answering. I'm sorry I don't post the whole code here.
Anyway I manage to convert the whole code to C# using the link provided by hannesthegreat. However there're some problems:

private enum FileAccess
{

GENERIC_READ = 2147483648,
//bla bla
}

private enum FlagsAndAttributes
{

FILE_FLAG_WRITE_THROUGH = 2147483648,
//bla bla

}

for both the code above, the error message "Cannot implicitly convert type 'uint' to 'int'. An explicit conversion exists (are you missing a cast?)" is displayed. When I tried adding the (int) syntax to make the code like this GENERIC_READ = (int) 2147483648, , the error message "Constant value '2147483648' cannot be converted to a 'int' (use 'unchecked' syntax to override)" is displayed.

What should I do here?
Thanks a lot
imin at 2007-11-9 11:40:10 >
# 5 Re: convert this vb.net code to c#
...Basically the same as Jonny's great code.Great to know Hannes ... (as I have done it by hand ) :D Now I can promote myself to be a good transcoder machine <rofl> Maybe making lots of money with that. :D :D ;)

Hi imin :
Regarding enums: Derive them from long or uint
private enum FileAccess: uint {
GENERIC_READ = 2147483648,
//bla bla
}
private enum FlagsAndAttributes :uint{
FILE_FLAG_WRITE_THROUGH = 2147483648,
//bla bla
}

Thats all about:wave:
JonnyPoet at 2007-11-9 11:41:09 >