[英]file.Move without renaming it
Im coding a programm, which should read out the id3 tags of mp3 files, create a Directory named like the artist and then i want to move the mp3 files into the specific artist directory. 我编写一个程序,它应该读出mp3文件的id3标签,创建一个名为艺术家的目录,然后我想将mp3文件移动到特定的艺术家目录。
When im trying to Move the Mp3 file, it doesnt move it into the subfolder(named like the Artist) of my Musik Directory, which i created. 当我试图移动Mp3文件时,它不会将其移动到我创建的Musik目录的子文件夹(命名为艺术家)。 I just want to Move the Mp3 files, not rename them. 我只想移动Mp3文件,而不是重命名它们。
here is my code: 这是我的代码:
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!");
}
}
}
It only moves my files into the C:\\Musik Directory and it's renaming my files like Artist-Song; 它只将我的文件移动到C:\\ Musik目录中,它重命名我的文件,如Artist-Song; Any help is welcome. 欢迎任何帮助。 Thanks:) 谢谢:)
You have missed a backslash in your path. 你错过了路径中的反斜杠。 I would strongly suggest using Path.Combine
for this type of thing too. 我强烈建议将Path.Combine
用于此类事情。 It makes it far more readable and is a cleaner way than pure string concatenation. 它使它更具可读性,并且比纯字符串连接更简洁。 Also Path.GetFileName()
is super useful... know it / love it ;) Path.GetFileName()
也非常有用......知道/喜欢它;)
File.Move(songs[i], Path.Combine(@"C:\Musik", Path.Combine(artist[i], Path.GetFileName(songs[i]));
UPDATE UPDATE
If you are targeting .Net 4.0 or higher you can use the 4 string override to make this cleaner (as is pointed out in the suggestions). 如果您的目标是.Net 4.0或更高版本,则可以使用4字符串覆盖来使其更清晰(如建议中所述)。 If you are targeting 3.5 or lower, use the top solution. 如果您的目标是3.5或更低,请使用顶级解决方案。
File.Move(songs[i], Path.Combine(@"C:", "Musik", artist[i], Path.GetFileName(songs[i]));
You have missed one backslash ("\\") in your code. 你的代码中错过了一个反斜杠(“\\”)。
// 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");
}
File.Move(songs[i],(@"C:\Musik\" + artist[i] + songs[i]));
Add a slash between the folders: 在文件夹之间添加斜杠:
File.Move(songs[i],String.Format("C:\Musik\{0}/{1}", artist[i], songs[i]));
Also, consider using String.Format instead of the usual string concatenation, it looks more readeable IMO. 另外,考虑使用String.Format而不是通常的字符串连接,它看起来更易于转换IMO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.