本文整理汇总了C#中FileInfo.Create方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfo.Create方法的具体用法?C# FileInfo.Create怎么用?C# FileInfo.Create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileInfo
的用法示例。
在下文中一共展示了FileInfo.Create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateFileWithBytes
/**
* path:文件创建目录
* info:写入的内容
*/
public static void CreateFileWithBytes(string path, byte[] info, int length)
{
//文件流信息
Stream sw;
FileInfo t = new FileInfo(path);
CreateDirectory(System.IO.Path.GetDirectoryName(path));
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.Create();
}
else
{
//如果此文件存在则删除
File.Delete(path);
sw = t.Create();
}
//以行的形式写入信息
//sw.WriteLine(info);
sw.Write(info, 0, length);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
}
示例2: CopyFile
void CopyFile(string src_path, string tar_oath){
string path = src_path;
string path2 = tar_oath;
FileInfo fi1 = new FileInfo(path);
FileInfo fi2 = new FileInfo(path2);
try
{
// Create the file and clean up handles.
using (FileStream fs = fi1.Create()) {}
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(path2);
//Try to copy it again, which should succeed.
fi1.CopyTo(path2, true);
}
catch
{
}
}
示例3: SettingUpdatesProperties
public void SettingUpdatesProperties()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.All(TimeFunctions(requiresRoundtripping: true), (tuple) =>
{
DateTime dt = new DateTime(2014, 12, 1, 12, 0, 0, tuple.Item3);
tuple.Item1(testFile.FullName, dt);
var result = tuple.Item2(testFile.FullName);
Assert.Equal(dt, result);
Assert.Equal(dt.ToLocalTime(), result.ToLocalTime());
// File and Directory UTC APIs treat a DateTimeKind.Unspecified as UTC whereas
// ToUniversalTime treats it as local.
if (tuple.Item3 == DateTimeKind.Unspecified)
{
Assert.Equal(dt, result.ToUniversalTime());
}
else
{
Assert.Equal(dt.ToUniversalTime(), result.ToUniversalTime());
}
});
}
示例4: CopyTo
/// <summary>A DirectoryInfo extension method that copies to.</summary>
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
/// <param name="obj">The obj to act on.</param>
/// <param name="destDirName">Pathname of the destination directory.</param>
/// <param name="searchPattern">A pattern specifying the search.</param>
/// <param name="searchOption">The search option.</param>
public static void CopyTo(this DirectoryInfo obj, string destDirName, string searchPattern, SearchOption searchOption)
{
var files = obj.GetFiles(searchPattern, searchOption);
foreach (var file in files)
{
var outputFile = destDirName + file.FullName.Substring(obj.FullName.Length);
var directory = new FileInfo(outputFile).Directory;
if (directory == null)
{
throw new Exception("The directory cannot be null.");
}
if (!directory.Exists)
{
directory.Create();
}
file.CopyTo(outputFile);
}
// Ensure empty dir are also copied
var directories = obj.GetDirectories(searchPattern, searchOption);
foreach (var directory in directories)
{
var outputDirectory = destDirName + directory.FullName.Substring(obj.FullName.Length);
var directoryInfo = new DirectoryInfo(outputDirectory);
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
}
}
示例5: Main
public static void Main(string[] args)
{
FileInfo file = new FileInfo(@"..\..\CopyDelete.cs");
string fullPath =file.FullName;
DirectoryInfo dir = new DirectoryInfo(@"..\..\");
dir.CreateSubdirectory("backup");
DirectoryInfo backupDir = new DirectoryInfo(@"..\..\backup");
foreach (FileInfo subfile in backupDir.GetFiles())
{
subfile.Delete();
}
file.CopyTo(@"..\..\backup\CopyDelete_Backup.cs");
DirectoryInfo tempDir = new DirectoryInfo(@"..\..\temp");
tempDir.Create();
FileInfo tempFile = new FileInfo(@"..\..\temp\temp.txt");
FileStream fs = tempFile.Create();
fs.Close();
tempFile.Delete();
tempDir.Delete(true);
Console.WriteLine("All done.");
Console.ReadLine();
}
示例6: NonExistentPath
public virtual void NonExistentPath()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.Throws<FileNotFoundException>(() => Move(GetTestFilePath(), testFile.FullName));
Assert.Throws<DirectoryNotFoundException>(() => Move(testFile.FullName, Path.Combine(TestDirectory, GetTestFileName(), GetTestFileName())));
Assert.Throws<FileNotFoundException>(() => Move(Path.Combine(TestDirectory, GetTestFileName(), GetTestFileName()), testFile.FullName));
}
示例7: PathWithIllegalCharacters
public void PathWithIllegalCharacters()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.All(IOInputs.GetPathsWithInvalidCharacters(), (invalid) =>
{
Assert.Throws<ArgumentException>(() => Move(testFile.FullName, invalid));
});
}
示例8: DeleteThenRefresh
public void DeleteThenRefresh()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.True(testFile.Exists);
testFile.Delete();
testFile.Refresh();
Assert.False(testFile.Exists);
}
示例9: UnixAttributeSetting
public void UnixAttributeSetting(FileAttributes attr)
{
var test = new FileInfo(GetTestFilePath());
test.Create().Dispose();
Set(test.FullName, attr);
test.Refresh();
Assert.Equal(attr, Get(test.FullName));
Set(test.FullName, 0);
}
示例10: ValidPathExists_ReturnsTrue
public void ValidPathExists_ReturnsTrue()
{
Assert.All((IOInputs.GetValidPathComponentNames()), (component) =>
{
string path = Path.Combine(TestDirectory, component);
FileInfo testFile = new FileInfo(path);
testFile.Create().Dispose();
Assert.True(Exists(path));
});
}
示例11: BasicMove
public void BasicMove()
{
FileInfo testFileSource = new FileInfo(GetTestFilePath());
testFileSource.Create().Dispose();
string testFileDest = GetTestFilePath();
Move(testFileSource.FullName, testFileDest);
Assert.True(File.Exists(testFileDest));
Assert.False(File.Exists(testFileSource.FullName));
}
示例12: NameChange
public void NameChange()
{
string source = GetTestFilePath();
string dest = GetTestFilePath();
FileInfo testFile = new FileInfo(source);
testFile.Create().Dispose();
Assert.Equal(source, testFile.FullName);
testFile.MoveTo(dest);
testFile.Refresh();
Assert.Equal(dest, testFile.FullName);
}
示例13: FullNameUpdatesOnCreate
public void FullNameUpdatesOnCreate()
{
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string testFile = Path.Combine(testDir.FullName, GetTestFileName());
FileInfo info = new FileInfo(testFile);
using (FileStream stream = info.Create())
{
Assert.True(File.Exists(testFile));
Assert.Equal(testFile, info.FullName);
}
}
示例14: SettingUpdatesProperties
public void SettingUpdatesProperties()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.All(TimeFunctions(), (tuple) =>
{
DateTime dt = new DateTime(2014, 12, 1, 12, 0, 0, tuple.Item3);
tuple.Item1(testFile.FullName, dt);
Assert.Equal(dt, tuple.Item2(testFile.FullName));
});
}
示例15: AttributeChange
public void AttributeChange()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.True((testFile.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly);
testFile.Attributes = FileAttributes.ReadOnly;
testFile.Refresh();
Assert.True((testFile.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
testFile.Attributes = new FileAttributes();
testFile.Refresh();
Assert.True((testFile.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly);
}