コード例 #1
0
        private void PlatformWriteFile(string path, string contents)
        {
            if (FileExists(path))
            {
                NSFileHandle handle = NSFileHandle.OpenUpdateUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error);

                if (error != null)
                {
                    throw new NSErrorException(error);
                }
                else
                {
                    handle.SeekToEndOfFile();
                    handle.WriteData(NSData.FromString(contents));
                    handle.CloseFile();
                }
            }
            else
            {
                CreateFile(path, NSData.FromString(contents));
            }
        }
コード例 #2
0
        public void WriteFile()
        {
            try
            {
                // Gets the direct path to the file
                NSString folderPath = dataPath.AppendPathComponent(new NSString("testfile.txt"));

                // The text who should be written
                NSString someText = new NSString("Test");

                // The data
                NSData data = someText.Encode(NSStringEncoding.UTF8);

                // Handle the data and writes it to the specific path
                NSFileHandle nsfh = NSFileHandle.OpenWrite(folderPath);

                // Writes the data
                nsfh.WriteData(data);
            }
            catch (Exception ex)
            {
                // Failed write data into a file
            }
        }
コード例 #3
0
        public void WriteFile(string path, string userIdentity, string contents)
        {
            if (!string.IsNullOrEmpty(path) || !string.IsNullOrEmpty(contents))
            {
                if (!NSFileManager.DefaultManager.FileExists(path))
                {
                    var dict = new NSMutableDictionary();
                    UIApplication.SharedApplication.InvokeOnMainThread(() =>
                    {
                        NSFileManager.DefaultManager.CreateFile(path, NSData.FromString(contents), dict);
                    });
                    _fileProtectionManagerService.ProtectFile(path, userIdentity);
                    _fileProtectionManagerService.EncryptFile(path, userIdentity);
                }
                else
                {
                    NSFileHandle fileHandle = null;
                    NSError      error      = null;
                    Exception    exception  = null;

                    try
                    {
                        UIApplication.SharedApplication.InvokeOnMainThread(() =>
                        {
                            _fileProtectionManagerService.DecryptFile(path);
                            fileHandle = NSFileHandle.OpenUpdateUrl(NSUrl.CreateFileUrl(new[] { path }), out error);
                            fileHandle.SeekToEndOfFile();
                            fileHandle.WriteData(NSData.FromString(contents));

                            _fileProtectionManagerService.ProtectFile(path, userIdentity);
                            _fileProtectionManagerService.EncryptFile(path, userIdentity);
                        });
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                    finally
                    {
                        if (fileHandle != null)
                        {
                            UIApplication.SharedApplication.InvokeOnMainThread(() =>
                            {
                                fileHandle.CloseFile();
                            });
                        }
                    }

                    if (error != null)
                    {
                        _loggingService.LogError(typeof(FileSystemService), new Exception(error.DebugDescription), error.Description);
                        throw new NSErrorException(error);
                    }
                    else if (exception != null)
                    {
                        _loggingService.LogError(typeof(FileSystemService), exception, exception.Message);
                        throw exception;
                    }
                }
            }
        }