【问题标题】:file.Move without renaming itfile.Move 不重命名
【发布时间】:2014-06-25 22:39:17
【问题描述】:

我正在编写一个程序,它应该读出 mp3 文件的 id3 标签,创建一个以艺术家命名的目录,然后我想将 mp3 文件移动到特定的艺术家目录中。

当我尝试移动 Mp3 文件时,它不会将它移动到我创建的音乐目录的子文件夹(命名为艺术家)中。 我只想移动 Mp3 文件,而不是重命名它们。

这是我的代码:

   public void moveFiles(string path, string[] title, string[] artist,string [] songs)
    {//loop through the title array
        for(int i=0;i<title.Length;i++)
        {// no artist no name
            if (artist[i] == null)
            { 
                i += 1;
            }//check if sourceFile is existing
            if (File.Exists(songs[i]))
            {//check if destinationFile is existing
                if (File.Exists((@"C:\Musik\" + artist[i] + songs[i])))
                {//if delete
                    File.Delete((@"C:\Musik\" + artist[i] + songs[i]));
                } 
                else
                { //move file from songs[i](sourcePath)to (destinationPath)
                    File.Move(songs[i],(@"C:\Musik\" + artist[i] + songs[i]));                  
                    MessageBox.Show("Das Lied " + title[i] + " wurde erfolgreich verschoben");
                }
            }
            else
            {
                MessageBox.Show(songs[i]+" does not exist!");
            }
        }
    }

它只会将我的文件移动到 C:\Musik 目录中,它会像 Artist-Song 一样重命名我的文件; 欢迎任何帮助。 谢谢:)

【问题讨论】:

  • 提示:您不需要File.ExistsIf the file to be deleted does not exist, no exception is thrown. 还有一个:使用i++ 而不是i += 1

标签: c# id3-tag file-move


【解决方案1】:

您的代码中遗漏了一个反斜杠(“\”)。

// By popular suggestion, using Path.Combine...
const string dstRootDirectoryName = @"C:\Musik";
var destinationFileName = Path.Combine(dstRootDirectoryName, artist[i], songs[i]);
if (File.Exists(destinationFileName)
{
    File.Delete(destinationFileName);
}
else
{
    File.Move(songs[i], destinationFileName);
    MessageBox.Show("The file:" + title[i] + " was moved");
}

【讨论】:

  • Path.Combine 会更好。而songs[i] 可能包含完整的路径,而不仅仅是文件名。
  • @RaphaëlAlthaus 我们假设它是一条完整路径,因为它的“工作”是File.Move 的第一个参数
  • @crashmstr 同意。使用Path.Combine() 方法更新了答案。谢谢你的建议。
  • @EvanL 有一个 4 字符串参数覆盖。 doc
  • Takes up to 4params string[] paths 在 .Net 4 及更高版本中。
【解决方案2】:
File.Move(songs[i],(@"C:\Musik\" + artist[i] + songs[i]));

在文件夹之间添加斜线:

File.Move(songs[i],String.Format("C:\Musik\{0}/{1}", artist[i], songs[i]));

另外,考虑使用 String.Format 代替通常的字符串连接,它看起来更易读 IMO。

【讨论】:

  • 或者让 .Net 为您完成工作并使用Path.Combine
【解决方案3】:

您在路径中遗漏了一个反斜杠。我强烈建议也将Path.Combine 用于此类事情。它使它更具可读性,并且比纯字符串连接更简洁。 Path.GetFileName() 也非常有用……知道/喜欢它;)

File.Move(songs[i], Path.Combine(@"C:\Musik", Path.Combine(artist[i], Path.GetFileName(songs[i]));

更新

如果您的目标是 .Net 4.0 或更高版本,您可以使用 4 字符串覆盖来使其更清晰(如建议中所指出的那样)。如果您的目标是 3.5 或更低,请使用顶级解决方案。

File.Move(songs[i], Path.Combine(@"C:", "Musik", artist[i], Path.GetFileName(songs[i]));

【讨论】:

  • 除了@"C:\Musik\" + artist[i],它应该是@"C:\Musik\", artist[i],而不是完全使用Path.Combine
  • 我针对 3.5 或更低版本或 4.0 或更高版本进行了编辑。
  • +1 通过针对多个版本的 .NET Framework。
  • 如果您使用的是 3.5 或更低版本,请链接调用,以便您始终使用 Path.Combine 而不是使用 +。
  • @crashmstr 根据您的建议进行了清理。
猜你喜欢
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 2016-05-29
  • 2022-08-14
  • 1970-01-01
  • 2011-10-25
相关资源
最近更新 更多