C# FileInfo.Create的代码示例
通过代码示例来学习C# FileInfo.Create方法
通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。
FileInfo.Create是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的FileInfo.Create() 已经帮大家高亮显示了,大家可以重点学习FileInfo.Create() 方法的写法,从而快速掌握该方法的应用。
FileInfo.Create的代码示例1 - NewFileAttributesAreUpdated()
using System.IO; [TestCaseSource(typeof(FileRunnersAndFolders), nameof(FileRunnersAndFolders.Folders))] public void NewFileAttributesAreUpdated(string parentFolder) { string filename = Path.Combine(parentFolder, "FileAttributesAreUpdated"); FileSystemRunner fileSystem = FileSystemRunner.DefaultRunner; string virtualFile = this.Enlistment.GetVirtualPathTo(filename); virtualFile.ShouldNotExistOnDisk(fileSystem); File.Create(virtualFile).Dispose(); virtualFile.ShouldBeAFile(fileSystem); // Update defaults. FileInfo is not batched, so each of these will create a separate Open-Update-Close set. FileInfo before = new FileInfo(virtualFile); DateTime testValue = DateTime.Now + TimeSpan.FromDays(1); before.CreationTime = testValue; before.LastAccessTime = testValue; before.LastWriteTime = testValue; before.Attributes = FileAttributes.Hidden; // FileInfo caches information. We can refresh, but just to be absolutely sure... virtualFile.ShouldBeAFile(fileSystem).WithInfo(testValue, testValue, testValue, FileAttributes.Hidden); File.Delete(virtualFile); virtualFile.ShouldNotExistOnDisk(fileSystem); }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:28,代码来源:BasicFileSystemTests.cs
在NewFileAttributesAreUpdated()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Create的代码示例2 - CreateGitScript()
using System.IO; // TODO(#1364): Don't call this method on POSIX platforms (or have it no-op on them) private void CreateGitScript(GVFSEnlistment enlistment) { FileInfo gitCmd = new FileInfo(Path.Combine(enlistment.EnlistmentRoot, "git.cmd")); using (FileStream fs = gitCmd.Create()) using (StreamWriter writer = new StreamWriter(fs)) { writer.Write( @" @echo OFF echo . echo ^[105;30m echo This repo was cloned using GVFS, and the git repo is in the 'src' directory echo Switching you to the 'src' directory and rerunning your git command echo [0m @echo ON cd src git %* "); } gitCmd.Attributes = FileAttributes.Hidden; }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:26,代码来源:CloneVerb.cs
在CreateGitScript()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Create的代码示例3 - FileCreateTest()
using System.IO; [TestMethod] public void FileCreateTest() { string fileFullName = Path.Combine(HomePath.AppDomainBaseDirectory, "temp.txt"); FileInfo file = new FileInfo(fileFullName); // 1. 创建之后如果不使用必须Close; var fs = file.Create(); var buffer = Encoding.UTF8.GetBytes("test"); fs.Write(buffer, 0, buffer.Length); fs.Close(); file = new FileInfo(fileFullName); // 2.文件已存在时调用Create没有异常; fs = file.Create(); byte[] data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); fs.Close(); // 3. 旧文件被覆盖 Assert.AreNotEqual("test", Encoding.UTF8.GetString(data)); }
开发者ID:ntminer,项目名称:NtMiner,代码行数:21,代码来源:IoTests.cs
在FileCreateTest()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了2次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Create的代码示例4 - FileWriteIfDifferentInternal()
using System.IO; internal static bool FileWriteIfDifferentInternal(FileInfo file, MemoryStream stream, bool bypassAutoCleanupDatabase = false) { if (!bypassAutoCleanupDatabase) RecordInAutoCleanupDatabase(file.FullName); if (file.Exists) { if (!IsFileDifferent(file, stream)) return false; if (file.IsReadOnly) file.IsReadOnly = false; } else { // make sure target directory exist if (!file.Directory.Exists) file.Directory.Create(); } // write the file using (FileStream outStream = file.Open(FileMode.Create)) { stream.WriteTo(outStream); } return true; }
开发者ID:ubisoft,项目名称:Sharpmake,代码行数:30,代码来源:Util.cs
在FileWriteIfDifferentInternal()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了2次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Create的代码示例5 - CompressFiles()
using System.IO; #endregion #region Compression task helper methods //===================================================================== ///
/// This is used to handle file compression as a background task /// private void CompressFiles() { int addCount = 0, baseFolderLength = this.WorkingFolder.Length; if(this.WorkingFolder[baseFolderLength - 1] != '\\') baseFolderLength++; fileCount = Directory.EnumerateFiles(this.WorkingFolder, "*", SearchOption.AllDirectories).Count(); using(var archive = ZipFile.Open(archiveName, ZipArchiveMode.Create)) { // Compress the entire working folder. Files are stored relative to the root. We'll handle // enumerating the files so that we can report progress. foreach(var file in Directory.EnumerateFiles(this.WorkingFolder, "*", SearchOption.AllDirectories)) { string entryName = file.Substring(baseFolderLength); if(Path.DirectorySeparatorChar == '\\') entryName = entryName.Replace('\\', '/'); archive.CreateEntryFromFile(file, entryName, CompressionLevel.Optimal); var fi = new FileInfo(file); folders.Add(fi.DirectoryName); addCount++; uncompressedSize += fi.Length; if((addCount % 500) == 0) progressProvider.Report(addCount); } } }
开发者ID:EWSoftware,项目名称:SHFB,代码行数:42,代码来源:BuildHelpViewerFile.cs
在CompressFiles()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Create的代码示例6 - CompressFiles()
using System.IO; ///
/// This is used to handle file compression as a background task /// private void CompressFiles() { int addCount = 0, baseFolderLength = workingFolder.Length; if(workingFolder[baseFolderLength - 1] != '\\') baseFolderLength++; fileCount = archiveFiles.Count; using(var archive = ZipFile.Open(archiveName, ZipArchiveMode.Create)) { // Compress just the files needed for the document. Files are stored relative to the root. // We'll handle enumerating the files so that we can report progress. foreach(var file in archiveFiles) { string entryName = file.Substring(baseFolderLength); if(Path.DirectorySeparatorChar == '\\') entryName = entryName.Replace('\\', '/'); archive.CreateEntryFromFile(file, entryName, CompressionLevel.Optimal); var fi = new FileInfo(file); addCount++; uncompressedSize += fi.Length; if((addCount % 500) == 0) progressProvider.Report(addCount); } } }
开发者ID:EWSoftware,项目名称:SHFB,代码行数:37,代码来源:OpenXmlFileGenerator.cs
在CompressFiles()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Create的代码示例7 - RegisterNativeBinary()
using System.IO; ///
/// Registers any embedded native (unmanaged) binaries required by ImageProcessor. /// /// The name of the native binary. /// The resource bytes containing the native binary. ////// Thrown if the binary cannot be registered. /// public void RegisterNativeBinary(string name, byte[] resourceBytes) { nativeBinaries.GetOrAdd( name, b => { IntPtr pointer; string folder = this.Is64BitEnvironment ? "x64" : "x86"; var assembly = Assembly.GetExecutingAssembly(); string targetBasePath = new Uri(assembly.Location).LocalPath; string targetPath = Path.GetFullPath(Path.Combine(targetBasePath, "..\\" + folder + "\\" + b)); // Copy the file across if necessary. var fileInfo = new FileInfo(targetPath); bool rewrite = true; if (fileInfo.Exists) { byte[] existing = File.ReadAllBytes(targetPath); if (resourceBytes.SequenceEqual(existing)) { rewrite = false; } } if (rewrite) { // ReSharper disable once AssignNullToNotNullAttribute var directoryInfo = new DirectoryInfo(Path.GetDirectoryName(targetPath)); if (!directoryInfo.Exists) { directoryInfo.Create(); } File.WriteAllBytes(targetPath, resourceBytes); } try { // Load the binary into memory. pointer = NativeMethods.LoadLibrary(targetPath); } catch (Exception ex) { throw new ApplicationException(ex.Message); } if (pointer == IntPtr.Zero) { throw new ApplicationException("Cannot load " + b); } return pointer; }); }
开发者ID:JimBobSquarePants,项目名称:ImageProcessor,代码行数:65,代码来源:NativeBinaryFactory.cs
在RegisterNativeBinary()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileInfo.Create的代码示例8 - TestMain()
using System.IO; [Test] public void TestMain() { FileInfo file = TempFile.CreateTempFile("HexDump", ".dat"); try { FileStream out1 = new FileStream(file.FullName, FileMode.Create, FileAccess.ReadWrite); try { IOUtils.Copy(new MemoryStream(Encoding.UTF8.GetBytes("teststring")), out1); } finally { out1.Close(); } Assert.IsTrue(file.Exists); Assert.IsTrue(file.Length > 0); //HexDump.Main(new String[] { file.AbsolutePath }); } finally { file.Delete(); //Assert.IsTrue(file.Exists); } }
开发者ID:dotnetcore,项目名称:NPOI,代码行数:29,代码来源:TestHexDump.cs
在TestMain()方法中,FileInfo的代码示例类中的Create的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
本文中的FileInfo.Create方法示例由csref.cn整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。