System.IO.File.Open(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare)

Here are the examples of the csharp api System.IO.File.Open(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

947 Examples 7

19 View Source File : Program.cs
License : MIT License
Project Creator : 13xforever

static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("  gvas-converter path_to_save_file|path_to_json");
                return;
            }

            var ext = Path.GetExtension(args[0]).ToLower();
            if (ext == ".json")
            {
                Console.WriteLine("Not implemented atm");
            }
            else
            {
                Console.WriteLine("Parsing UE4 save file structure...");
                Gvas save;
                using (var stream = File.Open(args[0], FileMode.Open, FileAccess.Read, FileShare.Read))
                    save = UESerializer.Read(stream);

                Console.WriteLine("Converting to json...");
                var json = JsonConvert.SerializeObject(save, new JsonSerializerSettings{Formatting = Formatting.Indented});

                Console.WriteLine("Saving json...");
                using (var stream = File.Open(args[0] + ".json", FileMode.Create, FileAccess.Write, FileShare.Read))
                using (var writer = new StreamWriter(stream, new UTF8Encoding(false)))
                    writer.Write(json);
            }
            Console.WriteLine("Done.");
            Console.ReadKey(true);
        }

19 View Source File : Dumper.cs
License : MIT License
Project Creator : 13xforever

public void DetectDisc(string inDir = "", Func<Dumper, string> outputDirFormatter = null)
        {
            outputDirFormatter ??= d => $"[{d.ProductCode}] {d.replacedle}";
            string discSfbPath = null;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var drives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.CDRom && d.IsReady);
                if (string.IsNullOrEmpty(inDir))
                {
                    foreach (var drive in drives)
                    {
                        discSfbPath = Path.Combine(drive.Name, "PS3_DISC.SFB");
                        if (!File.Exists(discSfbPath))
                            continue;

                        input = drive.Name;
                        Drive = drive.Name[0];
                        break;
                    }
                }
                else
                {
                    discSfbPath = Path.Combine(inDir, "PS3_DISC.SFB");
                    if (File.Exists(discSfbPath))
                    {
                        input = Path.GetPathRoot(discSfbPath);
                        Drive = discSfbPath[0];
                    }
                }
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                if (string.IsNullOrEmpty(inDir))
                    inDir = "/media";
                discSfbPath = IOEx.GetFilepaths(inDir, "PS3_DISC.SFB", 2).FirstOrDefault();
                if (!string.IsNullOrEmpty(discSfbPath))
                    input = Path.GetDirectoryName(discSfbPath);
            }
            else
                throw new NotImplementedException("Current OS is not supported");

            if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(discSfbPath))
                throw new DriveNotFoundException("No valid PS3 disc was detected. Disc must be detected and mounted.");

            Log.Info("Selected disc: " + input);
            discSfbData = File.ReadAllBytes(discSfbPath);
            var replacedleId = CheckDiscSfb(discSfbData);
            var paramSfoPath = Path.Combine(input, "PS3_GAME", "PARAM.SFO");
            if (!File.Exists(paramSfoPath))
                throw new InvalidOperationException($"Specified folder is not a valid PS3 disc root (param.sfo is missing): {input}");

            using (var stream = File.Open(paramSfoPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                ParamSfo = ParamSfo.ReadFrom(stream);
            CheckParamSfo(ParamSfo);
            if (replacedleId != ProductCode)
                Log.Warn($"Product codes in ps3_disc.sfb ({replacedleId}) and in param.sfo ({ProductCode}) do not match");

            // todo: maybe use discutils instead to read TOC as one block
            var files = IOEx.GetFilepaths(input, "*", SearchOption.AllDirectories);
            DiscFilenames = new List<string>();
            var totalFilesize = 0L;
            var rootLength = input.Length;
            foreach (var f in files)
            {
                try { totalFilesize += new FileInfo(f).Length; } catch { }
                DiscFilenames.Add(f.Substring(rootLength));
            }
            TotalFileSize = totalFilesize;
            TotalFileCount = DiscFilenames.Count;

            OutputDir = new string(outputDirFormatter(this).ToCharArray().Where(c => !InvalidChars.Contains(c)).ToArray());
            Log.Debug($"Output: {OutputDir}");
        }

19 View Source File : Dumper.cs
License : MIT License
Project Creator : 13xforever

public async Task FindDiscKeyAsync(string discKeyCachePath)
        {
            // reload disc keys
            try
            {
                foreach (var keyProvider in DiscKeyProviders)
                {
                    Log.Trace($"Getting keys from {keyProvider.GetType().Name}...");
                    var newKeys = await keyProvider.EnumerateAsync(discKeyCachePath, ProductCode, Cts.Token).ConfigureAwait(false);
                    Log.Trace($"Got {newKeys.Count} keys");
                    lock (AllKnownDiscKeys)
                    {
                        foreach (var keyInfo in newKeys)
                        {
                            try
                            {
                                if (!AllKnownDiscKeys.TryGetValue(keyInfo.DecryptedKeyId, out var duplicates))
                                    AllKnownDiscKeys[keyInfo.DecryptedKeyId] = duplicates = new HashSet<DiscKeyInfo>();
                                duplicates.Add(keyInfo);
                            }
                            catch (Exception e)
                            {
                                Log.Error(e);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to load disc keys");
            }
            // check if user provided something new since the last attempt
            var untestedKeys = new HashSet<string>();
            lock (AllKnownDiscKeys)
                untestedKeys.UnionWith(AllKnownDiscKeys.Keys);
            untestedKeys.ExceptWith(TestedDiscKeys);
            if (untestedKeys.Count == 0)
                throw new KeyNotFoundException("No valid disc decryption key was found");

            // select physical device
            string physicalDevice = null;
            List<string> physicalDrives = new List<string>();
            Log.Trace("Trying to enumerate physical drives...");
            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    physicalDrives = EnumeratePhysicalDrivesWindows();
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    physicalDrives = EnumeratePhysicalDrivesLinux();
                else
                    throw new NotImplementedException("Current OS is not supported");
            }
            catch (Exception e)
            {
                Log.Error(e);
                throw;
            }
            Log.Debug($"Found {physicalDrives.Count} physical drives");

            if (physicalDrives.Count == 0)
                throw new InvalidOperationException("No optical drives were found");

            foreach (var drive in physicalDrives)
            {
                try
                {
                    Log.Trace($"Checking physical drive {drive}...");
                    using var discStream = File.Open(drive, FileMode.Open, FileAccess.Read, FileShare.Read);
                    var tmpDiscReader = new CDReader(discStream, true, true);
                    if (tmpDiscReader.FileExists("PS3_DISC.SFB"))
                    {
                        Log.Trace("Found PS3_DISC.SFB, getting sector data...");
                        var discSfbInfo = tmpDiscReader.GetFileInfo("PS3_DISC.SFB");
                        if (discSfbInfo.Length == discSfbData.Length)
                        {
                            var buf = new byte[discSfbData.Length];
                            var sector = tmpDiscReader.PathToClusters(discSfbInfo.FullName).First().Offset;
                            Log.Trace($"PS3_DISC.SFB sector number is {sector}, reading content...");
                            discStream.Seek(sector * tmpDiscReader.ClusterSize, SeekOrigin.Begin);
                            discStream.ReadExact(buf, 0, buf.Length);
                            if (buf.SequenceEqual(discSfbData))
                            {
                                physicalDevice = drive;
                                break;
                            }
                            Log.Trace("SFB content check failed, skipping the drive");
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Debug($"Skipping drive {drive}: {e.Message}");
                }
            }
            if (physicalDevice == null)
                throw new AccessViolationException("Couldn't get physical access to the drive");

            Log.Debug($"Selected physical drive {physicalDevice}");
            driveStream = File.Open(physicalDevice, FileMode.Open, FileAccess.Read, FileShare.Read);

            // find disc license file
            discReader = new CDReader(driveStream, true, true);
            FileRecord detectionRecord = null;
            byte[] expectedBytes = null;
            try
            {
                foreach (var path in Detectors.Keys)
                    if (discReader.FileExists(path))
                    {
                        var clusterRange = discReader.PathToClusters(path);
                        detectionRecord = new FileRecord(path, clusterRange.Min(r => r.Offset), discReader.GetFileLength(path));
                        expectedBytes = Detectors[path];
                        if (detectionRecord.Length == 0)
                            continue;
                        
                        Log.Debug($"Using {path} for disc key detection");
                        break;
                    }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            if (detectionRecord == null)
                throw new FileNotFoundException("Couldn't find a single disc key detection file, please report");

            if (Cts.IsCancellationRequested)
                return;

            SectorSize = discReader.ClusterSize;

            // select decryption key
            driveStream.Seek(detectionRecord.StartSector * discReader.ClusterSize, SeekOrigin.Begin);
            detectionSector = new byte[discReader.ClusterSize];
            detectionBytesExpected = expectedBytes;
            sectorIV = Decrypter.GetSectorIV(detectionRecord.StartSector);
            Log.Debug($"Initialized {nameof(sectorIV)} ({sectorIV?.Length * 8} bit) for sector {detectionRecord.StartSector}: {sectorIV?.ToHexString()}");
            driveStream.ReadExact(detectionSector, 0, detectionSector.Length);
            string discKey = null;
            try
            {
                discKey = untestedKeys.AsParallel().FirstOrDefault(k => !Cts.IsCancellationRequested && IsValidDiscKey(k));
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            if (discKey == null)
                throw new KeyNotFoundException("No valid disc decryption key was found");

            if (Cts.IsCancellationRequested)
                return;

            lock (AllKnownDiscKeys)
                AllKnownDiscKeys.TryGetValue(discKey, out allMatchingKeys);
            var discKeyInfo = allMatchingKeys?.First();
            DiscKeyFilename = Path.GetFileName(discKeyInfo?.FullPath);
            DiscKeyType = discKeyInfo?.KeyType ?? default;
        }

19 View Source File : Dumper.cs
License : MIT License
Project Creator : 13xforever

public async Task DumpAsync(string output)
        {
            // check and create output folder
            var dumpPath = output;
            while (!string.IsNullOrEmpty(dumpPath) && !Directory.Exists(dumpPath))
            {
                var parent = Path.GetDirectoryName(dumpPath);
                if (parent == null || parent == dumpPath)
                    dumpPath = null;
                else
                    dumpPath = parent;
            }
            if (filesystemStructure is null)
                (filesystemStructure, emptyDirStructure) = GetFilesystemStructure();
            var validators = GetValidationInfo();
            if (!string.IsNullOrEmpty(dumpPath))
            {
                var root = Path.GetPathRoot(Path.GetFullPath(output));
                var drive = DriveInfo.GetDrives().FirstOrDefault(d => d?.RootDirectory.FullName.StartsWith(root) ?? false);
                if (drive != null)
                {
                    var spaceAvailable = drive.AvailableFreeSpace;
                    TotalFileSize = filesystemStructure.Sum(f => f.Length);
                    var diff = TotalFileSize + 100 * 1024 - spaceAvailable;
                    if (diff > 0)
                        Log.Warn($"Target drive might require {diff.replacedtorageUnit()} of additional free space");
                }
            }

            foreach (var dir in emptyDirStructure)
                Log.Trace($"Empty dir: {dir}");
            foreach (var file in filesystemStructure)
                Log.Trace($"0x{file.StartSector:x8}: {file.Filename} ({file.Length})");
            var outputPathBase = Path.Combine(output, OutputDir);
            if (!Directory.Exists(outputPathBase))
                Directory.CreateDirectory(outputPathBase);

            TotalFileCount = filesystemStructure.Count;
            TotalSectors = discReader.TotalClusters;
            Log.Debug("Using decryption key: " + allMatchingKeys.First().DecryptedKeyId);
            var decryptionKey = allMatchingKeys.First().DecryptedKey;
            var sectorSize = (int)discReader.ClusterSize;
            var unprotectedRegions = driveStream.GetUnprotectedRegions();
            ValidationStatus = true;

            foreach (var dir in emptyDirStructure)
            {
                try
                {
                    if (Cts.IsCancellationRequested)
                        return;

                    var convertedName = Path.DirectorySeparatorChar == '\\' ? dir : dir.Replace('\\', Path.DirectorySeparatorChar);
                    var outputName = Path.Combine(outputPathBase, convertedName);
                    if (!Directory.Exists(outputName))
                    {
                        Log.Debug("Creating empty directory " + outputName);
                        Directory.CreateDirectory(outputName);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    BrokenFiles.Add((dir, "Unexpected error: " + ex.Message));
                }
            }
            
            foreach (var file in filesystemStructure)
            {
                try
                {
                    if (Cts.IsCancellationRequested)
                        return;

                    Log.Info($"Reading {file.Filename} ({file.Length.replacedtorageUnit()})");
                    CurrentFileNumber++;
                    var convertedFilename = Path.DirectorySeparatorChar == '\\' ? file.Filename : file.Filename.Replace('\\', Path.DirectorySeparatorChar);
                    var inputFilename = Path.Combine(input, convertedFilename);

                    if (!File.Exists(inputFilename))
                    {
                        Log.Error($"Missing {file.Filename}");
                        BrokenFiles.Add((file.Filename, "missing"));
                        continue;
                    }

                    var outputFilename = Path.Combine(outputPathBase, convertedFilename);
                    var fileDir = Path.GetDirectoryName(outputFilename);
                    if (!Directory.Exists(fileDir))
                    {
                        Log.Debug("Creating directory " + fileDir);
                        Directory.CreateDirectory(fileDir);
                    }

                    var error = false;
                    var expectedHashes = (
                        from v in validators
                        where v.Files.ContainsKey(file.Filename)
                        select v.Files[file.Filename].Hashes
                    ).ToList();
                    var lastHash = "";
                    var tries = 2;
                    do
                    {
                        try
                        {
                            tries--;
                            using var outputStream = File.Open(outputFilename, FileMode.Create, FileAccess.Write, FileShare.Read);
                            using var inputStream = File.Open(inputFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
                            using var decrypter = new Decrypter(inputStream, driveStream, decryptionKey, file.StartSector, sectorSize, unprotectedRegions);
                            Decrypter = decrypter;
                            await decrypter.CopyToAsync(outputStream, 8 * 1024 * 1024, Cts.Token).ConfigureAwait(false);
                            outputStream.Flush();
                            var resultHashes = decrypter.GetHashes();
                            var resultMd5 = resultHashes["MD5"];
                            if (decrypter.WasEncrypted && decrypter.WasUnprotected)
                                Log.Debug("Partially decrypted " + file.Filename);
                            else if (decrypter.WasEncrypted)
                                Log.Debug("Decrypted " + file.Filename);

                            if (!expectedHashes.Any())
                            {
                                if (ValidationStatus == true)
                                    ValidationStatus = null;
                            }
                            else if (!IsMatch(resultHashes, expectedHashes))
                            {
                                error = true;
                                var msg = "Unexpected hash: " + resultMd5;
                                if (resultMd5 == lastHash || decrypter.LastBlockCorrupted)
                                {
                                    Log.Error(msg);
                                    BrokenFiles.Add((file.Filename, "corrupted"));
                                    break;
                                }
                                Log.Warn(msg + ", retrying");
                            }

                            lastHash = resultMd5;
                        }
                        catch (Exception e)
                        {
                            Log.Error(e, e.Message);
                            error = true;
                        }
                    } while (error && tries > 0 && !Cts.IsCancellationRequested);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    BrokenFiles.Add((file.Filename, "Unexpected error: " + ex.Message));
                }
            }
            Log.Info("Completed");
        }

19 View Source File : PkgChecker.cs
License : MIT License
Project Creator : 13xforever

internal static async Task CheckAsync(List<FileInfo> pkgList, int fnameWidth, int sigWidth, int csumWidth, int allCsumsWidth, CancellationToken cancellationToken)
        {
            TotalFileSize = pkgList.Sum(i => i.Length);

            var buf = new byte[1024 * 1024]; // 1 MB
            foreach (var item in pkgList)
            {
                Write($"{item.Name.Trim(fnameWidth).PadRight(fnameWidth)} ");
                try
                {
                    CurrentPadding = sigWidth;
                    CurrentFileSize = item.Length;
                    if (item.Length < 0xC0 + 0x20) // header + csum at the end
                    {
                        Write("invalid pkg".PadLeft(allCsumsWidth) + Environment.NewLine, ConsoleColor.Red);
                        continue;
                    }

                    using var file = File.Open(item.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    var header = new byte[0xc0];
                    file.ReadExact(header);
                    byte[] sha1Sum = null;
                    using (var sha1 = SHA1.Create())
                        sha1Sum = sha1.ComputeHash(header, 0, 0x80);
                    if (!ValidateCmac(header))
                        Write("cmac".PadLeft(sigWidth) + " ", ConsoleColor.Red);
                    else if (!ValidateHash(header, sha1Sum))
                        Write("sha1".PadLeft(sigWidth) + " ", ConsoleColor.Yellow);
                    else if (!ValidateSigNew(header, sha1Sum))
                    {
                        if (!ValidateSigOld(header, sha1Sum))
                            Write("ecdsa".PadLeft(sigWidth) + " ", ConsoleColor.Red);
                        else
                            Write("ok (old)".PadLeft(sigWidth) + " ", ConsoleColor.Yellow);
                    }
                    else
                        Write("ok".PadLeft(sigWidth) + " ", ConsoleColor.Green);

                    CurrentPadding = csumWidth;
                    file.Seek(0, SeekOrigin.Begin);
                    byte[] hash;
                    using (var sha1 = SHA1.Create())
                    {
                        var dataLengthToHash = CurrentFileSize - 0x20;
                        int read;
                        do
                        {
                            read = await file.ReadAsync(buf, 0, (int)Math.Min(buf.Length, dataLengthToHash - CurrentFileProcessedBytes), cancellationToken).ConfigureAwait(false);
                            CurrentFileProcessedBytes += read;
                            sha1.TransformBlock(buf, 0, read, null, 0);
                        } while (read > 0 && CurrentFileProcessedBytes < dataLengthToHash && !cancellationToken.IsCancellationRequested);
                        sha1.TransformFinalBlock(buf, 0, 0);
                        hash = sha1.Hash;
                    }
                    if (cancellationToken.IsCancellationRequested)
                        return;

                    var expectedHash = new byte[0x14];
                    file.ReadExact(expectedHash);
                    CurrentFileProcessedBytes += 0x20;
                    if (!expectedHash.SequenceEqual(hash))
                        Write("fail".PadLeft(csumWidth) + Environment.NewLine, ConsoleColor.Red);
                    else
                        Write("ok".PadLeft(csumWidth) + Environment.NewLine, ConsoleColor.Green);
                }
                catch (Exception e)
                {
                    Write("Error" + Environment.NewLine + e.Message + Environment.NewLine, ConsoleColor.Red);
                }
                finally
                {
                    ProcessedBytes += CurrentFileSize;
                    CurrentFileProcessedBytes = 0;
                    CurrentPadding = 0;
                }
                if (cancellationToken.IsCancellationRequested)
                    return;
            }
        }

19 View Source File : DateAndSizeRollingFileAppenderTests.cs
License : MIT License
Project Creator : Abc-Arbitrage

private string GetLastLine()
        {
            var reader = new StreamReader(File.Open(_appender.CurrentFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
            string written = null;

            while (!reader.EndOfStream)
            {
                written = reader.ReadLine();
            }

            return written;
        }

19 View Source File : Shapefile.cs
License : Microsoft Public License
Project Creator : abfo

public void Open(string path)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Shapefile");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (path.Length <= 0)
            {
                throw new ArgumentException("path parameter is empty", "path");
            }

            _shapefileMainPath = Path.ChangeExtension(path, MainPathExtension);
            _shapefileIndexPath = Path.ChangeExtension(path, IndexPathExtension);
            _shapefileDbasePath = Path.ChangeExtension(path, DbasePathExtension);

            if (!File.Exists(_shapefileMainPath))
            {
                throw new FileNotFoundException("Shapefile main file not found", _shapefileMainPath);
            }
            if (!File.Exists(_shapefileIndexPath))
            {
                throw new FileNotFoundException("Shapefile index file not found", _shapefileIndexPath);
            }
            if (!File.Exists(_shapefileDbasePath))
            {
                throw new FileNotFoundException("Shapefile dBase file not found", _shapefileDbasePath);
            }

            _mainStream = File.Open(_shapefileMainPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            _indexStream = File.Open(_shapefileIndexPath, FileMode.Open, FileAccess.Read, FileShare.Read);

            if (_mainStream.Length < Header.HeaderLength)
            {
                throw new InvalidOperationException("Shapefile main file does not contain a valid header");
            }

            if (_indexStream.Length < Header.HeaderLength)
            {
                throw new InvalidOperationException("Shapefile index file does not contain a valid header");
            }

            // read in and parse the headers
            byte[] headerBytes = new byte[Header.HeaderLength];
            _mainStream.Read(headerBytes, 0, Header.HeaderLength);
            _mainHeader = new Header(headerBytes);
            _indexStream.Read(headerBytes, 0, Header.HeaderLength);
            _indexHeader = new Header(headerBytes);

            // set properties from the main header
            _type = _mainHeader.ShapeType;
            _boundingBox = new RectangleD(_mainHeader.XMin, _mainHeader.YMin, _mainHeader.XMax, _mainHeader.YMax);

            // index header length is in 16-bit words, including the header - number of 
            // shapes is the number of records (each 4 workds long) after subtracting the header bytes
            _count = (_indexHeader.FileLength - (Header.HeaderLength / 2)) / 4;

            // open the metadata database
            OpenDb();

            _opened = true;
        }

19 View Source File : FileCache.cs
License : Apache License 2.0
Project Creator : acarteas

private FileStream GetCleaningLock()
        {
            try
            {
                return File.Open(Path.Combine(CacheDir, SemapreplacedFile), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
            }
            catch (Exception)
            {
                return null;
            }
        }

19 View Source File : FileCacheManager.cs
License : Apache License 2.0
Project Creator : acarteas

protected FileStream GetStream(string path, FileMode mode, FileAccess access, FileShare share)
        {
            FileStream stream = null;
            TimeSpan interval = new TimeSpan(0, 0, 0, 0, 50);
            TimeSpan totalTime = new TimeSpan();
            while (stream == null)
            {
                try
                {
                    stream = File.Open(path, mode, access, share);
                }
                catch (IOException ex)
                {
                    Thread.Sleep(interval);
                    totalTime += interval;

                    //if we've waited too long, throw the original exception.
                    if (AccessTimeout.Ticks != 0)
                    {
                        if (totalTime > AccessTimeout)
                        {
                            throw ex;
                        }
                    }
                }
            }
            return stream;
        }

19 View Source File : JobServerQueue.cs
License : MIT License
Project Creator : actions

private async Task UploadFile(UploadFileInfo file)
        {
            bool uploadSucceed = false;
            try
            {
                if (String.Equals(file.Type, CoreAttachmentType.Log, StringComparison.OrdinalIgnoreCase))
                {
                    // Create the log
                    var taskLog = await _jobServer.CreateLogAsync(_scopeIdentifier, _hubName, _planId, new TaskLog(String.Format(@"logs\{0:D}", file.TimelineRecordId)), default(CancellationToken));

                    // Upload the contents
                    using (FileStream fs = File.Open(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var logUploaded = await _jobServer.AppendLogContentAsync(_scopeIdentifier, _hubName, _planId, taskLog.Id, fs, default(CancellationToken));
                    }

                    // Create a new record and only set the Log field
                    var attachmentUpdataRecord = new TimelineRecord() { Id = file.TimelineRecordId, Log = taskLog };
                    QueueTimelineRecordUpdate(file.TimelineId, attachmentUpdataRecord);
                }
                else
                {
                    // Create attachment
                    using (FileStream fs = File.Open(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var result = await _jobServer.CreateAttachmentAsync(_scopeIdentifier, _hubName, _planId, file.TimelineId, file.TimelineRecordId, file.Type, file.Name, fs, default(CancellationToken));
                    }
                }

                uploadSucceed = true;
            }
            finally
            {
                if (uploadSucceed && file.DeleteSource)
                {
                    try
                    {
                        File.Delete(file.Path);
                    }
                    catch (Exception ex)
                    {
                        Trace.Info("Catch exception during delete success uploaded file.");
                        Trace.Error(ex);
                    }
                }
            }
        }

19 View Source File : FileContainerServer.cs
License : MIT License
Project Creator : actions

private async Task<UploadResult> UploadAsync(RunnerActionPluginExecutionContext context, int uploaderId, CancellationToken token)
        {
            List<string> failedFiles = new List<string>();
            long uploadedSize = 0;
            string fileToUpload;
            Stopwatch uploadTimer = new Stopwatch();
            while (_fileUploadQueue.TryDequeue(out fileToUpload))
            {
                token.ThrowIfCancellationRequested();
                try
                {
                    using (FileStream fs = File.Open(fileToUpload, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        string itemPath = (_containerPath.TrimEnd('/') + "/" + fileToUpload.Remove(0, _sourceParentDirectory.Length + 1)).Replace('\\', '/');
                        bool failAndExit = false;
                        try
                        {
                            uploadTimer.Restart();
                            using (HttpResponseMessage response = await _fileContainerHttpClient.UploadFileAsync(_containerId, itemPath, fs, _projectId, cancellationToken: token, chunkSize: 4 * 1024 * 1024))
                            {
                                if (response == null || response.StatusCode != HttpStatusCode.Created)
                                {
                                    context.Output($"Unable to copy file to server StatusCode={response?.StatusCode}: {response?.ReasonPhrase}. Source file path: {fileToUpload}. Target server path: {itemPath}");

                                    if (response?.StatusCode == HttpStatusCode.Conflict)
                                    {
                                        // fail upload task but continue with any other files
                                        context.Error($"Error '{fileToUpload}' has already been uploaded.");
                                    }
                                    else if (_fileContainerHttpClient.IsFastFailResponse(response))
                                    {
                                        // Fast fail: we received an http status code where we should abandon our efforts
                                        context.Output($"Cannot continue uploading files, so draining upload queue of {_fileUploadQueue.Count} items.");
                                        DrainUploadQueue(context);
                                        failedFiles.Clear();
                                        failAndExit = true;
                                        throw new UploadFailedException($"Critical failure uploading '{fileToUpload}'");
                                    }
                                    else
                                    {
                                        context.Debug($"Adding '{fileToUpload}' to retry list.");
                                        failedFiles.Add(fileToUpload);
                                    }
                                    throw new UploadFailedException($"Http failure response '{response?.StatusCode}': '{response?.ReasonPhrase}' while uploading '{fileToUpload}'");
                                }

                                uploadTimer.Stop();
                                context.Debug($"File: '{fileToUpload}' took {uploadTimer.ElapsedMilliseconds} milliseconds to finish upload");
                                uploadedSize += fs.Length;
                                OutputLogForFile(context, fileToUpload, $"Detail upload trace for file: {itemPath}", context.Debug);
                            }
                        }
                        catch (OperationCanceledException) when (token.IsCancellationRequested)
                        {
                            context.Output($"File upload has been cancelled during upload file: '{fileToUpload}'.");
                            throw;
                        }
                        catch (Exception ex)
                        {
                            context.Output($"Fail to upload '{fileToUpload}' due to '{ex.Message}'.");
                            context.Output(ex.ToString());

                            OutputLogForFile(context, fileToUpload, $"Detail upload trace for file that fail to upload: {itemPath}", context.Output);

                            if (failAndExit)
                            {
                                context.Debug("Exiting upload.");
                                throw;
                            }
                        }
                    }

                    Interlocked.Increment(ref _uploadFilesProcessed);
                }
                catch (Exception ex)
                {
                    context.Output($"File error '{ex.Message}' when uploading file '{fileToUpload}'.");
                    throw ex;
                }
            }

            return new UploadResult(failedFiles, uploadedSize);
        }

19 View Source File : FilePackageStorageService.cs
License : MIT License
Project Creator : ai-traders

private Stream GetFileStream(PackageIdenreplacedy id, Func<string, string, string> pathFunc)
        {
            var versionString = id.Version.ToNormalizedString().ToLowerInvariant();
            var path = pathFunc(id.Id.ToLowerInvariant(), versionString);

            return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        }

19 View Source File : BundleContainer.cs
License : GNU Affero General Public License v3.0
Project Creator : aianlinb

public virtual byte[] AppendAndSave(Stream newData, string originalPath = null) {
            offset = 0;
            return AppendAndSave(newData, File.Open(originalPath ?? path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
        }

19 View Source File : MainWindow.xaml.cs
License : GNU Affero General Public License v3.0
Project Creator : aianlinb

private async void AllowGameOpen_Click(object sender, RoutedEventArgs e) {
            ggpkContainer.fileStream.Close();
            var fi = new FileInfo(FilePath);
            var t = fi.LastWriteTimeUtc;
            var l = fi.Length;
        loop:
			try {
                MessageBox.Show(this, "GGPK file is now closed, you can open the game!\nClose the game and click OK to reopen the GGPK file and return to VisualGGPK2", "Released File Handle", MessageBoxButton.OK, MessageBoxImage.Information);
                fi = new FileInfo(FilePath);
                if (fi.LastWriteTimeUtc != t || fi.Length != l) {
                    MessageBox.Show(this, "The Content.ggpk has been modified, Now it's going to be reloaded", "GGPK Changed", MessageBoxButton.OK, MessageBoxImage.Warning);

                    Tree.Items.Clear();
                    TextView.Text = "Loading . . .";
                    TextView.Visibility = Visibility.Visible;
                    FilterButton.IsEnabled = false;
                    AllowGameOpen.IsEnabled = false;

                    // Initial GGPK
                    await Task.Run(() => ggpkContainer = new GGPKContainer(FilePath, BundleMode, SteamMode));

                    var root = CreateNode(ggpkContainer.rootDirectory);
                    Tree.Items.Add(root); // Initial TreeView
                    root.IsExpanded = true;

                    FilterButton.IsEnabled = true;
                    if (!SteamMode)
                        AllowGameOpen.IsEnabled = true;

                    TextView.AppendText("\r\n\r\nDone!\r\n");
                } else {
                    ggpkContainer.fileStream = File.Open(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                    ggpkContainer.Reader = new(ggpkContainer.fileStream);
                    ggpkContainer.Writer = new(ggpkContainer.fileStream);
                }
            } catch (IOException) {
                MessageBox.Show(this, "Cannot access the file, make sure you have closed the game!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                goto loop;
            }
        }

19 View Source File : LocalStorage.cs
License : GNU General Public License v3.0
Project Creator : aiportal

private long WriteImageData(Snapshot sshot, Database db)
		{
			long position;
			try
			{
				var sessionDate = db.SelectSingle<DateTime>("SessionInfo", "CreateTime", new { SessionId = sshot.SessionId });
				string path = Path.Combine(DataPath, string.Format(@"{0:yyyy}\{0:MMdd}\{1}.rdt", sessionDate, sshot.SessionId));
				if (!Directory.Exists(Path.GetDirectoryName(path)))
					Directory.CreateDirectory(Path.GetDirectoryName(path));
				using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read))
				{
					position = fs.Position;
					if (sshot.ImageData != null && sshot.ImageData.Length > 0)
					{
						fs.Write(sshot.ImageData, 0, sshot.ImageData.Length);
					}
					Debug.replacedert(sshot.EventsData != null && sshot.EventsData.Length > 0 && sshot.EventsData.Length % 16 == 0);
					fs.Write(sshot.EventsData, 0, sshot.EventsData.Length);
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
			return position;
		}

19 View Source File : StorageEngine.cs
License : GNU General Public License v3.0
Project Creator : aiportal

private static SnapImage LoadSnapshot(string snapshotId, Database db)
		{
			SnapImage snap = null;
			try
			{
				var sshot = db.SelectObject<SnapImage>("SnapshotData", new { SnapshotId = snapshotId },
					"SessionId", "SnapshotId", "BackgroundId", "ScreenWidth", "ScreenHeight", "MouseState", "IsGrayScale", "ImagePos", "ImageLen");
				if (sshot != null)
				{
					var sessionDate = db.SelectSingle<DateTime>("SessionInfo", "CreateTime", new { SessionId = sshot.SessionId });
					string fpath = Path.Combine(LocalStorage.DataPath, string.Format(@"{0:yyyy}\{0:MMdd}\{1}.rdt", sessionDate, sshot.SessionId));

					using (FileStream fs = File.Open(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
					using (BinaryReader br = new BinaryReader(fs))
					{
						br.BaseStream.Seek(sshot.ImagePos, SeekOrigin.Begin);
						byte[] bsData = br.ReadBytes(sshot.ImageLen);

						snap = sshot;
						snap.ImageData = bsData;
					}
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
			return snap;
		}

19 View Source File : CompressStorage.cs
License : GNU General Public License v3.0
Project Creator : aiportal

private void WriteImageData(Snapshot sshot, out long imgPos, out int imgLen)
		{
			try
			{
				switch ((ImageStorageType)Global.Config.ImageStorage)
				{
					case ImageStorageType.TextOnly:
						imgPos = imgLen = 0;
						break;
					case ImageStorageType.GrayScale:
						break;
					case ImageStorageType.RawImage:
						break;
				}
				
				string path = Path.Combine(DataPath, sshot.SessionId + ".rdt");
				using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read))
				{
					if (sshot.ImageData != null && sshot.ImageData.Length > 0)
					{
						imgPos = fs.Position;
						fs.Write(sshot.ImageData, 0, sshot.ImageData.Length);
						fs.Write(sshot.EventsData, 0, sshot.EventsData.Length);
						imgLen = sshot.ImageData.Length;
					}
					else
					{
						imgPos = fs.Position;
						fs.Write(sshot.EventsData, 0, sshot.EventsData.Length);
						imgLen = 0;
					}
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
		}

19 View Source File : ImageStorage.cs
License : GNU General Public License v3.0
Project Creator : aiportal

public long WriteImageData(string sessionId, byte[] imgData, byte[] addData)
		{
			Debug.replacedert(sessionId != null && imgData != null && imgData.Length > 0);
			long pos;
			try
			{
				string path = Path.Combine(DataPath, sessionId + ".rdm");
				using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read))
				{
					pos = fs.Position;
					fs.Write(imgData, 0, imgData.Length);
					fs.Write(addData, 0, addData.Length);
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
			return pos;
		}

19 View Source File : LocalStorage.cs
License : GNU General Public License v3.0
Project Creator : aiportal

[Obsolete]
		public byte[] ReadImage(string snapshotId)
		{
			byte[] bsImage = null;
			try
			{
				var sshot = Database.Invoke(db => db.SelectRow("Snapshots", new { SnapshotId = snapshotId },
					"SessionId", "WindowRect", "MouseState", "ImagePos", "ImageLength"));
				if (sshot != null)
				{
					Guid sessionId = new Guid(sshot["SessionId"].ToString());
					string path = Path.Combine(DataPath, sessionId.ToString("n") + ".rdt");
					if (File.Exists(path))
					{
						using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
						using (BinaryReader br = new BinaryReader(fs))
						{
							br.BaseStream.Seek(Convert.ToInt64(sshot["ImagePos"]), SeekOrigin.Begin);
							bsImage = br.ReadBytes(Convert.ToInt32(sshot["ImageLength"]));
						}
					}
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
			return bsImage;
		}

19 View Source File : _CompressEngine.cs
License : GNU General Public License v3.0
Project Creator : aiportal

private Image LoadImage(SnapImage sshot)
		{
			byte[] bsImage = null;
			string path = Path.Combine(DataPath, sshot.SessionId + ".rdt");
			if (File.Exists(path))
			{
				using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
				using (BinaryReader br = new BinaryReader(fs))
				{
					br.BaseStream.Seek(sshot.ImagePos, SeekOrigin.Begin);
					bsImage = br.ReadBytes(sshot.ImageLength);
				}
			}

			Image img = null;
			if (bsImage != null && bsImage.Length > 0)
			{
				using (MemoryStream ms = new MemoryStream(bsImage))
					img = Image.FromStream(ms);
			}
			return img;
		}

19 View Source File : StorageEngine.cs
License : GNU General Public License v3.0
Project Creator : aiportal

public static byte[] ReadImage(string snapshotId)
		{
			byte[] bsImage = null;
			try
			{
				SnapImage sshot;
				string fpath = null;
				using (Database db = new Database())
				{
					sshot = db.SelectObject<SnapImage>("SnapshotData", new { SnapshotId = snapshotId },
						"SessionId", "BackgroundId", "WindowRect", "MouseState", "IsGrayScale", "ImagePos", "ImageLen");
					if (sshot != null)
					{
						var sessionDate = db.SelectSingle<DateTime>("SessionInfo", "CreateTime", new { SessionId = sshot.SessionId });
						fpath = Path.Combine(LocalStorage.DataPath, string.Format(@"{0:yyyy}\{0:MMdd}\{1}.rdt", sessionDate, sshot.SessionId));
						TraceLogger.Instance.WriteLineInfo("Reading session file: " + fpath);
					}
				}
				if (sshot != null && File.Exists(fpath))
				{
					using (FileStream fs = File.Open(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
					using (BinaryReader br = new BinaryReader(fs))
					{
						br.BaseStream.Seek(sshot.ImagePos, SeekOrigin.Begin);
						bsImage = br.ReadBytes(sshot.ImageLen);
					}
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
			return bsImage;
		}

19 View Source File : ImageStorage.cs
License : GNU General Public License v3.0
Project Creator : aiportal

public byte[] ReadImageData(string snapshotId)
		{
			byte[] bsImage = null;
			try
			{
				var sshot = Database.Invoke(db => db.SelectRow("Snapshots", new { SnapshotId = snapshotId },
					"SessionId", "WindowRect", "MouseState", "ImagePos", "ImageLength"));
				if (sshot != null)
				{
					Guid sessionId = new Guid(sshot["SessionId"].ToString());
					string path = Path.Combine(DataPath, sessionId.ToString("n") + ".rdm");
					if (File.Exists(path))
					{
						using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
						using (BinaryReader br = new BinaryReader(fs))
						{
							br.BaseStream.Seek(Convert.ToInt64(sshot["ImagePos"]), SeekOrigin.Begin);
							bsImage = br.ReadBytes(Convert.ToInt32(sshot["ImageLength"]));
						}
					}
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
			return bsImage;
		}

19 View Source File : LocalStorage.cs
License : GNU General Public License v3.0
Project Creator : aiportal

public void WriteSnapshot(Snapshot sshot)
		{
			try
			{
				Debug.replacedert(sshot.EventsData.Length > 0 && sshot.EventsData.Length % 16 == 0);
				if (!string.IsNullOrEmpty(sshot.WindowUrl) && sshot.Url == null)
					TraceLogger.Instance.WriteLineInfo("Url can not be parsed: " + sshot.WindowUrl);

				long imgPos, imgLen;
				string path = Path.Combine(DataPath, sshot.SessionId + ".rdt");
				using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read))
				{
					if (sshot.ImageData != null && sshot.ImageData.Length > 0)
					{
						imgPos = fs.Position;
						fs.Write(sshot.ImageData, 0, sshot.ImageData.Length);
						fs.Write(sshot.EventsData, 0, sshot.EventsData.Length);
						imgLen = sshot.ImageData.Length;
					}
					else
					{
						imgPos = fs.Position;
						fs.Write(sshot.EventsData, 0, sshot.EventsData.Length);
						imgLen = 0;
					}
				}

				using (Database db = new Database())
				{
					var ss = new
					{
						SessionId = sshot.SessionId,
						SnapshotId = sshot.SnapshotId,
						SnapTime = sshot.SnapTime,
						ProcessId = sshot.ProcessId,
						ProcessName = sshot.ProcessName,
						WindowHandle = sshot.WindowHandle,
						WindowRect = DataConverter.Serialize(sshot.WindowRect),

						Windowreplacedle = sshot.Windowreplacedle,
						WindowUrl = sshot.Url == null ? sshot.WindowUrl : sshot.Url.AbsoluteUri,
						UrlHost = sshot.Url == null ? null : sshot.Url.Host,

						//MouseState = DataConverter.Serialize(sshot.Mouse),
						ImagePos = imgPos,
						ImageLength = imgLen,
						IsGrayScale = sshot.IsGrayScale,

						ControlText = sshot.ControlText,
						InputText = sshot.InputText,
						EventsCount = sshot.EventsCount,
					};

					db.InsertDistinct("Snapshots", ss, new { SnapshotId = sshot.SnapshotId });
					if (!string.IsNullOrEmpty(sshot.ProcessName))
						db.InsertDistinct("ApplicationInfo", new { ProcessName = ss.ProcessName });
					if (!string.IsNullOrEmpty(ss.UrlHost))
					{
						string name = ss.UrlHost.StartsWith("www.") ? ss.UrlHost.Substring(4) : ss.UrlHost;
						name = name.EndsWith(".com") ? name.Substring(0, name.Length - 4) : name;
						db.InsertDistinct("HostInfo", new { HostUrl = ss.UrlHost, HostName = name }, new { HostUrl = ss.UrlHost });
					}
				}
			}
			catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
		}

19 View Source File : AesBase.cs
License : MIT License
Project Creator : alecgn

internal AesEncryptionResult EncryptWithFileStream(string sourceFilePath, string encryptedFilePath, byte[] key = null, byte[] IV = null, CipherMode cipherMode = CipherMode.CBC,
            PaddingMode paddingMode = PaddingMode.PKCS7, bool deleteSourceFile = false, int kBbufferSize = 4)
        {
            if (!File.Exists(sourceFilePath))
            {
                return new AesEncryptionResult()
                {
                    Success = false,
                    //Message = $"{MessageStrings.Common_FileNotFound} \"{sourceFilePath}\"."
                    Message = $"{MessageStrings.Common_FileNotFound} \"{sourceFilePath}\"."
                };
            }

            if (string.IsNullOrWhiteSpace(encryptedFilePath))
            {
                return new AesEncryptionResult()
                {
                    Success = false,
                    Message = MessageStrings.Encryption_EncryptedFilePathError
                };
            }

            var destinationDirectory = Path.GetDirectoryName(encryptedFilePath);

            if (!Directory.Exists(destinationDirectory))
            {
                return new AesEncryptionResult()
                {
                    Success = false,
                    Message = $"{MessageStrings.Encryption_DestinationDirectoryNotFound} \"{destinationDirectory}\"."
                };
            }

            _key = key ?? _key;
            _IV = IV ?? _IV;

            var pathsEqual = encryptedFilePath.Equals(sourceFilePath, StringComparison.InvariantCultureIgnoreCase);

            try
            {
                using (var aesManaged = new AesManaged())
                {
                    if (_key == null)
                    {
                        aesManaged.GenerateKey();
                        _key = aesManaged.Key;
                    }
                    else
                    {
                        if (aesManaged.ValidKeySize((_key.Length * 8)))
                        {
                            aesManaged.Key = _key;
                        }
                        else
                        {
                            return new AesEncryptionResult()
                            {
                                Success = false,
                                Message = $"{MessageStrings.Common_InvalidKeySizeError} ({(_key.Length * 8)})."
                            };
                        }
                    }

                    if (_IV == null || _IV.Length == 0)
                    {
                        aesManaged.GenerateIV();
                        _IV = aesManaged.IV;
                    }
                    else
                    {
                        aesManaged.IV = _IV;
                    }

                    aesManaged.Mode = cipherMode;
                    aesManaged.Padding = paddingMode;

                    using (var encryptor = aesManaged.CreateEncryptor(_key, _IV))
                    {
                        using (var sourceFs = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            using (var encryptedFs = File.Open((pathsEqual ? encryptedFilePath + "_tmpcrypt" : encryptedFilePath), FileMode.Create, FileAccess.Write, FileShare.None))
                            {
                                using (var cs = new CryptoStream(encryptedFs, encryptor, CryptoStreamMode.Write))
                                {
                                    //plain.CopyTo(cs);

                                    var buffer = new byte[kBbufferSize * 1024];
                                    int read;
                                    var percentageDone = 0;

                                    while ((read = sourceFs.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        cs.Write(buffer, 0, read);

                                        var tmpPercentageDone = (int)(sourceFs.Position * 100 / sourceFs.Length);

                                        if (tmpPercentageDone != percentageDone)
                                        {
                                            percentageDone = tmpPercentageDone;

                                            RaiseOnEncryptionProgress(percentageDone, (percentageDone != 100 ? $"Encrypting ({percentageDone}%)..." : $"Encrypted ({percentageDone}%)."));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (pathsEqual)
                {
                    CommonMethods.ClearFileAttributes(sourceFilePath); // set "Normal" FileAttributes to avoid erros while trying to delete the file below
                    File.Delete(sourceFilePath);
                    File.Move(encryptedFilePath + "_tmpcrypt", encryptedFilePath);
                }

                if (deleteSourceFile && !pathsEqual)
                {
                    CommonMethods.ClearFileAttributes(sourceFilePath); // set "Normal" FileAttributes to avoid erros while trying to delete the file below
                    File.Delete(sourceFilePath);
                }

                //var message = $"File \"{sourceFilePath}\" successfully encrypted to \"{encryptedFilePath}\".";
                var message = string.Format(MessageStrings.Encryption_FileEncryptSuccess, sourceFilePath, encryptedFilePath);
                message += (deleteSourceFile && !pathsEqual ? $"\n{string.Format(MessageStrings.Encryption_FileDeleted, sourceFilePath)}" : "");

                return new AesEncryptionResult()
                {
                    Success = true,
                    Message = message,
                    Key = _key,
                    IV = _IV,
                    AesCipherMode = (AesCipherMode)cipherMode,
                    PaddingMode = paddingMode
                };
            }
            catch (Exception ex)
            {
                return new AesEncryptionResult()
                {
                    Success = false,
                    Message = $"{MessageStrings.Encryption_ExceptionError}\n{ex.ToString()}"
                };
            }
        }

19 View Source File : AesBase.cs
License : MIT License
Project Creator : alecgn

internal AesDecryptionResult DecryptWithFileStream(string encryptedFilePath, string decryptedFilePath, byte[] key, byte[] IV, CipherMode cipherMode = CipherMode.CBC,
            PaddingMode paddingMode = PaddingMode.PKCS7, bool deleteEncryptedFile = false, int kBbufferSize = 4, long startPosition = 0, long endPosition = 0)
        {
            if (!File.Exists(encryptedFilePath))
            {
                return new AesDecryptionResult()
                {
                    Success = false,
                    Message = $"{MessageStrings.Decryption_EncryptedFileNotFound} \"{encryptedFilePath}\"."
                };
            }

            if (string.IsNullOrWhiteSpace(decryptedFilePath))
            {
                return new AesDecryptionResult()
                {
                    Success = false,
                    Message = MessageStrings.Decryption_DecryptedFilePathError
                };
            }

            var destinationDirectory = Path.GetDirectoryName(decryptedFilePath);

            if (!Directory.Exists(destinationDirectory))
            {
                return new AesDecryptionResult()
                {
                    Success = false,
                    Message = $"{MessageStrings.Encryption_DestinationDirectoryNotFound} \"{destinationDirectory}\"."
                };
            }

            _key = key ?? _key;
            _IV = IV ?? _IV;

            if (_key == null)
            {
                return new AesDecryptionResult()
                {
                    Success = false,
                    Message = MessageStrings.Decryption_NullKeyError
                };
            }

            if (_IV == null)
            {
                return new AesDecryptionResult()
                {
                    Success = false,
                    Message = MessageStrings.Decryption_NullIVError
                };
            }

            if (endPosition < startPosition)
            {
                return new AesDecryptionResult()
                {
                    Success = false,
                    Message = string.Format(MessageStrings.Decryption_EndPositionLessThanStartError, endPosition, startPosition)
                };
            }

            var pathsEqual = decryptedFilePath.Equals(encryptedFilePath, StringComparison.InvariantCultureIgnoreCase);

            try
            {
                using (var aesManaged = new AesManaged())
                {
                    aesManaged.Key = _key;
                    aesManaged.IV = _IV;
                    aesManaged.Mode = cipherMode;
                    aesManaged.Padding = paddingMode;

                    using (var decryptedFs = File.Open((pathsEqual ? decryptedFilePath + "_tmpdecrypt" : decryptedFilePath), FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        using (var encryptedFs = File.Open(encryptedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            encryptedFs.Position = startPosition;

                            using (var decryptor = aesManaged.CreateDecryptor(_key, _IV))
                            {
                                using (var cs = new CryptoStream(decryptedFs, decryptor, CryptoStreamMode.Write))
                                {
                                    //encrypted.CopyTo(cs);

                                    var buffer = new byte[kBbufferSize * 1024];
                                    var totalBytesToRead = ((endPosition == 0 ? encryptedFs.Length : endPosition) - startPosition);
                                    var totalBytesNotRead = totalBytesToRead;
                                    long totalBytesRead = 0;
                                    var percentageDone = 0;

                                    while (totalBytesNotRead > 0)
                                    {
                                        var bytesRead = encryptedFs.Read(buffer, 0, (int)Math.Min(buffer.Length, totalBytesNotRead));

                                        if (bytesRead > 0)
                                        {
                                            cs.Write(buffer, 0, bytesRead);

                                            totalBytesRead += bytesRead;
                                            totalBytesNotRead -= bytesRead;
                                            var tmpPercentageDone = (int)(totalBytesRead * 100 / totalBytesToRead);

                                            if (tmpPercentageDone != percentageDone)
                                            {
                                                percentageDone = tmpPercentageDone;

                                                RaiseOnDecryptionProgress(percentageDone, (percentageDone != 100 ? $"Decrypting ({percentageDone}%)..." : $"Decrypted ({percentageDone}%)."));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (pathsEqual)
                {
                    CommonMethods.ClearFileAttributes(encryptedFilePath); // set "Normal" FileAttributes to avoid erros while trying to delete the file below
                    File.Delete(encryptedFilePath);
                    File.Move(decryptedFilePath + "_tmpdecrypt", decryptedFilePath);
                }

                if (deleteEncryptedFile && !pathsEqual)
                {
                    CommonMethods.ClearFileAttributes(encryptedFilePath); // set "Normal" FileAttributes to avoid erros while trying to delete the file below
                    File.Delete(encryptedFilePath);
                }

                var message = string.Format(MessageStrings.Decryption_FileDecryptSuccess, encryptedFilePath, decryptedFilePath);
                message += (deleteEncryptedFile && !pathsEqual ? $"\n{string.Format(MessageStrings.Encryption_FileDeleted, encryptedFilePath)}" : "");

                return new AesDecryptionResult()
                {
                    Success = true,
                    Message = message,
                    Key = _key,
                    IV = _IV,
                    AesCipherMode = (AesCipherMode)cipherMode,
                    PaddingMode = paddingMode
                };
            }
            catch (Exception ex)
            {
                return new AesDecryptionResult()
                {
                    Success = false,
                    Message = $"{MessageStrings.Decryption_ExceptionError}\n{ex.ToString()}"
                };
            }
        }

19 View Source File : CommonMethods.cs
License : MIT License
Project Creator : alecgn

public static void AppendDataBytesToFile(string filePath, byte[] dataBytes)
        {
            using (var fs = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.None))
            {
                fs.Write(dataBytes, 0, dataBytes.Length);
            }
        }

19 View Source File : CommonMethods.cs
License : MIT License
Project Creator : alecgn

public static byte[] GetBytesFromFile(string filePath, int dataLength, long offset = 0)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException($"{MessageStrings.Common_FileNotFound} {filePath}.", filePath);
            }

            if (dataLength < 1)
            {
                throw new ArgumentException($"{MessageStrings.Common_InvalidDataLengthError} ({dataLength}).", nameof(dataLength));
            }

            var dataBytes = new byte[dataLength];

            using (var fStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                fStream.Seek(offset, SeekOrigin.Begin);
                fStream.Read(dataBytes, 0, dataLength);
                fStream.Close();
            }

            return dataBytes;
        }

19 View Source File : App.Impl.cs
License : MIT License
Project Creator : alexanderdna

void ILogger.Log(LogLevel level, string log)
            {
                var logFilePath = Path.Combine(AppContext.BaseDirectory, "debug.log");
                using var logFileStream = File.Open(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                using var logWriter = new StreamWriter(logFileStream);
                logWriter.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                logWriter.Write(": [");
                logWriter.Write(level);
                logWriter.Write("] ");
                logWriter.WriteLine(log);
                logWriter.Flush();
            }

19 View Source File : GroupContentsControlViewModel.cs
License : GNU General Public License v3.0
Project Creator : alexdillon

private void ShowFileSendDialog(string fileName)
        {
            FileStream fileStream = null;
            try
            {
                fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            catch (Exception)
            {
                try
                {
                    // When many files hosted on OneDrive are opened, they are hardlocked
                    // and cannot be opened for reading. Copying them to tmp typically is allowed though.
                    var tempFile = TempFileUtils.GetTempFileName(fileName);
                    File.Copy(fileName, tempFile, true);
                    fileStream = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    // The copied file will automatically be cleaned up by the temporary storage system.
                }
                catch (Exception)
                {
                }
            }

            var dialog = new SendFileControlViewModel()
            {
                ContentStream = fileStream,
                FileName = Path.GetFileName(fileName),
                MessageContainer = this.MessageContainer,
                TypedMessageContents = this.TypedMessageContents,
                SendMessage = new AsyncRelayCommand<List<Attachment>>(this.SendContentMessageAsync, (a) => !this.IsSending),
            };

            this.SmallDialogManager.OpenPopup(dialog, Guid.Empty);
        }

19 View Source File : GroupContentsControlViewModel.cs
License : GNU General Public License v3.0
Project Creator : alexdillon

private void ShowFileSendDialog(string fileName)
        {
            FileStream fileStream = null;
            try
            {
                fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            catch (Exception)
            {
                try
                {
                    // When many files hosted on OneDrive are opened, they are hardlocked
                    // and cannot be opened for reading. Copying them to tmp typically is allowed though.
                    var tempFile = TempFileUtils.GetTempFileName(fileName);
                    File.Copy(fileName, tempFile, true);
                    fileStream = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    // The copied file will automatically be cleaned up by the temporary storage system.
                }
                catch (Exception)
                {
                }
            }

            var dialog = new SendFileControlViewModel()
            {
                ContentStream = fileStream,
                FileName = Path.GetFileName(fileName),
                MessageContainer = this.MessageContainer,
                TypedMessageContents = this.TypedMessageContents,
                SendMessage = new AsyncRelayCommand<List<Attachment>>(this.SendContentMessageAsync, (a) => !this.IsSending),
            };

            this.SmallDialogManager.OpenPopup(dialog, Guid.Empty);
        }

19 View Source File : GroupContentsControlViewModel.cs
License : GNU General Public License v3.0
Project Creator : alexdillon

private void ShowFileSendDialog(string fileName)
        {
            var dialog = new SendFileControlViewModel()
            {
                ContentStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
                FileName = System.IO.Path.GetFileName(fileName),
                MessageContainer = this.MessageContainer,
                TypedMessageContents = this.TypedMessageContents,
                SendMessage = new RelayCommand<GroupMeClientApi.Models.Attachments.Attachment>(async (a) => await this.SendContentMessageAsync(a), (a) => !this.IsSending, true),
            };

            this.PopupManager.PopupDialog = dialog;
        }

19 View Source File : DataMockSI.cs
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Altinn

public async Task<DataElement> InsertBinaryData(string org, string app, int instanceOwnerId, Guid instanceGuid, string dataType, HttpRequest request)
        {
            Guid dataGuid = Guid.NewGuid();
            string dataPath = GetDataPath(org, app, instanceOwnerId, instanceGuid);
            Instance instance = GetTestInstance(app, org, instanceOwnerId, instanceGuid);
            DataElement dataElement = new DataElement() { Id = dataGuid.ToString(), DataType = dataType, ContentType = request.ContentType };

            if (!Directory.Exists(Path.GetDirectoryName(dataPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(dataPath));
            }

            Directory.CreateDirectory(dataPath + @"blob");

            long filesize;

            using (Stream streamToWriteTo = File.Open(dataPath + @"blob\" + dataGuid.ToString(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                await request.Body.CopyToAsync(streamToWriteTo);
                streamToWriteTo.Flush();
                filesize = streamToWriteTo.Length;
                streamToWriteTo.Close();
            }

            dataElement.Size = filesize;
            string jsonData = JsonConvert.SerializeObject(dataElement);
            using StreamWriter sw = new StreamWriter(dataPath + dataGuid.ToString() + @".json");

            sw.Write(jsonData.ToString());
            sw.Close();

            return dataElement;
        }

19 View Source File : DataMockSI.cs
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Altinn

public async Task<DataElement> InsertBinaryData(string instanceId, string dataType, string contentType, string filename, Stream stream)
        {
            Application app = _applicationService.GetApplication();

            Guid dataGuid = Guid.NewGuid();
            string dataPath = GetDataPath(app.Org, app.Id.Split("/")[1], Convert.ToInt32(instanceId.Split("/")[0]), new Guid(instanceId.Split("/")[1]));

            DataElement dataElement = new DataElement() { Id = dataGuid.ToString(), DataType = dataType, ContentType = contentType, };

            if (!Directory.Exists(Path.GetDirectoryName(dataPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(dataPath));
            }

            Directory.CreateDirectory(dataPath + @"blob");

            long filesize;

            using (Stream streamToWriteTo = File.Open(dataPath + @"blob\" + dataGuid.ToString(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                await stream.CopyToAsync(streamToWriteTo);
                streamToWriteTo.Flush();
                filesize = streamToWriteTo.Length;
            }

            dataElement.Size = filesize;
            string jsonData = JsonConvert.SerializeObject(dataElement);
            using StreamWriter sw = new StreamWriter(dataPath + dataGuid.ToString() + @".json");

            sw.Write(jsonData.ToString());
            sw.Close();

            return dataElement;
        }

19 View Source File : PolicyRepositoryMock.cs
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Altinn

private async Task<Response<BlobContentInfo>> WriteStreamToTestDataFolder(string filepath, Stream fileStream)
        {
            string dataPath = GetDataBlobPath() + filepath;

            if (!Directory.Exists(Path.GetDirectoryName(dataPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(dataPath));
            }

            int filesize;

            using (Stream streamToWriteTo = File.Open(dataPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                await fileStream.CopyToAsync(streamToWriteTo);
                streamToWriteTo.Flush();
                filesize = (int)streamToWriteTo.Length;
            }

            BlobContentInfo mockedBlobInfo = BlobsModelFactory.BlobContentInfo(new ETag("ETagSuccess"), DateTime.Now, new byte[1], DateTime.Now.ToUniversalTime().ToString(), "encryptionKeySha256", "encryptionScope", 1);
            Mock<Response<BlobContentInfo>> mockResponse = new Mock<Response<BlobContentInfo>>();
            mockResponse.SetupGet(r => r.Value).Returns(mockedBlobInfo);

            Mock<Response> responseMock = new Mock<Response>();
            responseMock.SetupGet(r => r.Status).Returns((int)HttpStatusCode.Created);
            mockResponse.Setup(r => r.GetRawResponse()).Returns(responseMock.Object);

            return mockResponse.Object;
        }

19 View Source File : DataRepository.cs
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Altinn

private async Task<long> WriteToFileInternal(string path, MemoryStream stream)
        {
            long fileSize;
            await using (FileStream streamToWriteTo = File.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                await stream.CopyToAsync(streamToWriteTo);
                streamToWriteTo.Flush();
                fileSize = streamToWriteTo.Length;
            }

            return fileSize;
        }

19 View Source File : SelfUpdateProgressPage.cs
License : GNU General Public License v3.0
Project Creator : Amebis

public override void OnActivate()
        {
            base.OnActivate();

            // Setup self-update.
            var selfUpdate = new BackgroundWorker() { WorkerReportsProgress = true };
            selfUpdate.DoWork += (object sender, DoWorkEventArgs e) =>
            {
                selfUpdate.ReportProgress(0);
                var random = new Random();
                var tempFolder = Path.GetTempPath();
                var workingFolder = tempFolder + Path.GetRandomFileName() + "\\";
                Directory.CreateDirectory(workingFolder);
                try
                {
                    string installerFilename = null;
                    FileStream installerFile = null;

                    // Download installer.
                    while (DownloadUris.Count > 0)
                    {
                        Window.Abort.Token.ThrowIfCancellationRequested();
                        var uriIndex = random.Next(DownloadUris.Count);
                        try
                        {
                            var binaryUri = DownloadUris[uriIndex];
                            Trace.TraceInformation("Downloading installer file from {0}...", binaryUri.AbsoluteUri);
                            var request = WebRequest.Create(binaryUri);
                            request.Proxy = null;
                            using (var response = request.GetResponse())
                            {
                                // 1. Get installer filename from Content-Disposition header.
                                // 2. Get installer filename from the last segment of URI path.
                                // 3. Fallback to a predefined installer filename.
                                try { installerFilename = Path.GetFullPath(workingFolder + new ContentDisposition(request.Headers["Content-Disposition"]).FileName); }
                                catch
                                {
                                    try { installerFilename = Path.GetFullPath(workingFolder + binaryUri.Segments[binaryUri.Segments.Length - 1]); }
                                    catch { installerFilename = Path.GetFullPath(workingFolder + Properties.Settings.Default.Clientreplacedle + " Client Setup.exe"); }
                                }

                                // Save response data to file.
                                installerFile = File.Open(installerFilename, FileMode.CreateNew, FileAccess.Write, FileShare.Read | FileShare.Inheritable);
                                try
                                {
                                    using (var stream = response.GetResponseStream())
                                    {
                                        installerFile.Seek(0, SeekOrigin.Begin);
                                        var hash = new eduEd25519.SHA256();
                                        var buffer = new byte[1048576];
                                        long offset = 0, total = response.ContentLength;

                                        for (; ; )
                                        {
                                            // Wait for the data to arrive.
                                            Window.Abort.Token.ThrowIfCancellationRequested();
                                            var bufferLength = stream.Read(buffer, 0, buffer.Length);
                                            if (bufferLength == 0)
                                                break;
                                            //Window.Abort.Token.WaitHandle.WaitOne(100); // Mock a slow link for testing.

                                            // Append it to the file and hash it.
                                            Window.Abort.Token.ThrowIfCancellationRequested();
                                            installerFile.Write(buffer, 0, bufferLength);
                                            hash.TransformBlock(buffer, 0, bufferLength, buffer, 0);

                                            // Report progress.
                                            offset += bufferLength;
                                            selfUpdate.ReportProgress((int)(offset * 100 / total));
                                        }

                                        hash.TransformFinalBlock(buffer, 0, 0);
                                        if (!hash.Hash.SequenceEqual(Hash))
                                            throw new DownloadedFileCorruptException(string.Format(Resources.Strings.ErrorDownloadedFileCorrupt, binaryUri.AbsoluteUri));

                                        installerFile.SetLength(installerFile.Position);
                                        break;
                                    }
                                }
                                catch
                                {
                                    // Close installer file.
                                    installerFile.Close();
                                    installerFile = null;

                                    // Delete installer file. If possible.
                                    Trace.TraceInformation("Deleting file {0}...", installerFilename);
                                    try { File.Delete(installerFilename); }
                                    catch (Exception ex2) { Trace.TraceWarning("Deleting {0} file failed: {1}", installerFilename, ex2.ToString()); }
                                    installerFilename = null;

                                    throw;
                                }
                            }
                        }
                        catch (OperationCanceledException) { throw; }
                        catch (Exception ex)
                        {
                            Trace.TraceWarning("Error: {0}", ex.ToString());
                            DownloadUris.RemoveAt(uriIndex);
                        }
                    }

                    if (installerFilename == null || installerFile == null)
                    {
                        // The installer file is not ready.
                        throw new InstallerFileUnavailableException();
                    }

                    try
                    {
                        var updaterFilename = Path.GetFullPath(workingFolder + Properties.Settings.Default.Clientreplacedle + " Client Setup and Relaunch.wsf");
                        var updaterFile = File.Open(updaterFilename, FileMode.CreateNew, FileAccess.Write, FileShare.Read | FileShare.Inheritable);
                        try
                        {
                            // Prepare WSF file.
                            var writer = new XmlTextWriter(updaterFile, null);
                            writer.WriteStartDoreplacedent();
                            writer.WriteStartElement("package");
                            writer.WriteStartElement("job");

                            writer.WriteStartElement("reference");
                            writer.WriteAttributeString("object", "WScript.Shell");
                            writer.WriteEndElement(); // reference

                            writer.WriteStartElement("reference");
                            writer.WriteAttributeString("object", "Scripting.FileSystemObject");
                            writer.WriteEndElement(); // reference

                            writer.WriteStartElement("script");
                            writer.WriteAttributeString("language", "JScript");
                            var installerArgumentsEsc = string.IsNullOrEmpty(Arguments) ? "" : " " + HttpUtility.JavaScriptStringEncode(Arguments);
                            var argv = Environment.GetCommandLineArgs();
                            var arguments = new StringBuilder();
                            for (long i = 1, n = argv.LongLength; i < n; i++)
                            {
                                if (i > 1) arguments.Append(" ");
                                arguments.Append("\"");
                                arguments.Append(argv[i].Replace("\"", "\"\""));
                                arguments.Append("\"");
                            }
                            var script = new StringBuilder();
                            script.AppendLine("var wsh = WScript.CreateObject(\"WScript.Shell\");");
                            script.AppendLine("wsh.Run(\"\\\"" + HttpUtility.JavaScriptStringEncode(installerFilename.Replace("\"", "\"\"")) + "\\\"" + installerArgumentsEsc + "\", 0, true);");
                            script.AppendLine("var fso = WScript.CreateObject(\"Scripting.FileSystemObject\");");
                            script.AppendLine("try { fso.DeleteFile(\"" + HttpUtility.JavaScriptStringEncode(installerFilename) + "\", true); } catch (err) {}");
                            script.AppendLine("try { fso.DeleteFile(\"" + HttpUtility.JavaScriptStringEncode(updaterFilename) + "\", true); } catch (err) {}");
                            script.AppendLine("try { fso.DeleteFolder(\"" + HttpUtility.JavaScriptStringEncode(workingFolder.TrimEnd(Path.DirectorySeparatorChar)) + "\", true); } catch (err) {}");
                            writer.WriteCData(script.ToString());
                            writer.WriteEndElement(); // script

                            writer.WriteEndElement(); // job
                            writer.WriteEndElement(); // package
                            writer.WriteEndDoreplacedent();
                            writer.Flush();

                            // Prepare WSF launch parameters.
                            Trace.TraceInformation("Launching update script file {0}...", updaterFilename);
                            var process = new Process();
                            process.StartInfo.FileName = "wscript.exe";
                            process.StartInfo.Arguments = "\"" + updaterFilename + "\"";
                            process.StartInfo.WorkingDirectory = workingFolder;

                            // Close WSF and installer files as late as possible to narrow the attack window.
                            // If Windows supported executing files that are locked for writing, we could leave those files open.
                            updaterFile.Close();
                            installerFile.Close();
                            process.Start();
                        }
                        catch
                        {
                            // Close WSF file.
                            updaterFile.Close();

                            // Delete WSF file. If possible.
                            Trace.TraceInformation("Deleting file {0}...", updaterFilename);
                            try { File.Delete(updaterFilename); }
                            catch (Exception ex2) { Trace.TraceWarning("Deleting {0} file failed: {1}", updaterFilename, ex2.ToString()); }

                            throw;
                        }
                    }
                    catch
                    {
                        // Close installer file.
                        installerFile.Close();

                        // Delete installer file. If possible.
                        Trace.TraceInformation("Deleting file {0}...", installerFilename);
                        try { File.Delete(installerFilename); }
                        catch (Exception ex2) { Trace.TraceWarning("Deleting {0} file failed: {1}", installerFilename, ex2.ToString()); }

                        throw;
                    }
                }
                catch
                {
                    // Delete working folder. If possible.
                    try { Directory.Delete(workingFolder); }
                    catch (Exception ex2) { Trace.TraceWarning("Deleting {0} folder failed: {1}", workingFolder, ex2.ToString()); }

                    throw;
                }
            };

            // Self-update progress.
            selfUpdate.ProgressChanged += (object sender, ProgressChangedEventArgs e) =>
            {
                Progress.Value = e.ProgressPercentage;
            };

            // Self-update complereplacedion.
            selfUpdate.RunWorkerCompleted += (object sender, RunWorkerCompletedEventArgs e) =>
            {
                if (e.Error == null)
                {
                    // Self-updating successfuly launched. Quit to release open files.
                    Wizard.OnQuitApplication(this);
                }
                else
                    Wizard.Error = e.Error;

                // Self-dispose.
                (sender as BackgroundWorker)?.Dispose();
            };

            selfUpdate.RunWorkerAsync();
        }

19 View Source File : EndPointListener.cs
License : MIT License
Project Creator : andruzzzhka

private static RSACryptoServiceProvider createRSAFromFile (string filename)
    {
      byte[] pvk = null;
      using (var fs = File.Open (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
        pvk = new byte[fs.Length];
        fs.Read (pvk, 0, pvk.Length);
      }

      var rsa = new RSACryptoServiceProvider ();
      rsa.ImportCspBlob (pvk);

      return rsa;
    }

19 View Source File : FileLogger.cs
License : MIT License
Project Creator : angelsix

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            // If we should not log...
            if (!IsEnabled(logLevel))
                // Return
                return;

            // Get current time
            var currentTime = DateTimeOffset.Now.ToString("yyyy-MM-dd hh:mm:ss");

            // Prepend log level
            var logLevelString = mConfiguration.OutputLogLevel ? $"{logLevel.ToString().ToUpper()}: " : "";

            // Prepend the time to the log if desired
            var timeLogString = mConfiguration.LogTime ? $"[{currentTime}] " : "";

            // Get the formatted message string
            var message = formatter(state, exception);

            // Write the message
            var output = $"{logLevelString}{timeLogString}{message}{Environment.NewLine}";

            // Normalize path
            // TODO: Make use of configuration base path
            var normalizedPath = mFilePath.ToUpper();

            var fileLock = default(object);

            // Double safety even though the FileLocks should be thread safe
            lock (FileLockLock)
            {
                // Get the file lock based on the absolute path
                fileLock = FileLocks.GetOrAdd(normalizedPath, path => new object());
            }

            // Lock the file
            lock (fileLock)
            {
                // Ensure folder
                if (!Directory.Exists(mDirectory))
                    Directory.CreateDirectory(mDirectory);

                // Open the file
                using (var fileStream = new StreamWriter(File.Open(mFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)))
                {
                    // Go to end
                    fileStream.BaseStream.Seek(0, SeekOrigin.End);

                    // NOTE: Ignore logToTop in configuration as not efficient for files on OS

                    // Write the message to the file
                    fileStream.Write(output);
                }
            }
        }

19 View Source File : DictionaryCompilerBase.cs
License : Apache License 2.0
Project Creator : AnkiUniversal

private void BuildTokenInfoDictionary(string inputDirAbsolutePath, string outputDirAbsolutePath, string encoding, EncodingProvider provider)
        {
            try
            {
                ProgressLog.Begin("compiling tokeninfo dict");
                var tokenInfoCompiler = GetTokenInfoDictionaryCompiler(encoding, provider);

                ProgressLog.Println("replacedyzing dictionary features");                

                using (var stream = tokenInfoCompiler.CombinedSequentialFileInputStream(inputDirAbsolutePath))
                {
                    tokenInfoCompiler.replacedyzeTokenInfo(stream);
                    ProgressLog.Println("reading tokeninfo");
                    tokenInfoCompiler.ReadTokenInfo(stream);
                    tokenInfoCompiler.Compile(stream);
                }

                List<string> surfaces = tokenInfoCompiler.GetSurfaces();

                ProgressLog.Begin("compiling fst");

                FSTCompiler fstCompiler = new FSTCompiler(surfaces);
                using (var stream = File.Open(outputDirAbsolutePath + Path.DirectorySeparatorChar + FST.FST.FST_FILENAME, FileMode.OpenOrCreate))
                {
                    fstCompiler.Compile(stream);
                }

                ProgressLog.Println("validating saved fst");

                FST.FST fst;
                using (var stream = File.OpenRead(outputDirAbsolutePath + Path.DirectorySeparatorChar + FST.FST.FST_FILENAME))
                {
                    fst = new FST.FST(stream);
                }

                foreach (string surface in surfaces)
                {
                    if (fst.Lookup(surface) < 0)
                    {
                        ProgressLog.Println("failed to look up [" + surface + "]");
                    }
                }

                ProgressLog.End();

                ProgressLog.Begin("processing target map");

                for (int i = 0; i < surfaces.Count; i++)
                {
                    int id = fst.Lookup(surfaces[i]);
                    tokenInfoCompiler.AddMapping(id, i);
                }

                tokenInfoCompiler.Write(outputDirAbsolutePath); // TODO: Should be refactored -Christian
                ProgressLog.End();
            }
            catch (Exception ex)
            {
                throw new Exception("DictionaryCompilerBase.BuildTokenInfoDictionary: " + ex.Message);
            }
        }

19 View Source File : WAD.cs
License : GNU General Public License v3.0
Project Creator : anotak

private void Open(string pathfilename)
		{
			FileAccess access;
			FileShare share;
			
			// Determine if opening for read only
			if(isreadonly)
			{
				// Read only
				access = FileAccess.Read;
				share = FileShare.ReadWrite;
			}
			else
			{
				// Private access
				access = FileAccess.ReadWrite;
				share = FileShare.Read;
			}
			
			// Keep filename
			filename = pathfilename;
			
			// Open the file stream
			file = File.Open(pathfilename, FileMode.OpenOrCreate, access, share);

			// Create file handling tools
			reader = new BinaryReader(file, ENCODING);
			if(!isreadonly) writer = new BinaryWriter(file, ENCODING);

			// Is the WAD file zero length?
			if(file.Length == 0)
			{
				// Create the headers in file
				CreateHeaders();
			}
			else
			{
				// Read information from file
				ReadHeaders();
			}
		}

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(bool val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadBool(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteBool(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(sbyte val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadSByte(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteSByte(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(byte val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadByte(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteByte(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(short val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadShort(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteShort(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(ushort val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadUShort(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteUShort(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(int val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadInt(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteInt(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(uint val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadUInt(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteUInt(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(long val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadLong(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteLong(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(ulong val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadULong(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteULong(stream, val);
            }
        }

19 View Source File : Dbg.cs
License : Apache License 2.0
Project Creator : AnthonyLloyd

public void Add(DateTime val)
        {
            if (reading)
            {
                var val2 = Hash.StreamSerializer.ReadDateTime(stream);
                if (val != val2)
                    throw new CsCheckException($"Actual {val} but Expected {val2}. (last string was {lastString})");
            }
            else
            {
                using var stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.None);
                Hash.StreamSerializer.WriteDateTime(stream, val);
            }
        }

See More Examples