public void Dispose() { var exts = new [] { ".md", ".exe" }; Utils.DeleteDirectory(Path.Combine(WorkingDirectory, ID)); Profiles.Do(p => { foreach (var ext in exts) { var path = Path.Combine(Directory.GetCurrentDirectory(), p + ext); if (File.Exists(path)) { File.Delete(path); } } }); }
public void Write() { var mainStream = File.Open(_path, FileMode.Create, FileAccess.Write); _streams.Add(mainStream); switch (Version) { case PackageVersion.V16: { WriteV16(mainStream); break; } case PackageVersion.V15: { WriteV15(mainStream); break; } case PackageVersion.V13: { WriteV13(mainStream); break; } case PackageVersion.V10: { WriteV10(mainStream); break; } case PackageVersion.V9: case PackageVersion.V7: { WriteV7(mainStream); break; } default: { throw new ArgumentException($"Cannot write version {Version} packages"); } } }
public static void GenerateMerges(Installer installer) { installer.ModList .Directives .OfType <MergedPatch>() .PMap(m => { Utils.LogStatus($"Generating zEdit merge: {m.To}"); var src_data = m.Sources.Select(s => File.ReadAllBytes(Path.Combine(installer.Outputfolder, s.RelativePath))) .ConcatArrays(); var patch_data = installer.LoadBytesFromPath(m.PatchID); using (var fs = File.OpenWrite(Path.Combine(installer.Outputfolder, m.To))) BSDiff.Apply(new MemoryStream(src_data), () => new MemoryStream(patch_data), fs); }); }
private void ForcePortable() { var path = Path.Combine(OutputFolder, "portable.txt"); if (File.Exists(path)) { return; } try { File.WriteAllText(path, "Created by Wabbajack"); } catch (Exception e) { Utils.Error(e, $"Could not create portable.txt in {OutputFolder}"); } }
private void WriteRemappedFile(RemappedInlineFile directive) { var data = Encoding.UTF8.GetString(LoadBytesFromPath(directive.SourceDataID)); data = data.Replace(Consts.GAME_PATH_MAGIC_BACK, GameFolder); data = data.Replace(Consts.GAME_PATH_MAGIC_DOUBLE_BACK, GameFolder.Replace("\\", "\\\\")); data = data.Replace(Consts.GAME_PATH_MAGIC_FORWARD, GameFolder.Replace("\\", "/")); data = data.Replace(Consts.MO2_PATH_MAGIC_BACK, OutputFolder); data = data.Replace(Consts.MO2_PATH_MAGIC_DOUBLE_BACK, OutputFolder.Replace("\\", "\\\\")); data = data.Replace(Consts.MO2_PATH_MAGIC_FORWARD, OutputFolder.Replace("\\", "/")); data = data.Replace(Consts.DOWNLOAD_PATH_MAGIC_BACK, DownloadFolder); data = data.Replace(Consts.DOWNLOAD_PATH_MAGIC_DOUBLE_BACK, DownloadFolder.Replace("\\", "\\\\")); data = data.Replace(Consts.DOWNLOAD_PATH_MAGIC_FORWARD, DownloadFolder.Replace("\\", "/")); File.WriteAllText(Path.Combine(OutputFolder, directive.To), data); }
public void File_ReadAllText() { Console.WriteLine("File.ReadAllText()\n"); // Create file and append text. var tempFile = Path.GetTempFileName(); string[] createText = { "Hello", "And", "Welcome" }; File.WriteAllLines(tempFile, createText); // Open the file to read from. var textRead = File.ReadAllText(tempFile); Console.WriteLine(textRead); File.Delete(tempFile, true); Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed."); }
public void File_WriteAllText() { Console.WriteLine("File.WriteAllText()"); Console.WriteLine("\n\tDefault AlphaFS Encoding: [{0}]", NativeMethods.DefaultFileEncoding.EncodingName); // Create file and append text. var tempFile = Path.GetTempFileName(); var allLines = UnitTestConstants.TextHelloWorld; // Create real UTF-8 file. File.WriteAllText(tempFile, allLines, NativeMethods.DefaultFileEncoding); // Read filestream contents. using (var streamRead = File.OpenText(tempFile)) { var line = streamRead.ReadToEnd(); Console.WriteLine("\n\tCreated: [{0}] filestream: [{1}]\n\n\tWriteAllText content:\n{2}", streamRead.CurrentEncoding.EncodingName, tempFile, line); Assert.IsTrue(line.Contains(allLines)); } // (over)Write. File.WriteAllText(tempFile, "Append 1"); File.WriteAllText(tempFile, allLines); File.WriteAllText(tempFile, "Append 2"); File.WriteAllText(tempFile, allLines); // Read filestream contents. using (var streamRead = File.OpenText(tempFile)) { var line = streamRead.ReadToEnd(); Console.WriteLine("\tWriteAllText content:\n{0}", line); Assert.IsTrue(line.Contains(allLines)); Assert.IsTrue(!line.Contains("Append 1")); Assert.IsTrue(!line.Contains("Append 2")); } File.Delete(tempFile, true); Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed."); }
private static void ProcessFile(string file, bool q, string dt) { try { var raw = File.ReadAllBytes(file); if (raw[0] == 1 || raw[0] == 2) { var di = new DollarI(raw, file); DisplayDollarI(di, dt, q); } else if (raw[0] == 5) { var info = new Info2(raw, file); DisplayInfo2(info, q, dt); } else { Log.Warning( "Unknown header 0x{Raw:X}! Send file to {Email} so support can be added", raw[0], "*****@*****.**"); _failedFiles.Add(file); } if (q == false) { Console.WriteLine(); } } catch (UnauthorizedAccessException ua) { Log.Error(ua, "Unable to access {File}. Are you running as an administrator? Error: {Message}", file, ua.Message); _failedFiles.Add(file); } catch (Exception ex) { _failedFiles.Add(file); Log.Error(ex, "Error processing file {File} Please send it to {Email}. Error: {Message}", file, "*****@*****.**", ex.Message); } }
private static void ProcessFile(string file) { try { var raw = File.ReadAllBytes(file); if (raw[0] == 1 || raw[0] == 2) { var di = new DollarI(raw, file); DisplayDollarI(di); } else if (raw[0] == 5) { var info = new Info2(raw, file); DisplayInfo2(info); } else { _logger.Warn( $"Unknown header '0x{raw[0]:X}! Send file to [email protected] so support can be added"); _failedFiles.Add(file); } if (_fluentCommandLineParser.Object.Quiet == false) { _logger.Info(""); } } catch (UnauthorizedAccessException ua) { _logger.Error( $"Unable to access '{_fluentCommandLineParser.Object.File}'. Are you running as an administrator? Error: {ua.Message}"); _failedFiles.Add(file); } catch (Exception ex) { _failedFiles.Add(file); _logger.Error( $"Error processing file '{_fluentCommandLineParser.Object.File}' Please send it to [email protected]. Error: {ex.Message}"); } }
public void Configure() { File.WriteAllLines(Path.Combine(MO2Folder, "ModOrganizer.ini"), new [] { "[General]", $"gameName={Game.MetaData().MO2Name}", $"gamePath={GameFolder.Replace("\\", "\\\\")}", $"download_directory={DownloadsFolder}" }); Directory.CreateDirectory(DownloadsFolder); Directory.CreateDirectory(Path.Combine(GameFolder, "Data")); Profiles.Do(profile => { File.WriteAllLines(Path.Combine(MO2Folder, "profiles", profile, "modlist.txt"), Mods.Select(s => $"+{s}").ToArray()); }); }
public static void Rotate(string filenameBase) { if (File.Exists(filenameBase)) { for (int i = 8; i >= 0; i--) { string fn = filenameBase + "." + i; if (File.Exists(fn)) { string fn2 = filenameBase + "." + (i + 1); if (File.Exists(fn2)) { File.Delete(fn2); } File.Move(fn, fn2); } } File.Copy(filenameBase, filenameBase + ".0"); } }
internal List <string> GetArchiveEntryNames(VirtualFile file) { if (!file.IsStaged) { throw new InvalidDataException("File is not staged"); } if (file.Extension == ".bsa") { using (var ar = new BSAReader(file.StagedPath)) { return(ar.Files.Select(f => f.Path).ToList()); } } if (file.Extension == ".zip") { using (var s = new ZipFile(File.OpenRead(file.StagedPath))) { s.IsStreamOwner = true; s.UseZip64 = UseZip64.On; if (s.OfType <ZipEntry>().FirstOrDefault(e => !e.CanDecompress) == null) { return(s.OfType <ZipEntry>() .Where(f => f.IsFile) .Select(f => f.Name.Replace('/', '\\')) .ToList()); } } } /* * using (var e = new ArchiveFile(file.StagedPath)) * { * return e.Entries * .Where(f => !f.IsFolder) * .Select(f => f.FileName).ToList(); * }*/ return(null); }
public void SyncToDisk() { if (!_disableDiskCache) { Utils.Status("Syncing VFS Cache"); lock (this) { try { _isSyncing = true; if (File.Exists("vfs_cache.bin_new")) { File.Delete("vfs_cache.bin_new"); } using (var fs = File.OpenWrite("vfs_cache.bin_new")) using (var bw = new BinaryWriter(fs)) { Utils.Log($"Syncing VFS to Disk: {_files.Count} entries"); foreach (var f in _files.Values) { f.Write(bw); } } if (File.Exists("vfs_cache.bin")) { File.Delete("vfs_cache.bin"); } File.Move("vfs_cache.bin_new", "vfs_cache.bin"); } finally { _isSyncing = false; } } } }
public Package Read() { var mainStream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read); using (var reader = new BinaryReader(mainStream, new UTF8Encoding(), true)) { // Check for v13 package headers mainStream.Seek(-8, SeekOrigin.End); Int32 headerSize = reader.ReadInt32(); byte[] signature = reader.ReadBytes(4); if (Package.Signature.SequenceEqual(signature)) { mainStream.Seek(-headerSize, SeekOrigin.End); return(ReadPackageV13(mainStream, reader)); } // Check for v10 package headers mainStream.Seek(0, SeekOrigin.Begin); signature = reader.ReadBytes(4); Int32 version; if (Package.Signature.SequenceEqual(signature)) { version = reader.ReadInt32(); if (version == 10) { return(ReadPackageV10(mainStream, reader)); } } // Check for v9 and v7 package headers mainStream.Seek(0, SeekOrigin.Begin); version = reader.ReadInt32(); if (version == 7 || version == 9) { return(ReadPackageV7(mainStream, reader)); } throw new NotAPackageException("No valid signature found in package file"); } }
public void VerifyAllFiles() { foreach (var dest_file in Directory.EnumerateFiles(InstallFolder, "*", DirectoryEnumerationOptions.Recursive)) { var rel_file = dest_file.RelativeTo(InstallFolder); if (rel_file.StartsWith(Consts.LOOTFolderFilesDir) || rel_file.StartsWith(Consts.GameFolderFilesDir)) { continue; } Assert.IsTrue(File.Exists(Path.Combine(MO2Folder, rel_file)), $"Only in Destination: {rel_file}"); } var skip_extensions = new HashSet <string> { ".txt", ".ini" }; foreach (var src_file in Directory.EnumerateFiles(MO2Folder, "*", DirectoryEnumerationOptions.Recursive)) { var rel_file = src_file.RelativeTo(MO2Folder); if (rel_file.StartsWith("downloads\\")) { continue; } var dest_file = Path.Combine(InstallFolder, rel_file); Assert.IsTrue(File.Exists(dest_file), $"Only in Source: {rel_file}"); var fi_src = new FileInfo(src_file); var fi_dest = new FileInfo(dest_file); if (!skip_extensions.Contains(Path.GetExtension(src_file))) { Assert.AreEqual(fi_src.Length, fi_dest.Length, $"Differing sizes {rel_file}"); Assert.AreEqual(src_file.FileHash(), dest_file.FileHash(), $"Differing content hash {rel_file}"); } } }
public async Task MegaDownload() { var ini = @"[General] directURL=https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k"; var state = (AbstractDownloadState)await DownloadDispatcher.ResolveArchive(ini.LoadIniString()); Assert.IsNotNull(state); var url_state = DownloadDispatcher.ResolveArchive( "https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k"); Assert.AreEqual("https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k", ((MegaDownloader.State)url_state).Url); var converted = await state.RoundTripState(); Assert.IsTrue(await converted.Verify(new Archive { Size = 20 })); var filename = Guid.NewGuid().ToString(); Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List <string> { "https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k" } })); Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List <string> { "blerg" } })); await converted.Download(new Archive { Name = "MEGA Test.txt" }, filename); Assert.AreEqual("eSIyd+KOG3s=", Utils.FileHash(filename)); Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!"); }
// 导出数据 private void ExportData(object sender, RoutedEventArgs e) { if (!File.Exists("./data.db")) { MessageWindow.ShowDialog("文件 data.db 不存在", this); return; } SaveFileDialog dlg = new SaveFileDialog { Title = "另存为", FileName = "data.db", DefaultExt = ".db", Filter = "Data base file|*.db" }; if (dlg.ShowDialog() == true) { FileInfo fi = new FileInfo("./data.db"); fi.CopyTo(dlg.FileName); } }
public async Task DropboxTests() { var ini = @"[General] directURL=https://www.dropbox.com/s/5hov3m2pboppoc2/WABBAJACK_TEST_FILE.txt?dl=0"; var state = (AbstractDownloadState)await DownloadDispatcher.ResolveArchive(ini.LoadIniString()); Assert.IsNotNull(state); var url_state = DownloadDispatcher.ResolveArchive( "https://www.dropbox.com/s/5hov3m2pboppoc2/WABBAJACK_TEST_FILE.txt?dl=0"); Assert.AreEqual("https://www.dropbox.com/s/5hov3m2pboppoc2/WABBAJACK_TEST_FILE.txt?dl=1", ((HTTPDownloader.State)url_state).Url); var converted = await state.RoundTripState(); Assert.IsTrue(await converted.Verify(new Archive { Size = 20 })); var filename = Guid.NewGuid().ToString(); Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List <string> { "https://www.dropbox.com/s/5hov3m2pboppoc2/WABBAJACK_TEST_FILE.txt?" } })); Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List <string> { "blerg" } })); await converted.Download(new Archive { Name = "MEGA Test.txt" }, filename); Assert.AreEqual("eSIyd+KOG3s=", Utils.FileHash(filename)); Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!"); }
public void GetFilesWithExtension_FileContainingPatternUseToReturn() { const string name1 = "G-SN750B_02_S13UJ1NQ221583.cde"; const string name2 = "G-SN750B_02_S13UJ1NQ221583.cde-backup-with-hash"; var f1 = File.Create(name1); var f2 = File.Create(name2); f1.Close(); f2.Close(); var files = AlphaFSHelper.GetFilesWithExtension(".", "cde"); foreach (var file in files) { Console.WriteLine($"file {file}"); } //System.Threading.Thread.Sleep(1000); // delay 1 second File.Delete(name1); File.Delete(name2); Assert.That(files.Count(), Is.EqualTo(1), "Oops somehow we got a file not ending in \"cde\" in our result set."); }
private static void SetupNLog() { if (File.Exists(Path.Combine(BaseDirectory, "Nlog.config"))) { return; } var config = new LoggingConfiguration(); var loglevel = LogLevel.Info; var layout = @"${message}"; var consoleTarget = new ColoredConsoleTarget(); config.AddTarget("console", consoleTarget); consoleTarget.Layout = layout; var rule1 = new LoggingRule("*", loglevel, consoleTarget); config.LoggingRules.Add(rule1); LogManager.Configuration = config; }
public void VerifyInstalledGameFile(string file) { var src = Path.Combine(GameFolder, file); Assert.IsTrue(File.Exists(src), src); var dest = Path.Combine(InstallFolder, Consts.GameFolderFilesDir, file); Assert.IsTrue(File.Exists(dest), dest); var src_data = File.ReadAllBytes(src); var dest_data = File.ReadAllBytes(dest); Assert.AreEqual(src_data.Length, dest_data.Length); for (int x = 0; x < src_data.Length; x++) { if (src_data[x] != dest_data[x]) { Assert.Fail($"Index {x} of {Consts.GameFolderFilesDir}\\{file} are not the same"); } } }
private static void DeleteDirectory(string path) { Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories) .DoProgress("Cleaning VFS Files", file => { try { var fi = new FileInfo(file); fi.Attributes &= ~FileAttributes.ReadOnly; File.Delete(file); } catch (Exception ex) { Utils.Log(ex.ToString()); } }); Directory.EnumerateDirectories(path, DirectoryEnumerationOptions.Recursive) .OrderByDescending(d => d.Length) .DoProgress("Cleaning VFS Folders", folder => { try { if (!Directory.Exists(folder)) { return; } var di = new DirectoryInfo(folder); di.Attributes &= ~FileAttributes.ReadOnly; Directory.Delete(path, true); } catch (Exception ex) { Utils.Log(ex.ToString()); } }); }
private void LoadFromDisk() { try { Utils.Log("Loading VFS Cache"); if (!File.Exists("vfs_cache.bin")) { return; } _files = new Dictionary <string, VirtualFile>(); try { using (var fs = File.OpenRead("vfs_cache.bin")) using (var br = new BinaryReader(fs)) { while (true) { var fr = VirtualFile.Read(br); _files.Add(fr.FullPath, fr); } } } catch (EndOfStreamException ex) { } CleanDB(); } catch (Exception ex) { Utils.Log($"Purging cache due to {ex}"); File.Delete("vfs_cache.bson"); _files.Clear(); } }
public async Task GoogleDriveTests() { var ini = @"[General] directURL=https://drive.google.com/file/d/1grLRTrpHxlg7VPxATTFNfq2OkU_Plvh_/view?usp=sharing"; var state = (AbstractDownloadState)await DownloadDispatcher.ResolveArchive(ini.LoadIniString()); Assert.IsNotNull(state); var url_state = DownloadDispatcher.ResolveArchive( "https://drive.google.com/file/d/1grLRTrpHxlg7VPxATTFNfq2OkU_Plvh_/view?usp=sharing"); Assert.AreEqual("1grLRTrpHxlg7VPxATTFNfq2OkU_Plvh_", ((GoogleDriveDownloader.State)url_state).Id); var converted = await state.RoundTripState(); Assert.IsTrue(await converted.Verify(new Archive { Size = 20 })); var filename = Guid.NewGuid().ToString(); Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist { GoogleIDs = new List <string> { "1grLRTrpHxlg7VPxATTFNfq2OkU_Plvh_" } })); Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist { GoogleIDs = new List <string>() })); await converted.Download(new Archive { Name = "MEGA Test.txt" }, filename); Assert.AreEqual("eSIyd+KOG3s=", Utils.FileHash(filename)); Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!"); }
/// <summary> /// Documentation: https://developers.google.com/drive/v3/web/manage-downloads /// </summary> /// <param name="_service"></param> /// <param name="_fileResource"></param> /// <param name="_saveTo"></param> public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo) { var request = _service.Files.Get(_fileResource.Id); var stream = new System.IO.MemoryStream(); request.Download(stream); System.IO.FileStream file = new System.IO.FileStream(_saveTo, System.IO.FileMode.Create, System.IO.FileAccess.Write); try { stream.WriteTo(file); } catch (Exception x) { log.Error("Exception[4]: " + x.Message + " | " + x); } finally { file.Close(); stream.Close(); } return true; }
/// <summary> /// Documentation: https://developers.google.com/drive/v3/reference/files/update /// </summary> /// <param name="_service"></param> /// <param name="_uploadFile"></param> /// <param name="_fileId"></param> /// <returns></returns> public static File updateFile(DriveService _service, string _uploadFile, string _fileId) { Pause(); log.Info("Update file: " + _uploadFile); System.IO.FileInfo f = new System.IO.FileInfo(_uploadFile); File file = new File(); fileSize = f.Length; string name = Alpha.Path.GetFileName(_uploadFile); FileName = name; using (var stream = new System.IO.FileStream(_uploadFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { FilesResource.UpdateMediaUpload request = _service.Files.Update(file, _fileId, stream, GetMimeType(_uploadFile)); try { request.ChunkSize = 1048576; _ChunkSize = 1048576; request.ProgressChanged += Upload_ProgressChanged; request.Upload(); while (request.ContentStream.Position != request.ContentStream.Length) { Pause(); for (int i = 15; i > 0; i = i - 1) { log.Error("Network error. Resume attempt."); Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { ((MainWindow)Application.Current.MainWindow).lb_currUpload.Content = "Network error. Resume attempt in: " + i; })); Thread.Sleep(1000); } request.Resume(); } return request.ResponseBody; } catch (Exception x) { log.Error("Exception[6]: " + x.Message + " | " + x); return null; } } }
public async Task GameFileSourceDownload() { // Test mode off for this test Consts.TestMode = false; await DownloadDispatcher.GetInstance <LoversLabDownloader>().Prepare(); var ini = $@"[General] gameName={Game.SkyrimSpecialEdition.MetaData().MO2ArchiveName} gameFile=Data/Update.esm"; var state = (AbstractDownloadState)await DownloadDispatcher.ResolveArchive(ini.LoadIniString()); Assert.IsNotNull(state); var converted = await state.RoundTripState(); Assert.IsTrue(await converted.Verify(new Archive { Size = 20 })); var filename = Guid.NewGuid().ToString(); Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List <string>() })); await converted.Download(new Archive { Name = "Update.esm" }, filename); Assert.AreEqual("/DLG/LjdGXI=", Utils.FileHash(filename)); CollectionAssert.AreEqual(File.ReadAllBytes(Path.Combine(Game.SkyrimSpecialEdition.MetaData().GameLocation(), "Data/Update.esm")), File.ReadAllBytes(filename)); Consts.TestMode = true; }
private static void ProcessFile(string file) { if (File.Exists(file) == false) { _logger.Warn($"'{file}' does not exist! Skipping"); return; } _logger.Warn($"\r\nProcessing '{file}'..."); Stream fileS; try { fileS = new FileStream(file, FileMode.Open, FileAccess.Read); } catch (Exception) { //file is in use if (Helper.IsAdministrator() == false) { _logger.Fatal("\r\nAdministrator privileges not found! Exiting!!\r\n"); Environment.Exit(0); } _logger.Warn($"\r\n'{file}' is in use. Rerouting..."); var files = new List <string>(); files.Add(file); var rawFiles = Helper.GetFiles(files); fileS = rawFiles.First().FileStream; } try { var evt = new EventLog(fileS); var seenRecords = 0; foreach (var eventRecord in evt.GetEventRecords()) { if (_includeIds.Count > 0) { if (_includeIds.Contains(eventRecord.EventId) == false) { //it is NOT in the list, so skip continue; } } else if (_excludeIds.Count > 0) { if (_excludeIds.Contains(eventRecord.EventId)) { //it IS in the list, so skip continue; } } eventRecord.SourceFile = file; try { _csvWriter?.WriteRecord(eventRecord); _csvWriter?.NextRecord(); var xml = string.Empty; if (_swXml != null) { xml = eventRecord.ConvertPayloadToXml(); _swXml.WriteLine(xml); } if (_swJson != null) { JsConfig.IncludeNullValues = true; JsConfig.DateHandler = DateHandler.ISO8601; var jsOut = eventRecord.ToJson(); if (_fluentCommandLineParser.Object.FullJson) { if (xml.IsNullOrEmpty()) { xml = eventRecord.ConvertPayloadToXml(); } var xd = new XmlDocument(); xd.LoadXml(xml); jsOut = JsonConvert.SerializeXmlNode(xd); } _swJson.WriteLine(jsOut); } seenRecords += 1; } catch (Exception e) { _logger.Error($"Error processing record #{eventRecord.RecordNumber}: {e.Message}"); } } if (evt.ErrorRecords.Count > 0) { _errorFiles.Add(file, evt.ErrorRecords.Count); } _fileCount += 1; _logger.Info(""); _logger.Fatal("Event log details"); _logger.Info(evt); _logger.Info($"Records processed: {seenRecords:N0} Errors: {evt.ErrorRecords.Count:N0}"); if (evt.ErrorRecords.Count > 0) { _logger.Warn("\r\nErrors"); foreach (var evtErrorRecord in evt.ErrorRecords) { _logger.Info($"Record #{evtErrorRecord.Key}: Error: {evtErrorRecord.Value}"); } } if (_fluentCommandLineParser.Object.Metrics && evt.EventIdMetrics.Count > 0) { _logger.Fatal("\r\nMetrics"); _logger.Warn("Event Id\tCount"); foreach (var esEventIdMetric in evt.EventIdMetrics.OrderBy(t => t.Key)) { if (_includeIds.Count > 0) { if (_includeIds.Contains((int)esEventIdMetric.Key) == false) { //it is NOT in the list, so skip continue; } } else if (_excludeIds.Count > 0) { if (_excludeIds.Contains((int)esEventIdMetric.Key)) { //it IS in the list, so skip continue; } } _logger.Info($"{esEventIdMetric.Key}\t\t{esEventIdMetric.Value:N0}"); } } } catch (Exception e) { if (e.Message.Contains("Invalid signature! Expected 'ElfFile")) { _logger.Info($"'{file}' is not an evtx file! Message: {e.Message} Skipping..."); } else { _logger.Error($"Error processing '{file}'! Message: {e.Message}"); } } fileS?.Close(); }
public void SetVolumeMountPoint() { Console.WriteLine("Volume.SetVolumeMountPoint()\n"); if (!IsAdmin()) { Assert.Fail(); } #region Logical Drives int cnt = 0; string destFolder = Path.Combine(TempFolder, "Volume.SetVolumeMountPoint()-" + Path.GetRandomFileName()); Directory.CreateDirectory(destFolder); string guid = Volume.GetUniqueVolumeNameForPath(SysDrive); try { StopWatcher(true); Volume.SetVolumeMountPoint(destFolder, guid); Console.WriteLine( "\t#{0:000}\tSystem Drive: [{1}]\tGUID: [{2}]\n\t\tDestination : [{3}]\n\t\tCreated Mount Point.\n\t{4}", ++cnt, SysDrive, guid, destFolder, Reporter(true)); Console.WriteLine("\n"); EnumerateVolumeMountPoints(); Console.WriteLine("\n\nFile.GetLinkTargetInfo()"); LinkTargetInfo lti = File.GetLinkTargetInfo(destFolder); Assert.IsTrue(!string.IsNullOrWhiteSpace(lti.PrintName)); Assert.IsTrue(!string.IsNullOrWhiteSpace(lti.SubstituteName)); Assert.IsTrue(Dump(lti, -14), "Unable to dump object."); // Cleanup. StopWatcher(true); bool deleteOk = false; try { Volume.DeleteVolumeMountPoint(destFolder); deleteOk = true; } catch (Exception ex) { Console.WriteLine("\nCaught Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, " ")); } Console.WriteLine("\n\nVolume.DeleteVolumeMountPoint() (Should be True): [{0}]\tFolder: [{1}]\n{2}\n", deleteOk, destFolder, Reporter()); Directory.Delete(destFolder, true, true); Assert.IsTrue(deleteOk && !Directory.Exists(destFolder)); EnumerateVolumeMountPoints(); } catch (Exception ex) { Console.WriteLine("\nCaught Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, " ")); cnt = 0; } finally { // Always remove mount point. // Experienced: CCleaner deletes through mount points! try { Volume.DeleteVolumeMountPoint(destFolder); } catch { } } Assert.IsTrue(cnt > 0, "Nothing was enumerated."); #endregion // Logical Drives }
private static void Main(string[] args) { ExceptionlessClient.Default.Startup("tYeWS6A5K5uItgpB44dnNy2qSb2xJxiQWRRGWebq"); SetupNLog(); _logger = LogManager.GetLogger("EvtxECmd"); _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments> { IsCaseSensitive = false }; _fluentCommandLineParser.Setup(arg => arg.File) .As('f') .WithDescription("File to process. This or -d is required\r\n"); _fluentCommandLineParser.Setup(arg => arg.Directory) .As('d') .WithDescription("Directory to process that contains evtx files. This or -f is required"); _fluentCommandLineParser.Setup(arg => arg.CsvDirectory) .As("csv") .WithDescription( "Directory to save CSV formatted results to."); // This, --json, or --xml required _fluentCommandLineParser.Setup(arg => arg.CsvName) .As("csvf") .WithDescription( "File name to save CSV formatted results to. When present, overrides default name"); _fluentCommandLineParser.Setup(arg => arg.JsonDirectory) .As("json") .WithDescription( "Directory to save JSON formatted results to."); // This, --csv, or --xml required _fluentCommandLineParser.Setup(arg => arg.JsonName) .As("jsonf") .WithDescription( "File name to save JSON formatted results to. When present, overrides default name"); _fluentCommandLineParser.Setup(arg => arg.XmlDirectory) .As("xml") .WithDescription( "Directory to save XML formatted results to."); // This, --csv, or --json required _fluentCommandLineParser.Setup(arg => arg.XmlName) .As("xmlf") .WithDescription( "File name to save XML formatted results to. When present, overrides default name\r\n"); _fluentCommandLineParser.Setup(arg => arg.DateTimeFormat) .As("dt") .WithDescription( "The custom date/time format to use when displaying time stamps. Default is: yyyy-MM-dd HH:mm:ss.fffffff") .SetDefault("yyyy-MM-dd HH:mm:ss.fffffff"); _fluentCommandLineParser.Setup(arg => arg.IncludeIds) .As("inc") .WithDescription( "List of event IDs to process. All others are ignored. Overrides --exc Format is 4624,4625,5410") .SetDefault(string.Empty); _fluentCommandLineParser.Setup(arg => arg.ExcludeIds) .As("exc") .WithDescription( "List of event IDs to IGNORE. All others are included. Format is 4624,4625,5410") .SetDefault(string.Empty); _fluentCommandLineParser.Setup(arg => arg.FullJson) .As("fj") .WithDescription( "When true, export all available data when using --json. Default is FALSE.") .SetDefault(false); _fluentCommandLineParser.Setup(arg => arg.Metrics) .As("met") .WithDescription( "When true, show metrics about processed event log. Default is TRUE.\r\n") .SetDefault(true); _fluentCommandLineParser.Setup(arg => arg.MapsDirectory) .As("maps") .WithDescription( "The path where event maps are located. Defaults to 'Maps' folder where program was executed\r\n ") .SetDefault(Path.Combine(BaseDirectory, "Maps")); _fluentCommandLineParser.Setup(arg => arg.Debug) .As("debug") .WithDescription("Show debug information during processing").SetDefault(false); _fluentCommandLineParser.Setup(arg => arg.Trace) .As("trace") .WithDescription("Show trace information during processing\r\n").SetDefault(false); var header = $"EvtxECmd version {Assembly.GetExecutingAssembly().GetName().Version}" + "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" + "\r\nhttps://github.com/EricZimmerman/evtx"; var footer = @"Examples: EvtxECmd.exe -f ""C:\Temp\Application.evtx"" --csv ""c:\temp\out"" --csvf MyOutputFile.csv" + "\r\n\t " + @" EvtxECmd.exe -f ""C:\Temp\Application.evtx"" --csv ""c:\temp\out""" + "\r\n\t " + @" EvtxECmd.exe -f ""C:\Temp\Application.evtx"" --json ""c:\temp\jsonout""" + "\r\n\t " + "\r\n\t" + " Short options (single letter) are prefixed with a single dash. Long commands are prefixed with two dashes\r\n"; _fluentCommandLineParser.SetupHelp("?", "help") .WithHeader(header) .Callback(text => _logger.Info(text + "\r\n" + footer)); var result = _fluentCommandLineParser.Parse(args); if (result.HelpCalled) { return; } if (result.HasErrors) { _logger.Error(""); _logger.Error(result.ErrorText); _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options); return; } if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() && _fluentCommandLineParser.Object.Directory.IsNullOrEmpty()) { _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options); _logger.Warn("-f or -d is required. Exiting"); return; } _logger.Info(header); _logger.Info(""); _logger.Info($"Command line: {string.Join(" ", Environment.GetCommandLineArgs().Skip(1))}\r\n"); if (IsAdministrator() == false) { _logger.Fatal("Warning: Administrator privileges not found!\r\n"); } if (_fluentCommandLineParser.Object.Debug) { LogManager.Configuration.LoggingRules.First().EnableLoggingForLevel(LogLevel.Debug); } if (_fluentCommandLineParser.Object.Trace) { LogManager.Configuration.LoggingRules.First().EnableLoggingForLevel(LogLevel.Trace); } LogManager.ReconfigExistingLoggers(); var sw = new Stopwatch(); sw.Start(); var ts = DateTimeOffset.UtcNow; _errorFiles = new Dictionary <string, int>(); if (_fluentCommandLineParser.Object.JsonDirectory.IsNullOrEmpty() == false) { if (Directory.Exists(_fluentCommandLineParser.Object.JsonDirectory) == false) { _logger.Warn( $"Path to '{_fluentCommandLineParser.Object.JsonDirectory}' doesn't exist. Creating..."); try { Directory.CreateDirectory(_fluentCommandLineParser.Object.JsonDirectory); } catch (Exception) { _logger.Fatal( $"Unable to create directory '{_fluentCommandLineParser.Object.JsonDirectory}'. Does a file with the same name exist? Exiting"); return; } } var outName = $"{ts:yyyyMMddHHmmss}_EvtxECmd_Output.json"; if (_fluentCommandLineParser.Object.JsonName.IsNullOrEmpty() == false) { outName = Path.GetFileName(_fluentCommandLineParser.Object.JsonName); } var outFile = Path.Combine(_fluentCommandLineParser.Object.JsonDirectory, outName); _logger.Warn($"json output will be saved to '{outFile}'\r\n"); try { _swJson = new StreamWriter(outFile, false, Encoding.UTF8); } catch (Exception) { _logger.Error($"Unable to open '{outFile}'! Is it in use? Exiting!\r\n"); Environment.Exit(0); } } if (_fluentCommandLineParser.Object.XmlDirectory.IsNullOrEmpty() == false) { if (Directory.Exists(_fluentCommandLineParser.Object.XmlDirectory) == false) { _logger.Warn( $"Path to '{_fluentCommandLineParser.Object.XmlDirectory}' doesn't exist. Creating..."); try { Directory.CreateDirectory(_fluentCommandLineParser.Object.XmlDirectory); } catch (Exception) { _logger.Fatal( $"Unable to create directory '{_fluentCommandLineParser.Object.XmlDirectory}'. Does a file with the same name exist? Exiting"); return; } } var outName = $"{ts:yyyyMMddHHmmss}_EvtxECmd_Output.xml"; if (_fluentCommandLineParser.Object.XmlName.IsNullOrEmpty() == false) { outName = Path.GetFileName(_fluentCommandLineParser.Object.XmlName); } var outFile = Path.Combine(_fluentCommandLineParser.Object.XmlDirectory, outName); _logger.Warn($"XML output will be saved to '{outFile}'\r\n"); try { _swXml = new StreamWriter(outFile, false, Encoding.UTF8); } catch (Exception) { _logger.Error($"Unable to open '{outFile}'! Is it in use? Exiting!\r\n"); Environment.Exit(0); } } if (_fluentCommandLineParser.Object.CsvDirectory.IsNullOrEmpty() == false) { if (Directory.Exists(_fluentCommandLineParser.Object.CsvDirectory) == false) { _logger.Warn( $"Path to '{_fluentCommandLineParser.Object.CsvDirectory}' doesn't exist. Creating..."); try { Directory.CreateDirectory(_fluentCommandLineParser.Object.CsvDirectory); } catch (Exception) { _logger.Fatal( $"Unable to create directory '{_fluentCommandLineParser.Object.CsvDirectory}'. Does a file with the same name exist? Exiting"); return; } } var outName = $"{ts:yyyyMMddHHmmss}_EvtxECmd_Output.csv"; if (_fluentCommandLineParser.Object.CsvName.IsNullOrEmpty() == false) { outName = Path.GetFileName(_fluentCommandLineParser.Object.CsvName); } var outFile = Path.Combine(_fluentCommandLineParser.Object.CsvDirectory, outName); _logger.Warn($"CSV output will be saved to '{outFile}'\r\n"); try { _swCsv = new StreamWriter(outFile, false, Encoding.UTF8); _csvWriter = new CsvWriter(_swCsv); } catch (Exception) { _logger.Error($"Unable to open '{outFile}'! Is it in use? Exiting!\r\n"); Environment.Exit(0); } var foo = _csvWriter.Configuration.AutoMap <EventRecord>(); foo.Map(t => t.PayloadXml).Ignore(); foo.Map(t => t.RecordPosition).Ignore(); foo.Map(t => t.Size).Ignore(); foo.Map(t => t.Timestamp).Ignore(); foo.Map(t => t.RecordNumber).Index(0); foo.Map(t => t.TimeCreated).Index(1); foo.Map(t => t.TimeCreated).ConvertUsing(t => $"{t.TimeCreated.ToString(_fluentCommandLineParser.Object.DateTimeFormat)}"); foo.Map(t => t.EventId).Index(2); foo.Map(t => t.Level).Index(3); foo.Map(t => t.Provider).Index(4); foo.Map(t => t.Channel).Index(5); foo.Map(t => t.ProcessId).Index(6); foo.Map(t => t.ThreadId).Index(7); foo.Map(t => t.Computer).Index(8); foo.Map(t => t.UserId).Index(9); foo.Map(t => t.MapDescription).Index(10); foo.Map(t => t.UserName).Index(11); foo.Map(t => t.RemoteHost).Index(12); foo.Map(t => t.PayloadData1).Index(13); foo.Map(t => t.PayloadData2).Index(14); foo.Map(t => t.PayloadData3).Index(15); foo.Map(t => t.PayloadData4).Index(16); foo.Map(t => t.PayloadData5).Index(17); foo.Map(t => t.PayloadData6).Index(18); foo.Map(t => t.ExecutableInfo).Index(19); foo.Map(t => t.SourceFile).Index(20); _csvWriter.Configuration.RegisterClassMap(foo); _csvWriter.WriteHeader <EventRecord>(); _csvWriter.NextRecord(); } if (Directory.Exists(_fluentCommandLineParser.Object.MapsDirectory) == false) { _logger.Warn( $"Maps directory '{_fluentCommandLineParser.Object.MapsDirectory}' does not exist! Event ID maps will not be loaded!!"); } else { _logger.Debug($"Loading maps from '{Path.GetFullPath(_fluentCommandLineParser.Object.MapsDirectory)}'"); var errors = EventLog.LoadMaps(Path.GetFullPath(_fluentCommandLineParser.Object.MapsDirectory)); if (errors) { return; } _logger.Info($"Maps loaded: {EventLog.EventLogMaps.Count:N0}"); } _includeIds = new HashSet <int>(); _excludeIds = new HashSet <int>(); if (_fluentCommandLineParser.Object.ExcludeIds.IsNullOrEmpty() == false) { var excSegs = _fluentCommandLineParser.Object.ExcludeIds.Split(','); foreach (var incSeg in excSegs) { if (int.TryParse(incSeg, out var goodId)) { _excludeIds.Add(goodId); } } } if (_fluentCommandLineParser.Object.IncludeIds.IsNullOrEmpty() == false) { _excludeIds.Clear(); var incSegs = _fluentCommandLineParser.Object.IncludeIds.Split(','); foreach (var incSeg in incSegs) { if (int.TryParse(incSeg, out var goodId)) { _includeIds.Add(goodId); } } } if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() == false) { if (File.Exists(_fluentCommandLineParser.Object.File) == false) { _logger.Warn($"'{_fluentCommandLineParser.Object.File}' does not exist! Exiting"); return; } ProcessFile(_fluentCommandLineParser.Object.File); } else { _logger.Info($"Looking for event log files in '{_fluentCommandLineParser.Object.Directory}'"); _logger.Info(""); var f = new DirectoryEnumerationFilters(); f.InclusionFilter = fsei => fsei.Extension.ToUpperInvariant() == ".EVTX"; f.RecursionFilter = entryInfo => !entryInfo.IsMountPoint && !entryInfo.IsSymbolicLink; f.ErrorFilter = (errorCode, errorMessage, pathProcessed) => true; var dirEnumOptions = DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive | DirectoryEnumerationOptions.SkipReparsePoints | DirectoryEnumerationOptions.ContinueOnException | DirectoryEnumerationOptions.BasicSearch; var files2 = Directory.EnumerateFileSystemEntries(Path.GetFullPath(_fluentCommandLineParser.Object.Directory), dirEnumOptions, f); foreach (var file in files2) { ProcessFile(file); } } _swCsv?.Flush(); _swCsv?.Close(); _swJson?.Flush(); _swJson?.Close(); _swXml?.Flush(); _swXml?.Close(); sw.Stop(); _logger.Info(""); var suff = string.Empty; if (_fileCount != 1) { suff = "s"; } _logger.Error( $"Processed {_fileCount:N0} file{suff} in {sw.Elapsed.TotalSeconds:N4} seconds\r\n"); if (_errorFiles.Count > 0) { _logger.Info(""); _logger.Error("Files with errors"); foreach (var errorFile in _errorFiles) { _logger.Info($"'{errorFile.Key}' error count: {errorFile.Value:N0}"); } _logger.Info(""); } }
public static bool FileExists(this string filename) { return(File.Exists(filename)); }
public FileStream OpenWrite() { return(File.OpenWrite(_path)); }
/// <summary> /// Documentation: https://developers.google.com/drive/v2/reference/files/insert /// </summary> /// <param name="_service"></param> /// <param name="_name"></param> /// <param name="_description"></param> /// <param name="_parent"></param> /// <returns></returns> public static Moss createDirectory(DriveService _service, string _name, string _description, string _parent) { if (_name == null || _parent == null) { log.Error("Exception[2.1]: Folder name or parent are null. " + _name + "| " + _parent); return new Moss(); } Pause(); log.Info("Create catalog: " + _name); File NewDirectory = null; Moss returnValue = new Moss(); Dictionary<string, string> dictionary = new Dictionary<string, string>(); File body = new File(); body.Name = _name; body.Description = _description; body.MimeType = "application/vnd.google-apps.folder"; body.Parents = new List<string> { _parent }; body.Properties = dictionary; var request = _service.Files.Create(body); request.Fields = "id, name, parents, md5Checksum, description, mimeType"; do { try { NewDirectory = request.Execute(); } catch (Exception x) { log.Error("Exception[2]: " + x.Message + " | " + x); Task.Delay(3000).Wait(); continue; } } while (NewDirectory == null); Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { ((MainWindow)Application.Current.MainWindow).lb_currUpload.Content = "Folder: " + @_name + " successful created"; })); if (NewDirectory.Id != null && NewDirectory.MimeType != null && NewDirectory.Name != null && NewDirectory.Parents[0] != null) { returnValue.Id = NewDirectory.Id; returnValue.mimeType = NewDirectory.MimeType; returnValue.Name = NewDirectory.Name; returnValue.parents = NewDirectory.Parents[0]; } return returnValue; }
/// <summary> /// Documentation: https://developers.google.com/drive/v3/reference/files/insert /// </summary> /// <param name="_service"></param> /// <param name="_uploadFile"></param> /// <param name="_parent"></param> /// <param name="Description"></param> /// <returns></returns> public static File uploadFile(DriveService _service, string _uploadFile, string _parent, string Description) { log.Warn("[UPLOAD]: " + _uploadFile + " To Parent: " + _parent); //return null; Pause(); log.Info("Upload file: " + _uploadFile); if (Alpha.File.Exists(_uploadFile)) { File body = new File(); string name = Alpha.Path.GetFileName(_uploadFile); FileName = name; _ChunkSize = 0; body.Name = name; body.MimeType = GetMimeType(_uploadFile); body.Parents = new List<string> { _parent }; fileSize = Alpha.File.GetSize(_uploadFile); using (var stream = Alpha.File.OpenRead(_uploadFile)) { var request = _service.Files.Create(body, stream, body.MimeType); try { request.ChunkSize = 1048576; _ChunkSize = 1048576; request.ProgressChanged += Upload_ProgressChanged; var _watch = Stopwatch.StartNew(); request.Upload(); _watch.Stop(); log.Warn("[" + FileName + "]" + " | File Size: " + fileSize + "| Send Time: [" + _watch.ElapsedMilliseconds + "]"); while (request.ContentStream.Position != request.ContentStream.Length) { Pause(); log.Error("Network error. Resume attempt."); for (int i = 15; i > 0; i = i - 1) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { ((MainWindow)Application.Current.MainWindow).lb_currUpload.Content = "Network error. Resume attempt in: " + i; })); Thread.Sleep(1000); } request.Resume(); } } catch (Exception x) { log.Error("Exception[5]: " + x.Message + " | " + x); return null; } return request.ResponseBody; } } else { log.Error("File not found: " + _uploadFile); return null; } }