gpt4 book ai didi

c# - 关闭 StreamWriter 和 StreamReader 最好最正确的方法

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:07 24 4
gpt4 key购买 nike

我一直在努力整理一个代码,真是乱七八糟!此时第一个也是我最大的问题是我的一个 StreamWriters 或 StreamReader 处于打开状态。使用this link ,我正在尝试组织我的代码。但我的问题是我不确定应该在哪里关闭它:

我的代码是:

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

还是在这里?

                if (filename.Contains("RO_"))
{
if (rowCount <= 4)
{
Writer.WriteLine(inputLine);
}
if (rowCount >= 5)
{
Writer2.WriteLine(inputLine);
}
}
else
{
{ Writer3.WriteLine(inputLine); }
}

最佳答案

如果您不能重新组织此代码以便每个 StreamWriter 实例都可以包装在 using() 中,那么也许您可以这样做:

StreamWriter Writer = null, Writer2 = null, Writer3 = null;

try
{
// your existing code
}
catch
{
// Handle
}
finally
{
if (Writer != null)
Writer.Close();
if (Writer2 != null)
Writer2.Close();
if (Writer3 != null)
Writer3.Close();
}

这确保无论 try 中发生什么错误,您的编写器都将被关闭。

在我看来,有条件地实例化对象是一种味道,您应该根据 filename.Contains("RO_") 进行不同的实现。您可以使用策略模式并使用不同的文件处理器接口(interface)实现,根据文件名选择正确的接口(interface)实现。每个实现只会知道如何写入它需要的位置。这将允许您在每个编写器周围正确使用 using()

关于c# - 关闭 StreamWriter 和 StreamReader 最好最正确的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45151106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com