Path.Combine和File.Move与文件路径中的点

时间:2016-09-07 15:26:40

标签: c# csv

考虑下面的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错误的地方吗?

1 个答案:

答案 0 :(得分:3)

检查Path.Combine的文档 - 你会发现如果第二个参数包含一个完整的路径名 - 那将是返回的内容。请尝试使用fileInfo.Name。

fullPath = Path.Combine(fullPath, fileInfo.Name);

https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx