从ApplicationData.Current.LocalFolder切换到Knownfolders.DocumentsLibrary

时间:2016-12-30 23:01:40

标签: c# csv uwp document-library

我正在为gravesite的访客自助服务终端开发一个应用程序,其中一个功能从csv文件中提取数据并使用所述数据填充列表视图。以下代码用于获取数据并将其以列表类型的形式返回。

    public  class Grave 
    {
        public string plots { get; set; }
        public string DOBS { get; set; }
        public string lastNames { get; set; }
        public string firstNames { get; set; }
        public string companys { get; set; }
        public string regts { get; set; }
        public string unitTypes { get; set; }
        public string states { get; set; }
        public string ranks { get; set; }
        public string sections { get; set; }
        public string image { get; set; }
        public string text { get; set; }
        public string notenums { get; set; }
     }



public class GraveManager
{



    public  static  List<Grave> GetGrave() 
    {   //points to desired folder
        StorageFolder folder = ApplicationData.Current.LocalFolder;

        string csvPath = folder.Path + @"\PGexcel.csv";
        //loads .csv file from folder
        Csv csv = new Csv();

        //property that tells csv parser to not treat the first row as data
        csv.HasColumnNames = true;

        bool success1;
        success1 = csv.LoadFile(csvPath);

        //name of columns
        string plot = "Plot", DOB = "Date_of_Death", lastName = "Last_Name", firstName = "First_Name", company = "Company", regt = "Regt", state = "State", unitType = "Unit_Type", Rank = "Rank", Section = "Section", Notables= "Notables";


        //initialize string of arrays for each column
        string[] OCplots = new string[csv.NumRows];
        string[] OCDOBS = new string[csv.NumRows];
        string[] OClastNames = new string[csv.NumRows];
        string[] OCfirstNames = new string[csv.NumRows];
        string[] OCcompanys = new string[csv.NumRows];
        string[] OCregts = new string[csv.NumRows];
        string[] OCunitTypes = new string[csv.NumRows];
        string[] OCstates = new string[csv.NumRows];
        string[] OCranks = new string[csv.NumRows];
        string[] OCsections = new string[csv.NumRows];
        string[] OCimage = new string[csv.NumRows];
        string[] OCtext = new string[csv.NumRows];
        string[] OCnotenums = new string[csv.NumRows];


        //populates the arrays with values from .csv file
        for (int i = 0; i < csv.NumRows; i++)
        {

            OCplots[i] = csv.GetCellByName(i, plot);
            OCDOBS[i] = csv.GetCellByName(i, DOB);
            OClastNames[i] = csv.GetCellByName(i, lastName);
            OCfirstNames[i] = csv.GetCellByName(i, firstName);
            OCcompanys[i] = csv.GetCellByName(i, company);
            OCregts[i] = csv.GetCellByName(i, regt);
            OCunitTypes[i] = csv.GetCellByName(i, unitType);
            OCranks[i] = csv.GetCellByName(i, Rank);
            OCsections[i] = csv.GetCellByName(i, Section);
            OCstates[i] = csv.GetCellByName(i, state);
            OCnotenums[i] = csv.GetCellByName(i, Notables); 
        }



        //concantenate arrays with .jpg .txt to call for corresponding files
        for (int i = 0; i < csv.NumRows; i++)
        {
            //OCimage[i] = "C:/Users/POGR_ADMIN/AppData/Local/Packages/6b3614f6-6a5f-48fc-9687-80291e70b64d_phwtyg9y34v1t/LocalState/" + OCplots[i] + ".jpg";
            OCimage[i] = folder.Path + @"\" + OCplots[i] + ".jpg";
            OCtext[i] = folder.Path + @"\"+ OCplots[i] + ".txt";  
        }

        var graves = new List<Grave>();



        //attempt to populate List using the for loop
        for (int i = 0; i < csv.NumRows; i++)
        {
            graves.Add(new Grave { plots= OCplots[i], DOBS = OCDOBS[i], lastNames = OClastNames[i], firstNames = OCfirstNames[i], companys = OCcompanys[i], regts = OCregts[i], states = OCstates[i], unitTypes = OCunitTypes[i],ranks = OCranks[i], sections = OCsections[i], image = OCimage[i], text = OCtext[i], notenums = OCnotenums[i] });

        }




        return graves;             

    }
}

然后我在另一个页面上初始化一个列表类型grave,并在调用构造函数时通过调用该方法来填充它。这里显示的是:

   public List<Grave> Graves;

   public FindaGrave()
    {
        this.InitializeComponent();
        GraveInitializer();

    }

     public void GraveInitializer()
    {
        Graves = GraveManager.GetGrave();
    }

这段代码完美无缺,并且精确地填充了列表视图。但是,我注意到,由于人为错误,他们找到了csv文件中的许多错误并计划查找。话虽如此,他们希望能够对csv文件进行更改,并且更改将在重新加载应用程序后显示(没有我的任何帮助)。由于打包的本地状态文件夹,我认为适合的唯一方法是更改 StorageFolder folder = ApplicationData.Current.LocalFolder;进入StorageFolder folder = KnownFolders.DocumentsLibrary;

我觉得如果我确定该文件位于文档库中,则在应用程序包清单中添加该功能,如<uap:Capability Name="documentsLibrary" />,并添加相应的文件类型关联。但是,当我运行它时,列表不再显示,并且在我稍微调试之后,folder.Path不再有值,而string csvPath只等于`“\ PGexcel.csv “。我完全失去了,因为我认为这将是一个快速修复最终成为非常困难的东西,而且我是UWP的新手,所以这没有帮助。我已经找了好几天,找不到解决方案,对此事的任何帮助都将不胜感激。你将是一个救生员!!

2 个答案:

答案 0 :(得分:0)

oke我检查了你的代码,问题是UWP应用程序是沙盒类型,只能完全访问ApplicationData。即使您设法在应用程序数据之外获得完整路径,也无法打开它。您需要通过KnownFolders类打开它们或将它们保存在StorageApplicationPermissions类

最好的办法是使用Storiage Api来打开文件流并将其传递给你的csv解析器,但我不确定你的csv解析器是否接受了一个流

答案 1 :(得分:0)

由于UWP应用程序需要安全,因此不允许使用路径直接访问非应用程序数据文件系统位置。

您仍然可以使用StorageFile API,如果您的CSV库支持来自字符串而不是文件的输入,那么它就足够了。

public static async Task<List<Grave>> GetGraveAsync() 
{   
    //points to desired folder
    StorageFolder folder = ApplicationData.Current.LocalFolder;
    //find the file
    StorageFile csvFile = await folder.GetFileAsync( "PGexcel.csv" );


    //loads .csv file from folder
    Csv csv = new Csv();

    //property that tells csv parser to not treat the first row as data
    csv.HasColumnNames = true;

    bool success1;

    //load the CSV data from string
    success1 = csv.LoadFromString( await FileIO.ReadAllText(csvFile) );

    //... rest of your code

    return graves;             
}

StorageApplicationPermissions.FutureAccessList

您问题的最简单解决方案是让用户使用FileOpenPicker在首次启动应用时选择文件,然后使用StorageApplicationPermissions.FutureAccessList API进行存储。此API允许您将访问权限保存到1000个存储项,以便您可以访问用户选择的文件。甚至多次发布应用程序。