C# 3.0 将文件附件表示为列表的更好方法(c#3.0)

C# 3.0 将文件附件表示为列表的更好方法(c#3.0),c#-3.0,refactoring,C# 3.0,Refactoring,我写过 List<Attachment> lstAttachment = new List<Attachment>(); //Check if any error file is present in which case it needs to be send if (new FileInfo(Path.Combine(errorFolder, errorFileName)).Exists) {

我写过

List<Attachment> lstAttachment = new List<Attachment>();

            //Check if any error file is present in which case it needs to be send
            if (new FileInfo(Path.Combine(errorFolder, errorFileName)).Exists)
            {
                Attachment unprocessedFile = new Attachment(Path.Combine(errorFolder, errorFileName));
                lstAttachment.Add(unprocessedFile);
            }
            //Check if any processed file is present in which case it needs to be send
           if (new FileInfo(Path.Combine(outputFolder, outputFileName)).Exists)
            {
                Attachment processedFile = new Attachment(Path.Combine(outputFolder, outputFileName));
                lstAttachment.Add(processedFile);
            }
List lstatchment=新列表();
//检查是否存在任何错误文件,在这种情况下需要发送该文件
如果(新文件信息(Path.Combine(errorFolder,errorFileName)).Exists)
{
附件未处理文件=新附件(Path.Combine(errorFolder,errorFileName));
lstaachment.Add(未处理的文件);
}
//检查是否存在任何已处理的文件,在这种情况下需要发送该文件
如果(新文件信息(Path.Combine(outputFolder,outputFileName)).Exists)
{
Attachment processedFile=新附件(Path.Combine(outputFolder,outputFileName));
lstaachment.Add(processedFile);
}
工作正常,正在提供预期的输出

基本上,我是根据文件是否存在将文件附加到列表中

我正在寻找任何其他优雅的解决方案,而不是我所写的

原因:想学习表示同一程序的不同方式

我正在使用C#3.0

谢谢。

看起来好些吗

...

var lstAttachment = new List<Attachment>();
string errorPath = Path.Combine(errorFolder, errorFileName);
string outputPath = Path.Combine(outputFolder, outputFileName);

AddAttachmentToCollection(lstAttachment, errorPath);
AddAttachmentToCollection(lstAttachment, outputPath);

...

public static void AddAttachmentToCollection(ICollection<Attachment> collection, string filePath)
{
    if (File.Exists(filePath))
    {
        var attachment = new Attachment(filePath);
        collection.Add(attachment);
    }
}
。。。
var lstAttachment=新列表();
字符串errorPath=Path.Combine(errorFolder,errorFileName);
字符串outputPath=Path.Combine(outputFolder,outputFileName);
AddAttachmentToCollection(lstAttachment,errorPath);
AddAttachmentToCollection(lstAttachment,outputPath);
...
公共静态void AddAttachmentToCollection(ICollection集合,字符串文件路径)
{
if(File.Exists(filePath))
{
var附件=新附件(文件路径);
收集。添加(附件);
}
}
它看起来更好吗

...

var lstAttachment = new List<Attachment>();
string errorPath = Path.Combine(errorFolder, errorFileName);
string outputPath = Path.Combine(outputFolder, outputFileName);

AddAttachmentToCollection(lstAttachment, errorPath);
AddAttachmentToCollection(lstAttachment, outputPath);

...

public static void AddAttachmentToCollection(ICollection<Attachment> collection, string filePath)
{
    if (File.Exists(filePath))
    {
        var attachment = new Attachment(filePath);
        collection.Add(attachment);
    }
}
。。。
var lstAttachment=新列表();
字符串errorPath=Path.Combine(errorFolder,errorFileName);
字符串outputPath=Path.Combine(outputFolder,outputFileName);
AddAttachmentToCollection(lstAttachment,errorPath);
AddAttachmentToCollection(lstAttachment,outputPath);
...
公共静态void AddAttachmentToCollection(ICollection集合,字符串文件路径)
{
if(File.Exists(filePath))
{
var附件=新附件(文件路径);
收集。添加(附件);
}
}

来一点LINQ怎么样

var filenames = new List<string>() 
{
    Path.Combine(errorFolder, errorFilename),
    Path.Combine(outputFolder, outputFilename)
};
var attachments = filenames.Where(f => File.Exists(f))
                           .Select(f => new Attachment(f));
var filenames=新列表()
{
组合(errorFolder,errorFilename),
Path.Combine(outputFolder,outputFilename)
};
var attachments=文件名。其中(f=>File.Exists(f))
.选择(f=>新附件(f));

来一点LINQ怎么样

var filenames = new List<string>() 
{
    Path.Combine(errorFolder, errorFilename),
    Path.Combine(outputFolder, outputFilename)
};
var attachments = filenames.Where(f => File.Exists(f))
                           .Select(f => new Attachment(f));
var filenames=新列表()
{
组合(errorFolder,errorFilename),
Path.Combine(outputFolder,outputFilename)
};
var attachments=文件名。其中(f=>File.Exists(f))
.选择(f=>新附件(f));

我非常喜欢LINQ的表现力和优雅;有关示例,请参见我的答案。:)我真的很喜欢LINQ的表现力和优雅;有关示例,请参见我的答案。:)