如何从文件路径

时间:2017-08-25 14:09:38

标签: c# uwp

我有一个字符串数组fileContentArray,它包含一个filepath列表。

fileContentArray[0]    D:\Downloads\KuGou\1.mp3
fileContentArray[1]    E:\Downloads\2.mp3
fileContentArray[2]    F:\KuGou\3.mp3

并且

List<StorageFile> files = new List<StorageFile>();

我想将文件添加到列表中

private async Task ReadListAsync()
        {
            StorageFolder sFolder = ApplicationData.Current.LocalFolder;
            StorageFile sf = await sFolder.CreateFileAsync("List.txt", CreationCollisionOption.OpenIfExists);
            using (IRandomAccessStream readStream = await sf.OpenAsync(FileAccessMode.Read))
            {
                using (DataReader dataReader = new DataReader(readStream))
                {
                    UInt64 size = readStream.Size;
                    if (size <= UInt32.MaxValue)
                    {
                        UInt32 numBytesLoaded = await dataReader.LoadAsync((UInt32)size);
                        string fileContent = dataReader.ReadString(numBytesLoaded);
                        string[] fileContentArray = fileContent.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        List<StorageFile> files = new List<StorageFile>();
                        foreach (string filepath in fileContentArray)
                        {
                            StorageFile sff = await StorageFile.GetFileFromPathAsync(filepath);
                            files.Add(sff);
                        }

                        await AddStorageFilestoList(files);
                    }
                }
            }
        }

但是当我运行代码时,它会在GetFileFromPathAsync时告诉我错误。

  

{System.UnauthorizedAccessException:拒绝访问。

     

在   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()   在MP3Tag.Views.MasterDetailPage.d__31.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()
  在MP3Tag.Views.MasterDetailPage.d__10.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()
  在MP3Tag.Views.MasterDetailPage.d__9.MoveNext()}

在我的另一个应用程序中,几乎相同的代码可用。

string strLastFile = (Application.Current as App).localSettings.Values["LastFile"] as string;
                if (strLastFile != null)
                {
                    try
                    {
                        file = await StorageFile.GetFileFromPathAsync(strLastFile);
                        await LoadFileAsync(file);
                    }
                    catch
                    {
                        //文件不存 
                    }
                }

它只是一个本地应用程序,读取一个txt文件,其中包含一个文件路径列表。 我想显示一些有关文件的信息。 如何以正确的方式使用它? THX。

0 个答案:

没有答案
相关问题