我想把新创建的文件名保存到一个变量中,这样一旦上传任务完成,我就可以把它作为参数传递给它。
“DateTimeFormat”是在调用任务时动态生成的,因此文件名必须是一个变量。
var dateTimeFormat = now.ToString(_tsFormat); //special formatting of date, can be any format for demo
if (results != null)
{
try
{
using (XmlWriter writer = XmlWriter.Create($"{_configuration.Value.FileLocation}InventoryImportPart-{dateTimeFormat}.xml"))
{
filename = Path.GetFileName(writer); //trying to save file name
writer.WriteStartDocument();
foreach (var item in results)
{
XNode productNode = JsonConvert.DeserializeXNode(item.ToString(), "data");
productNode.WriteTo(writer);
}
writer.WriteEndDocument();
//Code calling upload task with filename as parameter
}
}
catch (Exception e)
{
_logger.LogError($"Exception when getting CosmosDB events: {e}");
}
}
应该很容易,但我却一片空白。
只需切换:生成文件名并记住在变量中,然后在XmlWriter.create
中使用变量:
filename = $"{_configuration.Value.FileLocation}InventoryImportPart-{dateTimeFormat}.xml";
using (XmlWriter writer = XmlWriter.Create(filename))
{
...
}