C# FileInfo.Open的代码示例
通过代码示例来学习C# FileInfo.Open方法
通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。
FileInfo.Open是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的FileInfo.Open() 已经帮大家高亮显示了,大家可以重点学习FileInfo.Open() 方法的写法,从而快速掌握该方法的应用。
FileInfo.Open的代码示例1 - ServiceLogContainsUpgradeMessaging()
using System.IO; private bool ServiceLogContainsUpgradeMessaging() { // This test checks for the upgrade timer start message in the Service log // file. GVFS.Service should schedule the timer as it starts. string expectedTimerMessage = "Checking for product upgrades. (Start)"; string serviceLogFolder = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "GVFS", GVFSServiceProcess.TestServiceName, "Logs"); DirectoryInfo logsDirectory = new DirectoryInfo(serviceLogFolder); FileInfo logFile = logsDirectory.GetFiles() .OrderByDescending(f => f.LastWriteTime) .FirstOrDefault(); if (logFile != null) { using (StreamReader fileStream = new StreamReader(File.Open(logFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { string nextLine = null; while ((nextLine = fileStream.ReadLine()) != null) { if (nextLine.Contains(expectedTimerMessage)) { return true; } } } } return false; }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:34,代码来源:GVFSUpgradeReminderTests.cs
在ServiceLogContainsUpgradeMessaging()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了2次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Open的代码示例2 - GitRequestsReplacementForTruncatedObject()
using System.IO; [TestCase] public void GitRequestsReplacementForTruncatedObject() { Action
truncateObject = (string objectPath) => { FileInfo objectFileInfo = new FileInfo(objectPath); using (FileStream objectStream = new FileStream(objectPath, FileMode.Open)) { objectStream.SetLength(objectFileInfo.Length - 8); } }; this.RunGitDiffWithCorruptObject(truncateObject); // TODO 1114508: Update git cat-file to request object from GVFS when it finds a truncated object on disk. ////this.RunGitCatFileWithCorruptObject(truncateObject); this.RunGitResetHardWithCorruptObject(truncateObject); this.RunGitCheckoutOnFileWithCorruptObject(truncateObject); }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:22,代码来源:GitCorruptObjectTests.cs
在GitRequestsReplacementForTruncatedObject()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Open的代码示例3 - TruncatedObjectRedownloaded()
using System.IO; [TestCase, Order(17)] public void TruncatedObjectRedownloaded() { GitProcess.InvokeProcess(this.Enlistment.RepoRoot, "checkout " + this.Enlistment.Commitish); ProcessResult revParseResult = GitProcess.InvokeProcess(this.Enlistment.RepoRoot, "rev-parse :Test_EPF_WorkingDirectoryTests/TruncatedObjectRedownloaded.txt"); string sha = revParseResult.Output.Trim(); sha.Length.ShouldEqual(40); string objectPath = Path.Combine(this.Enlistment.GetObjectRoot(this.fileSystem), sha.Substring(0, 2), sha.Substring(2, 38)); objectPath.ShouldNotExistOnDisk(this.fileSystem); string corruptObjectFolderPath = Path.Combine(this.Enlistment.DotGVFSRoot, "CorruptObjects"); int initialCorruptObjectCount = 0; if (this.fileSystem.DirectoryExists(corruptObjectFolderPath)) { initialCorruptObjectCount = new DirectoryInfo(corruptObjectFolderPath).EnumerateFileSystemInfos().Count(); } // Read a copy of TruncatedObjectRedownloaded.txt to force the object to be downloaded GitProcess.InvokeProcess(this.Enlistment.RepoRoot, "rev-parse :Test_EPF_WorkingDirectoryTests/TruncatedObjectRedownloaded_copy.txt").Output.Trim().ShouldEqual(sha); string testFileContents = this.Enlistment.GetVirtualPathTo("Test_EPF_WorkingDirectoryTests", "TruncatedObjectRedownloaded_copy.txt").ShouldBeAFile(this.fileSystem).WithContents(); objectPath.ShouldBeAFile(this.fileSystem); string modifedFile = "Test_EPF_WorkingDirectoryTests/TruncatedObjectRedownloaded.txt"; GVFSHelpers.ModifiedPathsShouldNotContain(this.Enlistment, this.fileSystem, modifedFile); // Truncate the contents of objectPath string tempTruncatedObjectPath = objectPath + "truncated"; FileInfo objectFileInfo = new FileInfo(objectPath); long objectLength = objectFileInfo.Length; using (FileStream objectStream = new FileStream(objectPath, FileMode.Open)) using (FileStream truncatedObjectStream = new FileStream(tempTruncatedObjectPath, FileMode.CreateNew)) { for (int i = 0; i < (objectStream.Length - 16); ++i) { truncatedObjectStream.WriteByte((byte)objectStream.ReadByte()); } } this.fileSystem.DeleteFile(objectPath); this.fileSystem.MoveFile(tempTruncatedObjectPath, objectPath); tempTruncatedObjectPath.ShouldNotExistOnDisk(this.fileSystem); objectPath.ShouldBeAFile(this.fileSystem); new FileInfo(objectPath).Length.ShouldEqual(objectLength - 16); // Windows should correct a corrupt obect // Read the original path and verify its contents are correct this.Enlistment.GetVirtualPathTo("Test_EPF_WorkingDirectoryTests", "TruncatedObjectRedownloaded.txt").ShouldBeAFile(this.fileSystem).WithContents(testFileContents); // Confirm there's a new item in the corrupt objects folder corruptObjectFolderPath.ShouldBeADirectory(this.fileSystem).WithItems().Count().ShouldEqual(initialCorruptObjectCount + 1); // File should not be in ModifiedPaths.dat GVFSHelpers.ModifiedPathsShouldNotContain(this.Enlistment, this.fileSystem, "Test_EPF_WorkingDirectoryTests/TruncatedObjectRedownloaded.txt"); }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:55,代码来源:WorkingDirectoryTests.cs
在TruncatedObjectRedownloaded()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Open的代码示例4 - YaraScanFile()
using System.IO; public static List
YaraScanFile(string fileName, bool verbose) { List beaconScanMatches = new List (); using (var ctx = new YaraContext()) { Rules rules = null; try { using (Compiler compiler = new Compiler()) { // Retrieve YARA rules from YaraRules static class and compile them for scanning foreach (string rule in YaraRules.meterpreterRules) { compiler.AddRuleString(rule); } compiler.AddRuleString(YaraRules.cobaltStrikeRule); rules = compiler.GetRules(); } // Scanner and ScanResults do not need to be disposed. var scanner = new Scanner(); List results = new List (); // If file size > 2GB, stream the file and use ScanMemory() on file chunks rather than reading the whole file via if (new FileInfo(fileName).Length < Int32.MaxValue) { results.AddRange(scanner.ScanFile(fileName, rules)); } else { using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { // Parse the file in 200MB chunks int chunkSize = 1024 * 1024 * 200; byte[] chunk = new byte[chunkSize]; int bytesRead = 0; long bytesToRead = fileStream.Length; while (bytesToRead != 0) { int n = fileStream.Read(chunk, 0, chunkSize); if (n == 0) { break; } // Yara scan the file chunk and add any results to the list var scanResults = scanner.ScanMemory(chunk, rules); // Because the file is being scanned in chunks, match offsets are based on the start of the chunk. Need to add // previous bytes read to the current match offsets if (scanResults.Count > 0) { foreach (ScanResult result in scanResults) { if (result.MatchingRule.Identifier.Contains("CobaltStrike")) { foreach (string key in result.Matches.Keys) { result.Matches[key][0].Offset += (ulong)bytesRead; } results.Add(result); } } } bytesRead += n; bytesToRead -= n; // Shitty progress update if (verbose && bytesRead > 0 && bytesRead <= fileStream.Length) Console.Write($"\r\tScanned {bytesRead} bytes of {fileStream.Length} byte file..."); } } if (verbose) Console.WriteLine($"\r\tFinished scanning file: {fileName}\t\t\t"); } beaconScanMatches = ParseScanResults(results); } finally { // Rules and Compiler objects must be disposed. if (rules != null) rules.Dispose(); } return beaconScanMatches; } }
开发者ID:Apr4h,项目名称:CobaltStrikeScan,代码行数:98,代码来源:CobaltStrikeScan.cs
在YaraScanFile()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Open的代码示例5 - GetBeaconsFromFile()
using System.IO; ///
/// Get Cobalt Strike Configurations from a file and output their contents to the console . /// /// Name of memory/dump file to be scanned for Cobalt Strike beacons private static void GetBeaconsFromFile(string fileName) { OutputMessageToConsole(LogLevel.Info, $"Scanning file: {fileName}"); Listbeacons = new List (); if (File.Exists(fileName)) { // Check the size of the file. If > 500MB, and notify that scanning will take time var fileSize = new FileInfo(fileName).Length; if (fileSize > Int32.MaxValue) { OutputMessageToConsole(LogLevel.Warn, $"\tFile is large: {fileSize / (1024 * 1024)} MB. Scanning will be slow."); } // Yara scan the file and return any matches List beaconMatches = CobaltStrikeScan.YaraScanFile(fileName, opts.Verbose); // Extract config bytes at each match offset and parse the beacon config from them if (beaconMatches.Count > 0) { if (opts.Verbose) { OutputMessageToConsole(LogLevel.Info, $"\t{beaconMatches.Count} Signature(s) detected in file. Attempting to extract and parse config...\n"); } foreach (BeaconMatch match in beaconMatches) { try { byte[] beaconConfigBytes = new byte[4096]; // Get a byte array from the offset of the file with beacon matches to avoid cases // where the file is too big to read in to a File object using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { fileStream.Seek((long)match.Offset, SeekOrigin.Begin); fileStream.Read(beaconConfigBytes, 0, 4096); } match.Offset = 0; beacons.Add(new Beacon(beaconConfigBytes, match.Offset, match.Version)); } catch (System.IO.IOException) { /* if (opts.Verbose) { OutputMessageToConsole(LogLevel.Error, $"Error extracting signatured data from file at offset: {match.Offset}"); } */ } } if (beacons.Count > 0) { foreach (Beacon beacon in beacons) { if (beacon.isValidBeacon()) { OutputMessageToConsole(LogLevel.Success, $"Cobalt Strike Beacon Configuration\n"); beacon.OutputToConsole(); } } } else OutputBeaconNotFoundMessage(); } else { OutputBeaconNotFoundMessage(); } } else { OutputMessageToConsole(LogLevel.Error, $"File doesn't exist: {fileName}\nExiting..."); System.Environment.Exit(1); } }
开发者ID:Apr4h,项目名称:CobaltStrikeScan,代码行数:78,代码来源:ConsoleUI.cs
在GetBeaconsFromFile()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Open的代码示例6 - Send()
using System.IO; ///
/// Sends the specified file using the WebSocket connection. /// /// ////// A ///that specifies the file to send. /// /// The file is sent as the binary data. /// /// ////// The current state of the connection is not Open. /// ////// ///is . /// /// public void Send(FileInfo fileInfo) { if (_readyState != WebSocketState.Open) { var msg = "The current state of the connection is not Open."; throw new InvalidOperationException(msg); } if (fileInfo == null) throw new ArgumentNullException("fileInfo"); if (!fileInfo.Exists) { var msg = "The file does not exist."; throw new ArgumentException(msg, "fileInfo"); } FileStream stream; if (!fileInfo.TryOpenRead(out stream)) { var msg = "The file could not be opened."; throw new ArgumentException(msg, "fileInfo"); } send(Opcode.Binary, stream); }/// The file does not exist. /// ////// -or- /// ////// The file could not be opened. /// ///
开发者ID:ntminer,项目名称:NtMiner,代码行数:52,代码来源:WebSocket.cs
在Send()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Open的代码示例7 - IsFileDifferent()
using System.IO; ///
/// Check if a file is different than the stream content. Content is compared bit per bit. /// /// file to compare /// stream to compare ///true=different, false=same public static bool IsFileDifferent(FileInfo file, Stream stream) { if (!file.Exists) return true; // Note: Using same buffer size than AreStreamsEqual for better efficiency using (var fstream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, _FileStreamBufferSize)) { return !AreStreamsEqual(stream, fstream); } }
开发者ID:ubisoft,项目名称:Sharpmake,代码行数:19,代码来源:Util.cs
在IsFileDifferent()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Open的代码示例8 - OpenWorkbook()
using System.IO; #endregion #region " OpenWorkbookFromStream " private void OpenWorkbook(string filename) { FileInfo info = new FileInfo(filename); if (info.Exists == false) throw new FileNotFoundException(string.Concat("Excel File '", filename, "' not found."), filename); using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read)) { OpenWorkbookFromStream(file); } }
开发者ID:MarcosMeli,项目名称:FileHelpers,代码行数:17,代码来源:ExcelNPOIStorage.cs
在OpenWorkbook()方法中,FileInfo的代码示例类中的Open的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
本文中的FileInfo.Open方法示例由csref.cn整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。