【问题标题】:Copy file from portable class library to Windows 8 - SQLite database将文件从可移植类库复制到 Windows 8 - SQLite 数据库
【发布时间】:2014-03-29 21:45:01
【问题描述】:

我有一个 SQLite 数据库文件,我需要将它复制到将要使用它的平台上,而且我有点笨拙。我已经看到了有关如何读取文本的示例,例如从 xml 文件中读取文本,但我只需要获取一个流并复制它,但我找不到我需要使用哪个类。

这是我目前所拥有的:

public async Task CopyDatabase()
    {
        var s = Assembly.Load(new AssemblyName("PhenotypesLibrary")).GetManifestResourceStream(DBPath.DBPathAsString());

        ////create a folder and file
        var folder = ApplicationData.Current.LocalFolder;
        var file = await folder.CreateFileAsync(DBPath.DBPathAsString());


        using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
            {
                using (StreamWriter writer = new StreamWriter(outputStream.AsStreamForWrite()))
                {
                    await writer.//ummmmmmmmm
                }
                //using (DataWriter dataWriter = new DataWriter(outputStream))
                //{ 

                //    var bytes = 
                //    dataWriter.WriteBytes
                //}
            }
        }

        // the way I did it from a file that was put in the project folder, not in a PCL
        //string path = DBPath.DBPathAsString();
        //bool copy = false;
        //try
        //{
        //    StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(path);
        //}
        //catch 
        //{
        //    copy = true;
        //}

        //if (copy)
        //{
        //    var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///" + path));
        //    await file.CopyAsync(ApplicationData.Current.LocalFolder);
        //}
    }

【问题讨论】:

    标签: sqlite portable-class-library


    【解决方案1】:

    我找到了答案。我不太清楚如何为 PCL 做目录,所以我把文件放到了 PCL 的根目录中。

        public async Task CopyDatabase()
        {
            if (await CheckFileExists()) return;
    
            Assembly assembly = Assembly.Load(new AssemblyName("PhenotypesLibrary"));
            var s = assembly.GetManifestResourceStream(DBPath.FullDBPath);
    
            DataReader dataReader = new DataReader(s.AsInputStream());
            await dataReader.LoadAsync((uint)s.Length);
            byte[] buffer = new byte[(int)s.Length];
            dataReader.ReadBytes(buffer);
    
            var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(DBPath.DBPathAsString);
            await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);
    
        }
    
        private static async Task<bool> CheckFileExists()
        {
            bool fileExists = false;
            try
            {
                var alreadyCopiedFile = await ApplicationData.Current.LocalFolder.GetFileAsync(DBPath.DBPathAsString);
                fileExists = true;
            }
            catch { }
            return fileExists;
        }
        /// <summary>
        /// The path to the database.
        /// </summary>
        public static string DBPathAsString = @"phenotypes_database.sqlite";
    
        /// <summary>
        /// The Path to the file for the Assembly.GetManifestResourceStream method.
        /// </summary>
        public static string FullDBPath = "PhenotypesLibrary.Database.phenotypes_database.sqlite";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多