File.Copy throws IOException: The filename, directory name, or volume label syntax is incorrect

41 Views Asked by At

This is more of a "why is this so" question as I've already worked around the issue.

I'm reading in an M3U playlist using m3uParser. The playlist was created by adding files (located on a local NAS box) to VLC and saving the playlist.

#EXTM3U
#EXTINF:298,Audio File Title
file://server-name/Music/01%20-%20Music%20-%20Can%27t%20Be%20Beat.flac

The purpose is to iterate through the playlist and copy the files to another location. No issues reading in the playlist.

var playlist = M3U.ParseFromFile(@"\\server-name\Music\Test\playlist.m3u");
var testPath = @"\\server-name\Music\Test";

foreach (var m in playlist.Medias)
{
  var fileName = Path.GetFileName(m.MediaFile);
  var dest = Path.Combine(testPath, Uri.UnescapeDataString(fileName));
  
  File.Copy(m.MediaFile, dest);
}

The issue comes with File.Copy. It throws an IOException:

RuntimeMethodInfo
FileSystem.CopyFile (String sourceFullPath, String destFullPath, Boolean overwrite)

The filename, directory name, or volume label syntax is incorrect. : 'C:\Users\user\AppData\Local\Temp\LINQPad8_fkqhopcf\shadow-26\file:\server-name\Music\01%20-%20Music%20-%20Can%27t%20Be%20Beat.flac'

Other SO questions deal with this exception, but the answer is always about making sure the destination specifies a file. In my case, I'm already taking care of defining a file as the destination. I also note the Copy method isn't complaining about file not found.

Is this just a case where Windows just can't handle the file location as defined in the playlist? My workaround assumes this is the case and reformats the media file into a "correct" network path.

1

There are 1 best solutions below

1
علی حق On

You are fileName Or dest is url , File.Copy Not support Url

Maybe this code will work for you


WebClient client = new WebClient();

var playlist = M3U.ParseFromFile(@"\\server-name\Music\Test\playlist.m3u");
var testPath = @"\\server-name\Music\Test";

foreach (var m in playlist.Medias)
{
  var fileName = Path.GetFileName(m.MediaFile);
  var dest = Path.Combine(testPath, Uri.UnescapeDataString(fileName));
  
 client.DownloadFile(fileName , dest);
}