私はそれが混乱しているコードを整理しようとしています!この時点での最初の、そして私の最大の問題は、私のStreamWritersまたはStreamReaderの1つが開いたままになっていることです。 this linkを使用して、自分のコードを整理しようとしています。しかし、私の問題は、私は、私はそれを閉じるべき場所を確認していないということです。StreamWriterとStreamReaderを閉じる最善の方法
私のコードは次のとおりです。
public static void ProcessFile(string[] ProcessFile, int id_customer, string directoryinprocess)
{
StreamWriter Writer = null, Writer2 = null, Writer3 = null;
foreach (string filename in ProcessFile)
{
// Used for the output name of the file
var dir = Path.GetDirectoryName(filename);
var fileName = Path.GetFileNameWithoutExtension(filename);
var ext = Path.GetExtension(filename);
var folderbefore = Path.GetFullPath(Path.Combine(dir, @"..\"));
int rowCount = 0;
string path_body_out = "";
string outputname = folderbefore + "output_temp\\" + fileName;
if (filename.Contains("RO_"))
{
Writer = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext) { AutoFlush = true };
Writer2 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_body_out" + ext) { AutoFlush = true };
path_body_out = dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext;
} // end of if
else
{
Writer3 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_out" + ext) { AutoFlush = true };
} // end of else
using (StreamReader Reader = new StreamReader(@filename))
{
while (!Reader.EndOfStream)
{
string inputLine = string.Empty;
inputLine = Reader.ReadLine();
rowCount++;
if (filename.Contains("RO_"))
{
if (rowCount <= 4)
{
Writer.WriteLine(inputLine);
}
if (rowCount >= 5)
{
Writer2.WriteLine(inputLine);
}
}
else
{
{ Writer3.WriteLine(inputLine); }
}
} // end of the while
} // end of using Stremreader
if (path_body_out.Contains("_hd_intermediate"))
{
ManipulateHeaderFilesTypeRo(dir, path_body_out);
}
else
{ }
} // end of the foreach
string[] extensions = { "_fv", "_body", "_out" };
string[] fileEntriesout = System.IO.Directory.EnumerateFiles(directoryinprocess, "*.csv", System.IO.SearchOption.AllDirectories)
.Where(file => extensions.Any(ex => Path.GetFileNameWithoutExtension(file).EndsWith(ex)))
.ToArray();
foreach (string filenameout in fileEntriesout)
{
string destinytablename = null;
if (filenameout.Contains("_hd_intermediate_fv"))
{ destinytablename = "TBL_DATA_TYPE_RO_HEADER"; }
else if (filenameout.Contains("_body_out"))
{ destinytablename = "TBL_DATA_TYPE_RO_BODY"; }
else
{ destinytablename = "TBL_DATA_TYPE_LOAD"; }
string id_file = Get_id_file(filenameout, id_customer);
DataTable csvFileData = GetDataTabletFromCSVFile(filenameout, id_file);
InsertDataIntoSQLServerUsingSQLBulkCopy(csvFileData, destinytablename);
} // end of the foreach
//} // end of the foreach
} // end of ProcessFile
質問: はどのように私はパーツを閉じる必要があります。
if (filename.Contains("RO_")) { Writer = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext) { AutoFlush = true }; Writer2 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_body_out" + ext) { AutoFlush = true }; path_body_out = dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext; } // end of if else { Writer3 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_out" + ext) { AutoFlush = true }; } // end of else using (StreamReader Reader = new StreamReader(@filename)) { while (!Reader.EndOfStream) { string inputLine = string.Empty; inputLine = Reader.ReadLine(); rowCount++; if (filename.Contains("RO_")) { if (rowCount <= 4) { Writer.WriteLine(inputLine); } if (rowCount >= 5) { Writer2.WriteLine(inputLine); } } else { { Writer3.WriteLine(inputLine); }
ここで閉じる必要がありますか?
if (filename.Contains("RO_"))
{
Writer = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext) { AutoFlush = true };
Writer2 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_body_out" + ext) { AutoFlush = true };
path_body_out = dir + "\\" + "output_temp\\" + fileName + "_hd_intermediate" + ext;
} // end of if
else
{
Writer3 = new StreamWriter(dir + "\\" + "output_temp\\" + fileName + "_out" + ext) { AutoFlush = true };
} // end of else
ここですか?すべてのStreamWriter
インスタンスがusing()
に包まれることができるようにあなたは、このコードを再編成することができない場合
if (filename.Contains("RO_"))
{
if (rowCount <= 4)
{
Writer.WriteLine(inputLine);
}
if (rowCount >= 5)
{
Writer2.WriteLine(inputLine);
}
}
else
{
{ Writer3.WriteLine(inputLine); }
}
うん、それは間違いない!今、それが働いている、私はこの混乱を改善するために働く、今私は今これが問題です。ありがとうございました。 –