Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows应用商店应用程序如何从文件中删除文本_C#_Windows Store Apps_Storagefile - Fatal编程技术网

C# Windows应用商店应用程序如何从文件中删除文本

C# Windows应用商店应用程序如何从文件中删除文本,c#,windows-store-apps,storagefile,C#,Windows Store Apps,Storagefile,在windows应用商店应用程序中,如何从文件中删除文本?比如说 如果我有 StorageFile file = await roamingfolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists); await FileIO.AppendTextAsync(file, temp); 如何从该文件中删除一些文本?您通常将文本读入字符串,删除文本,然后重写文件。这里我得到一个文件,然后将内容放入string

在windows应用商店应用程序中,如何从文件中删除文本?比如说

如果我有

StorageFile file = await roamingfolder.CreateFileAsync(filename,
    CreationCollisionOption.OpenIfExists);
await FileIO.AppendTextAsync(file, temp);

如何从该文件中删除一些文本?

您通常将文本读入字符串,删除文本,然后重写文件。

这里我得到一个文件,然后将内容放入stringbuilder,然后执行一些字符串操作,最后使用DataWriter将字符串放回文件

public static async Task UpdateTextContent(string contentItemId)
        {
            var storageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(TARGET_FOLDER);
            StorageFile sf = null;
            try
            {
                //get content of the file make sure that it exist
                sf = await storageFolder.GetFileAsync(TARGET_FILE);
            }
            catch (Exception ex)
            {

            }

            if (sf != null)
            {
                var targettxtfile = await Windows.Storage.FileIO.ReadTextAsync(sf);


                var sbProcessedTextToWrite = new StringBuilder(targettxtfile);



                if (targettxtfile.IndexOf(contentItemId) >= 0)
                {
                    string startmarker = new StringBuilder("[").Append(contentItemId).Append("#start]").ToString();
                    string endmarker = new StringBuilder("[").Append(contentItemId).Append("#end]").ToString();

                    int start = sbProcessedTextToWrite.ToString().IndexOf(startmarker);
                    int end = sbProcessedTextToWrite.ToString().IndexOf(endmarker);

                    int slen = end + endmarker.Length - start; 

                    //compute position to remove
                    sbProcessedTextToWrite.Remove(start, slen);
                }

                using (IRandomAccessStream fileStream = await sf.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
                    {
                        using (DataWriter dataWriter = new DataWriter(outputStream))
                        {

                            dataWriter.WriteString(sbProcessedTextToWrite.ToString());                            

                            await dataWriter.StoreAsync();

                            // For the in-memory stream implementation we are using, the flushAsync call 
                            // is superfluous,but other types of streams may require it.
                            await dataWriter.FlushAsync();

                            // In order to prolong the lifetime of the stream, detach it from the 
                            // DataWriter so that it will not be closed when Dispose() is called on 
                            // dataWriter. Were we to fail to detach the stream, the call to 
                            // dataWriter.Dispose() would close the underlying stream, preventing 
                            // its subsequent use by the DataReader below.
                            dataWriter.DetachStream();
                        }

                        //same here flush the outputStream as well
                        await outputStream.FlushAsync();
                    }
                }
            }


        }