2

So, I'm working on converting this code from VB.NET to C#:

    Public Class Form1
    Const filesplit As String = "|split|"
    Dim stub, opt() As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        On Error Resume Next
        FileOpen(1, Application.ExecutablePath, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared)
        stub = Space(LOF(1))
        FileGet(1, stub)
        FileClose(1)
        opt = Split(stub, filesplit)

    End Sub
End Class

I've used a series of online converters, and they don't really work for me.

How do I do it? I'm trying to understand VB.NET source code so I can use it in.

4
  • 4
    Are you sure that is VB.NET and not VB6? Commented Sep 12, 2014 at 12:04
  • What's FileOpen()? (And Space()? And LOF()? And FileGet()? And FileClose()? And Split()?) If this is VB .NET then you have some helper functions that you either need to examine and implement or move to a separate assembly and reference them as-is. Either way, all of the file operations you would need are in the System.IO namespace. Commented Sep 12, 2014 at 12:08
  • 1
    @David FileOpen/Space/LOF etc. are all standard functions of VB.NET (for backward compatibility with VB6; and yes, even the On Error statement still works in VB.NET). Commented Sep 12, 2014 at 12:30
  • This must be VB.NET code as Application.Executable path is not in VB6. Looks like it has been ported from VB6 and blindly copied and pasted. Commented Sep 12, 2014 at 12:43

4 Answers 4

2

Those methods are in the Microsoft.VisualBasic Namespace.

So you could just add a reference to that in your project and then use virtually the exact same code with a small amount of extra qualification on the methods used:

using Microsoft.VisualBasic; //add this statement

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string filesplit = "|split|";
        string stub;
        string[] opt;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FileSystem.FileOpen(1, Application.ExecutablePath, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared);
            stub = Strings.Space(Convert.ToInt32(FileSystem.LOF(1)));
            FileSystem.FileGet(1, ref stub);
            FileSystem.FileClose(1);
            opt = Strings.Split(stub, filesplit);
        }
    }
}

However you should really look into using the File.xxx methods in the System.IO namespace in both your VB.NET and C# code going forward, but this will get it working for you.

Sign up to request clarification or add additional context in comments.

3 Comments

However, since this is about a translation to C#, it would be smarter to (manually) convert to C# equivalents.
@rskar - I don't disagree with you, but it would have been smarter to translate this to VB.NET code that didn't require the VisualBasic namespace in the first place.
thank you my brother and you are the best answer, thank you to any one try to help me thank you
1

Use File.Open. That should get you what you want.

Comments

1

Your VB code is essentially doing this:

// The using clause ensures the StreamReader is properly disposed after the closing block.
using (StreamReader sr = File.OpenText(Application.ExecutablePath))
{
    stub = sr.ReadToEnd();
    opt = stub.Split(filesplit).ToArray();
}

This assumes filesplit is a char, string or something like Environment.NewLine

Comments

0

Basically you would use :

Dim content = File.ReadAllText("c:\temp\MyTest.txt")

However it would be more correct to use:

Dim path As String = "c:\temp\MyTest.txt"
If File.Exists(path) Then
    Dim content = File.ReadAllText(path)
    Rem do something with content
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.