变量的值未更新

时间:2016-05-10 17:01:33

标签: c# variables

我目前正在开发一个项目,用于扫描附件的邮箱,当找到附件时,它会被放置在用户的目录中。我的问题是,当我检查文件是否存在于路径中时,我会更改附件的名称并添加计数器和时间戳,这样就不会覆盖它。但是,当它进入条件并更改文件名时,它永远不会更新路径变量以包含Clean名称变量的正确值。

string timeProcessed = DateTime.Now.ToString();
byte[] bytefiles = attachment.ContentBytes;
string cleanName = MakeCleanName(userEmail.Subject, attachment.Name);       
string path = employeeStarPath + "\\" + cleanName;
// updated this in order to prevent images with the same name from overwritting eachother.
if (File.Exists(path))
{                                  
    cleanName = Path.GetFileNameWithoutExtension(attachment.Name).ToString()+"(" + counter + ")" + "-(Recieved - " + timeProcessed.Replace(":",".").Replace("/",".") + " )"+ Path.GetExtension(attachment.Name); << this value is not updated in the path variable.

}

现在我知道我可以通过再次调用path = employeeStarPath + "\\" + cleanName;来更新路径变量,但我觉得这会让我的代码有点混乱。

1 个答案:

答案 0 :(得分:2)

我可能不明白你的问题,但你可以只叫“string path = employeeStarPath +”\“+ cleanName;”最后在if?

之前
string timeProcessed = DateTime.Now.ToString();
byte[] bytefiles = attachment.ContentBytes;
string cleanName = MakeCleanName(userEmail.Subject, attachment.Name);       

// updated this in order to prevent images with the same name from overwritting eachother.
if (File.Exists(path))
{                                  
    cleanName = Path.GetFileNameWithoutExtension(attachment.Name).ToString()+"(" + counter + ")" + "-(Recieved - " + timeProcessed.Replace(":",".").Replace("/",".") + " )"+ Path.GetExtension(attachment.Name); << this value is not updated in the path variable.

}

string path = employeeStarPath + "\\" + cleanName;