Beispiel #1
0
        public static string Method1()
        {
            string[] args = new string[] { "-rd1", "-c", "IOPlatformExpertDevice", "|", "grep", "model" };
            NSTask   task = new NSTask();

            task.LaunchPath = @"/usr/sbin/ioreg";
            task.Arguments  = args;

            NSPipe pipe = new NSPipe();

            task.StandardOutput = pipe;
            task.Launch();

            string[] args2 = new string[] { "/IOPlatformUUID/ { split($0, line, \"\\\"\"); printf(\"%s\\n\", line[4]); }" };

            NSTask task2 = new NSTask();

            task2.LaunchPath = @"/usr/bin/awk";
            task2.Arguments  = args2;

            NSPipe pipe2 = new NSPipe();

            task2.StandardInput  = pipe;
            task2.StandardOutput = pipe2;
            NSFileHandle fileHandle2 = pipe2.ReadHandle;

            task2.Launch();

            NSData   data = fileHandle2.ReadDataToEndOfFile();
            NSString uuid = NSString.FromData(data, NSStringEncoding.UTF8);

            return(uuid.ToString().Replace("\n", ""));
        }
        public string ReadFile(string path, string userIdentity)
        {
            string result = String.Empty;

            if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
            {
                if (FileExists(path))
                {
                    NSFileHandle fileHandle = null;
                    NSError      error      = null;
                    Exception    exception  = null;

                    try
                    {
                        UIApplication.SharedApplication.InvokeOnMainThread(() =>
                        {
                            _fileProtectionManagerService.DecryptFile(path);
                            fileHandle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out error);
                            if (fileHandle != null)
                            {
                                var nsStringResult = NSString.FromData(fileHandle.ReadDataToEndOfFile(), NSStringEncoding.UTF8);
                                if (nsStringResult != null)
                                {
                                    result = nsStringResult.ToString();
                                }
                            }
                            _fileProtectionManagerService.EncryptFile(path, userIdentity);
                        });
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                    finally
                    {
                        if (fileHandle != null)
                        {
                            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;
                    }
                }
            }

            return(result);
        }
        private Stream PlatformGetInputStream(string path)
        {
            if (FileExists(path))
            {
                NSFileHandle handle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error);

                if (error != null)
                {
                    throw new NSErrorException(error);
                }
                else
                {
                    NSData result = handle.ReadDataToEndOfFile();
                    handle.CloseFile();
                    return result.AsStream();
                }
            }

            throw new FileNotFoundException();
        }
        private string PlatformReadFile(string path)
        {
            if (FileExists(path))
            {
                NSFileHandle handle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error);

                if (error != null)
                {
                    throw new NSErrorException(error);
                }
                else
                {
                    NSString result = NSString.FromData(handle.ReadDataToEndOfFile(), NSStringEncoding.UTF8);
                    handle.CloseFile();
                    return result?.ToString() ?? "";
                }
            }

            throw new FileNotFoundException();
        }
Beispiel #5
0
        public void ReadFile()
        {
            try
            {
                // Gets the direct path to the file
                NSString folderPath = dataPath.AppendPathComponent(new NSString("testfile.txt"));

                // Handle the data and read it from the specific path
                NSFileHandle nsfh = NSFileHandle.OpenRead(folderPath);

                // Gets the data from the file
                NSData data = nsfh.ReadDataToEndOfFile();

                string text = data.ToString();
            }
            catch (Exception ex)
            {
                // Failed read data from a file
            }
        }
Beispiel #6
0
        public override void ShellSyncCore(string path, string[] arguments, string autoWriteStdin, out string stdout, out string stderr, out int exitCode)
        {
            if (autoWriteStdin != "")
            {
                throw new Exception("ShellSyncCore::AutoWriteStdin not supported in macOS");                 // Never need yet, used only in Linux
            }
            try
            {
                var pipeOut = new NSPipe();
                var pipeErr = new NSPipe();

                var t = new NSTask();

                t.LaunchPath     = path;
                t.Arguments      = arguments;
                t.StandardOutput = pipeOut;
                t.StandardError  = pipeErr;

                t.Launch();
                t.WaitUntilExit();
                //t.Release();
                t.Dispose();

                NSFileHandle fileOut = pipeOut.ReadHandle;
                stdout = fileOut.ReadDataToEndOfFile().ToString();
                fileOut.CloseFile();

                NSFileHandle fileErr = pipeErr.ReadHandle;
                stderr = fileErr.ReadDataToEndOfFile().ToString();
                fileErr.CloseFile();

                exitCode = t.TerminationStatus;
            }
            catch (Exception ex)
            {
                stdout   = "";
                stderr   = "Error: " + ex.Message;
                exitCode = -1;
            }
        }
        public Stream GetInputStream(string path, string userIdentity)
        {
            NSFileHandle fileHandle = null;
            NSError      error      = null;
            NSData       result     = null;
            Exception    exception  = null;

            try
            {
                UIApplication.SharedApplication.InvokeOnMainThread(() =>
                {
                    _fileProtectionManagerService.DecryptFile(path);
                    fileHandle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out error);
                    result     = fileHandle.ReadDataToEndOfFile();
                    _fileProtectionManagerService.EncryptFile(path, userIdentity);
                });
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (fileHandle != null)
                {
                    fileHandle.CloseFile();
                }
            }

            if (error != null)
            {
                throw new NSErrorException(error);
            }
            else if (exception != null)
            {
                throw exception;
            }

            return(result?.AsStream() ?? null);
        }
Beispiel #8
0
        public override void ShellSync(string path, string[] arguments, out string stdout, out string stderr, out int exitCode)
        {
            try
            {
                var pipeOut = new NSPipe();
                var pipeErr = new NSPipe();

                var t = new NSTask();

                t.LaunchPath     = path;
                t.Arguments      = arguments;
                t.StandardOutput = pipeOut;
                t.StandardError  = pipeErr;

                t.Launch();
                t.WaitUntilExit();
                //t.Release();
                t.Dispose();

                NSFileHandle fileOut = pipeOut.ReadHandle;
                stdout = fileOut.ReadDataToEndOfFile().ToString();
                fileOut.CloseFile();

                NSFileHandle fileErr = pipeErr.ReadHandle;
                stderr = fileErr.ReadDataToEndOfFile().ToString();
                fileErr.CloseFile();

                exitCode = t.TerminationStatus;
            }
            catch (Exception ex)
            {
                stdout   = "";
                stderr   = "Error: " + ex.Message;
                exitCode = -1;
            }
        }