Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中使用FileOpen(VB.NET)?_C#_.net_Vb.net_Vb.net To C# - Fatal编程技术网

如何在C#中使用FileOpen(VB.NET)?

如何在C#中使用FileOpen(VB.NET)?,c#,.net,vb.net,vb.net-to-c#,C#,.net,Vb.net,Vb.net To C#,因此,我正在将这段代码从VB.NET转换为C#: 我使用了一系列的在线转换器,但它们对我来说并不起作用 我该怎么做?我正在努力理解VB.NET源代码,以便在中使用。使用。这应该可以满足您的需求。基本上您可以使用: Dim content = File.ReadAllText("c:\temp\MyTest.txt") 但是,更正确的做法是使用: Dim path As String = "c:\temp\MyTest.txt" If File.Exists(path) Then Dim

因此,我正在将这段代码从VB.NET转换为C#:

我使用了一系列的在线转换器,但它们对我来说并不起作用


我该怎么做?我正在努力理解VB.NET源代码,以便在中使用。

使用。这应该可以满足您的需求。

基本上您可以使用:

Dim content = File.ReadAllText("c:\temp\MyTest.txt")
但是,更正确的做法是使用:

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

您的VB代码基本上是这样做的:

// 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();
}

这假设filesplit是一个字符、字符串或类似环境的东西。NewLine

这些方法位于
Microsoft.VisualBasic
命名空间中

因此,您可以在您的项目中添加对该项目的引用,然后使用几乎完全相同的代码,并对所使用的方法进行少量额外的限定:

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);
        }
    }
}

但是,您应该认真研究在VB.NET和C#代码中使用
System.IO
命名空间中的
File.xxx
方法,但这会让它为您工作。

您确定这是VB.NET而不是VB6吗?什么是
FileOpen()
?(和
Space()
?和
LOF()
?和
FileGet()
?和
FileClose()
?和
Split()
?)如果这是VB.NET,那么您需要检查和实现一些辅助函数,或者将它们移动到单独的程序集并按原样引用它们。无论哪种方式,您需要的所有文件操作都在
System.IO
命名空间中。@David FileOpen/Space/LOF等都是VB.NET的标准函数(为了向后兼容VB6;是的,甚至
On Error
语句在VB.NET中仍然有效)。这必须是作为应用程序的VB.NET代码。可执行路径不在VB6中。看起来它是从VB6移植过来的,并被盲目地复制和粘贴。但是,由于这是关于C#的翻译,所以(手动)转换为C#等价物会更明智。@rskar-我不同意你的观点,但是,如果把它翻译成VB.NET代码,而不需要VisualBasic名称空间,那就更聪明了。谢谢你,我的兄弟,你是最好的答案,谢谢你能帮助我的人谢谢你
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);
        }
    }
}