本文整理汇总了C#中IFileInfo.ThrowIfNull方法的典型用法代码示例。如果您正苦于以下问题:C# IFileInfo.ThrowIfNull方法的具体用法?C# IFileInfo.ThrowIfNull怎么用?C# IFileInfo.ThrowIfNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFileInfo
的用法示例。
在下文中一共展示了IFileInfo.ThrowIfNull方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteFile
/// <summary>
/// Deletes the specified file.
/// </summary>
/// <param name="file">The file to delete.</param>
/// <exception cref="AccessException">The file could not be accessed.</exception>
public void DeleteFile(IFileInfo file)
{
file.ThrowIfNull(() => file);
if (!(file is LocalFileInfo))
throw new ArgumentException("The file must be of type LocalFileInfo.", "file");
try
{
File.SetAttributes(file.FullName, FileAttributes.Normal);
File.Delete(file.FullName);
}
catch (IOException ex)
{
throw new AccessException("The file could not be accessed.", ex);
}
catch (SecurityException ex)
{
throw new AccessException("The file could not be accessed.", ex);
}
catch (UnauthorizedAccessException ex)
{
throw new AccessException("The file could not be accessed.", ex);
}
}
示例2: FileCopyErrorEventArgs
/// <summary>
/// Initializes a new instance of the <see cref="FileCopyErrorEventArgs"/> class.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="targetDirectory">The target directory.</param>
public FileCopyErrorEventArgs(IFileInfo file, IDirectoryInfo targetDirectory)
{
file.ThrowIfNull(() => file);
targetDirectory.ThrowIfNull(() => targetDirectory);
this.File = file;
this.TargetDirectory = targetDirectory;
}
示例3: FileCopyEventArgs
/// <summary>
/// Initializes a new instance of the <see cref="FileCopyEventArgs"/> class.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="sourceDirectory">The source directory.</param>
/// <param name="targetDirectory">The target directory.</param>
public FileCopyEventArgs(IFileInfo file, IDirectoryInfo sourceDirectory, IDirectoryInfo targetDirectory)
{
file.ThrowIfNull(() => file);
sourceDirectory.ThrowIfNull(() => sourceDirectory);
targetDirectory.ThrowIfNull(() => targetDirectory);
this.File = file;
this.SourceDirectory = sourceDirectory;
this.TargetDirectory = targetDirectory;
}
示例4: CopyFile
/// <summary>
/// Copies the specified file to the target directory.
/// </summary>
/// <param name="sourceFileSystem">The source file system.</param>
/// <param name="sourceFile">The source file.</param>
/// <param name="targetDirectory">The target directory.</param>
/// <exception cref="AccessException">The source file or target directory could not be accessed.</exception>
public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory)
{
sourceFileSystem.ThrowIfNull(() => sourceFileSystem);
sourceFile.ThrowIfNull(() => sourceFile);
targetDirectory.ThrowIfNull(() => targetDirectory);
if (!(targetDirectory is LocalDirectoryInfo))
throw new ArgumentException("The target directory must be of type LocalDirectoryInfo.", "targetDirectory");
try
{
using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile))
{
string targetFilePath = this.CombinePath(targetDirectory.FullName, sourceFile.Name);
try
{
using (FileStream targetStream = File.Create(targetFilePath))
{
if (sourceFile.Length > 0)
{
var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 256 * 1024, true);
copyOperation.CopyProgressChanged +=
(sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e);
copyOperation.Execute();
}
}
}
catch (IOException)
{
File.Delete(targetFilePath);
throw;
}
}
}
catch (UnauthorizedAccessException ex)
{
throw new AccessException("The file could not be accessed.", ex);
}
catch (SecurityException ex)
{
throw new AccessException("The file could not be accessed.", ex);
}
catch (IOException ex)
{
throw new AccessException("The file could not be accessed.", ex);
}
}
示例5: CopyFile
/// <summary>
/// Copies the specified file to the target directory.
/// </summary>
/// <param name="sourceFileSystem">The source file system.</param>
/// <param name="sourceFile">The source file.</param>
/// <param name="targetDirectory">The target directory.</param>
/// <exception cref="AccessException">The source file or target directory could not be accessed.</exception>
/// /// <exception cref="ArgumentException">The target directory is not of type <see cref="FlagFtp.FtpDirectoryInfo"/>.</exception>
public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory)
{
sourceFileSystem.ThrowIfNull(() => sourceFileSystem);
sourceFile.ThrowIfNull(() => sourceFile);
targetDirectory.ThrowIfNull(() => targetDirectory);
if (!(targetDirectory is FtpDirectoryInfo))
throw new ArgumentException("The target directory must be of type FtpDirectoryInfo.", "targetDirectory");
Uri targetFilePath = new Uri(this.CombinePath(targetDirectory.FullName, sourceFile.Name));
try
{
using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile))
{
using (Stream targetStream = this.client.OpenWrite(targetFilePath))
{
if (sourceFile.Length > 0)
{
var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 8 * 1024, true);
copyOperation.CopyProgressChanged +=
(sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e);
copyOperation.Execute();
}
}
}
}
catch (WebException ex)
{
switch (ex.Status)
{
case WebExceptionStatus.ConnectFailure:
case WebExceptionStatus.ProxyNameResolutionFailure:
throw new FileSystemUnavailableException("The FTP server is currently unavailable.", ex);
}
throw new AccessException("The file could not be accessed", ex);
}
}
示例6: DeleteFile
/// <summary>
/// Deletes the specified file.
/// </summary>
/// <param name="file">The file to delete.</param>
/// <exception cref="AccessException">The file could not be accessed.</exception>
/// <exception cref="ArgumentException">The file is not of type <see cref="FlagFtp.FtpFileInfo"/>.</exception>
/// <exception cref="FileSystemUnavailableException">The file system is currently unavailable.</exception>
public void DeleteFile(IFileInfo file)
{
file.ThrowIfNull(() => file);
if (!(file is FtpFileInfo))
throw new ArgumentException("The file must be of type FtpFileInfo.", "file");
try
{
this.client.DeleteFile(new Uri(file.FullName));
}
catch (WebException ex)
{
switch (ex.Status)
{
case WebExceptionStatus.ConnectFailure:
case WebExceptionStatus.ProxyNameResolutionFailure:
throw new FileSystemUnavailableException("The FTP server is currently unavailable.", ex);
}
throw new AccessException("The file could not be accessed", ex);
}
}
示例7: FileDeletionErrorEventArgs
/// <summary>
/// Initializes a new instance of the <see cref="FileDeletionErrorEventArgs"/> class.
/// </summary>
/// <param name="file">The file.</param>
public FileDeletionErrorEventArgs(IFileInfo file)
{
file.ThrowIfNull(() => file);
this.File = file;
}
示例8: OpenFileStream
/// <summary>
/// Opens the stream of the specified file.
/// </summary>
/// <param name="file">The file.</param>
/// <returns>
/// A stream from the specified file.
/// </returns>
public Stream OpenFileStream(IFileInfo file)
{
file.ThrowIfNull(() => file);
return File.Open(file.FullName, FileMode.Open, FileAccess.Read);
}
示例9: OpenFileStream
/// <summary>
/// Opens the stream of the specified file.
/// </summary>
/// <param name="file">The file.</param>
/// <returns>
/// A stream from the specified file.
/// </returns>
public Stream OpenFileStream(IFileInfo file)
{
file.ThrowIfNull(() => file);
return this.client.OpenRead(new Uri(file.FullName));
}