考虑下面的foreach循环:
foreach (FileInfo fileInfo in files)
{
string fileName = fileInfo.ToString();
fileName = fileName.Split('_')[0]; // File Suffix
string sqlString = "SELECT 'company-plc.' + FTPUser FROM dbo.Control WHERE Brand = @fileName;";
SqlConnection connection = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sqlString, connection);
cmd.Parameters.AddWithValue("@fileName", fileName);
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string destinationSuffix = reader[0].ToString();
string fullPath = Path.Combine(destinationPath,destinationSuffix);
fullPath = Path.Combine(fullPath, fileInfo.FullName);
File.Move(fileInfo.FullName,fullPath);
}
reader.Close();
}
当前fileInfo位置:\\server\directory
destinationPath
= \\server\folder\folder
在SQL查询之后,destinationSuffix
= company-plc.test
正在移动fileInfo
的文件称为test.csv
。
目前的结果:
文件未移动,创建了.test
扩展名的文件,即
\\server\directory\company-plc.test
期望的结果:
\\server\folder\folder\company-plc.test\test.csv
任何人都可以看到Path.Combine
错误的地方吗?
答案 0 :(得分:3)
检查Path.Combine的文档 - 你会发现如果第二个参数包含一个完整的路径名 - 那将是返回的内容。请尝试使用fileInfo.Name。
fullPath = Path.Combine(fullPath, fileInfo.Name);
https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx