[英]Path.Combine and File.Move with dot in file path
考虑以下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();
}
当前文件信息位置: \\\\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
在哪里犯错误?
检查Path.Combine的文档-您将发现,如果第二个参数包含完整的路径名,则将返回该内容。 尝试改用fileInfo.Name。
fullPath = Path.Combine(fullPath, fileInfo.Name);
https://msdn.microsoft.com/zh-CN/library/fyy7a5kt(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.