简体   繁体   中英

Vb.net To C#.net

Please help me This function is using the language VB.NET What is the alternative function in the language of the C#

Public Sub Input(FileNumber As Integer, ByRef Value As String)
     Member of Microsoft.VisualBasic.FileSystem

Summary: Reads data from an open sequential file and assigns the data to variables.

You can use the same function, just import it in your C# project. In VisualStudio, go to References and add a reference to Microsoft.VisualBasic .

在此处输入图片说明

Then just add a using statement and start using the function.

using Microsoft.VisualBasic;

private void Form1_Load(object sender, EventArgs e)
{
    string test = "aksfhkaljfh";
    Microsoft.VisualBasic.FileSystem.Input(123, ref test);
}

AFAIK there is no perfect equivalent, but you can write a method that does what you want.
If your data is comma-seperated values, for example, you can:

foreach (var line in File.ReadAllLines(file))
{
    var values = line.Split(',');
    var firstValue = values[0];
    ...
}

This is a VB specific function. As explained in other answer, you can reference Microsoft.VisualBasic and use it from C#. But it's considered obsolete even in VB.NET. There is no similar C# specific functionality, you can use the System.IO BCL classes to do that. Like this:

using (var r = new BinaryReader(File.OpenRead(@"your path")))
{
    var s = r.ReadString();
    var i = r.ReadInt32();
    // etc.
}

There is a single liner: File.readAllText ()

https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx

The .NET framework provides all of the file reading capabilities you want. As stated in other answers, Visual Basic provides helpers to sort of shortcut these systems and provide a similar experience to older versions of Visual Basic that existed before the .NET framework existed.

If you want to convert something in Visual Basic to some other .NET language you'll need to

  1. Import the Visual Basic library as stated in another answer
  2. Use the .NET framework's equivalent code

As another answer stated, it's pretty simple to read the entire contents of a text file:

string pathToFile = @"C:\temp\myfile.txt"
string contents = System.IO.File.ReadAllText(pathToFile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM