MilkBottle
MilkBottle

Reputation: 4332

How to save a CSV file using FolderPicker

I have this problem to save csv file.

private async void btnPrint_Click(object sender, RoutedEventArgs e)
{
    var picker = new Windows.Storage.Pickers.FolderPicker();
    picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
    picker.FileTypeFilter.Add(".csv");//add any extension to avoid exception

    var folder = await picker.PickSingleFolderAsync();

    if (folder != null)
    {
        string strFilename = strBeginDate + "_To_" + EndDate + ".csv";

        StorageFile file = await folder.CreateFileAsync("Rpt" + strFilename, CreationCollisionOption.ReplaceExisting);

        foreach (var m in FlightData)
        {
            sb.Append(m.Id + "," + m.Date + "," + m.Price + "," + ...... + "\r\n");
        }

        await FileIO.AppendLinesAsync(file, new List<string>() { sb.ToString() });
    }
}

The above cause error:

"The filename, directory name, or volume label syntax is incorrect.\r\n"

If I use this, there is no problem.

StorageFile file = await folder.CreateFileAsync("sample.csv", CreationCollisionOption.ReplaceExisting);

Upvotes: 0

Views: 356

Answers (1)

Bart
Bart

Reputation: 10015

Since you're using dates in your filename, you're most likely using /, e.g. 2015/01/01. This is an invalid file name character, replace the / with a - or remove it completely and your code will work.

I've placed a small repro project on GitHub to show you the error and fix in action.

Upvotes: 1

Related Questions