C# - 浏览文件夹和二进制阅读器

时间:2018-02-19 20:15:57

标签: c# folderbrowserdialog binaryreader

我很难让二进制阅读器检测从“浏览文件夹”对话框中选择的文件。目的是读取目录中所有文件中的“X”位置,并将该数据保存到TXT文件中。我尝试了各种各样的方法,但似乎无法得到它...我遇到麻烦的是:

BinaryReader NDSRead2 = new BinaryReader(file)

我放入替换(file)的任何内容都会引发错误。尝试使用该行以及我的代码中的其他地方的各种方法,但似乎无法得到它。我的代码被列为打击。

 /// OPEN FOLDER DIALOGUE      
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.Description = "Select a folder";
        fbd.ShowNewFolderButton = false;
        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
           /// THIS NEXT CHUNK OF CODE SETS UP WHERE THE TXT FILE WILL BE SAVED, IN THE SELECTED DIRECTORY.
            string brlTextLoc = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            DirectoryInfo dir1 = new DirectoryInfo(fbd.SelectedPath);
           txtBRLSave = new System.IO.StreamWriter(fbd.SelectedPath + "BuildRomList.TXT", true);

            txtBRLSave.WriteLine("Build Rom List");
            txtBRLSave.WriteLine("Using The Rom Assistant v0.57");
            txtBRLSave.WriteLine();


          /// THE LOOP FOR EACH FILE TO BE READ IN FOLDER BEGINS.
            FileInfo[] files = dir1.GetFiles();
            System.IO.StreamWriter txtBRLSave;
            foreach (string file in Directory.EnumerateFiles(fbd.SelectedPath, "*.EXT"))
            {

                BinaryReader NDSRead2 = new BinaryReader(file);

            /// THE ISSUE I HAVE IS WITH "(FILE)" ABOVE... IT KEEPS GETTING FLAGGED IN RED NO MATTER WHAT I PUT IN THERE.

           /// BELOW CONTINUES THE BR CODE, AND TXT SAVING CODE, WHICH ISN'T NEEDED FOR THIS QUESTION AS I KNOW IT WORKS.

1 个答案:

答案 0 :(得分:2)

将字符串传递到流中,如下所示

foreach(string file in Directory.EnumerateFiles(...
{
    using(var stream = new FileStream(file, FileMode.Open))
    using(var NDSRead2 = new BinaryReader(stream))
    {
       // do you stuff
    }
}