简体   繁体   中英

How to fix StreamWriter error “cannot implicitly convert type string to System.IO.StreamWriter”

I'm using C# in Visual Studio 2017 Community.

I have a working, fully functional program that I am trying to optimize. In a nutshell, the program reads text and numbers from text files, does some math and reformatting, then outputs to a new text file.

The part I am trying to optimize is the way lines of text are written to the output file. Here is what works:

using System.IO

// Let's start building the MA output text file now.
// Designate an output file --- put it in same directory as original files
// give MA file ame name as the original file -- but different extension.

maOutFile = dir + "\\" + serialNumber[f] + ".ma";

//Create MA header lines and write to file.

string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";

// Open up the file for writing
File.WriteAllText(maOutFile, maHeaderLine1);
File.AppendAllText(maOutFile, maHeaderLine2);

The above code works fine. My concern is that it's not very efficient, especially over a slow network. It opens and closes the file every time it writes to it.

So, to make this run faster, I thought I would try StreamWriter.

My StreamWriter code looks like this:


// Designate an output file --- put it in same directory as the original files
// give MA file same name as the original file -- with different extension.

// maOutFile = dir + "\\" + serialNumber[f] + ".ma";

StreamWriter outfile;
outfile = dir + "\\" + serialNumber[f] + ".ma";

//Create MA header lines and write to file.

string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";

// Open up the file for writing
//File.WriteAllText(maOutFile, maHeaderLine1);
//File.AppendAllText(maOutFile, maHeaderLine2);
outfile.WriteLine(maHeaderLine1);
outfile.WriteLine(maHeaderLine2);

Visual Studio doesn't like my new code using StreamWriter. It puts a red line under everything to the right of the equals sign in the line "outfile = dir + "\" + serialNumber[f] + ".ma";

When I hover my mouse over the redlined code, the message is "cannot implicitly convert type string to System.IO.StreamWriter".

What doesn't it like about my new code?

You are assigning a string variable to a StreamWriter object instead of creating a StreamWriter object.

Try the following

using (StreamWriter sw = new StreamWriter(dir + "\\" + serialNumber[f] + ".ma"))
{
string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";

// Open up the file for writing
//File.WriteAllText(maOutFile, maHeaderLine1);

sw.WriteLine(maHeaderLine1);
sw.WriteLine(maHeaderLine2);
}

You're trying to assign a string to a StreamWriter here:

outfile = dir + "\\" + serialNumber[f] + ".ma";

And looking at the code you're trying to open a file as a stream, something along the lines of:

string outFileDir = dir + "\\" + serialNumber[f] + ".ma";
StreamWriter outFile = new StreamWriter(outFileDir);
 string fileName = dir + "\\" + serialNumber[f] + ".ma" ;  
try  
   {  
   using (StreamWriter writer = new StreamWriter(fileName))  
      {  
        string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
        string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";
        writer.Write(maHeaderLine1 + maHeaderLine2);  
      }  
   }  
catch(Exception exp)  
   {  
      Console.Write(exp.Message);  
   }

}

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