本文整理汇总了C#中IOMode.ToFileAccess方法的典型用法代码示例。如果您正苦于以下问题:C# IOMode.ToFileAccess方法的具体用法?C# IOMode.ToFileAccess怎么用?C# IOMode.ToFileAccess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IOMode
的用法示例。
在下文中一共展示了IOMode.ToFileAccess方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OpenFileStream
public static Stream/*!*/ OpenFileStream(RubyContext/*!*/ context, string/*!*/ path, IOMode mode) {
ContractUtils.RequiresNotNull(path, "path");
FileAccess access = mode.ToFileAccess();
FileMode fileMode;
if ((mode & IOMode.CreateIfNotExists) != 0) {
if ((mode & IOMode.ErrorIfExists) != 0) {
access |= FileAccess.Write;
fileMode = FileMode.CreateNew;
} else {
fileMode = FileMode.OpenOrCreate;
}
} else {
fileMode = FileMode.Open;
}
if ((mode & IOMode.Truncate) != 0 && (access & FileAccess.Write) == 0) {
throw RubyExceptions.CreateEINVAL("cannot truncate a file opened for reading only");
}
if ((mode & IOMode.WriteAppends) != 0 && (access & FileAccess.Write) == 0) {
throw RubyExceptions.CreateEINVAL("cannot append to a file opened for reading only");
}
if (String.IsNullOrEmpty(path)) {
throw RubyExceptions.CreateEINVAL();
}
Stream stream;
if (path == "NUL") {
stream = Stream.Null;
} else {
try {
stream = context.DomainManager.Platform.OpenInputFileStream(path, fileMode, access, FileShare.ReadWrite);
} catch (FileNotFoundException) {
throw RubyExceptions.CreateENOENT(String.Format("No such file or directory - {0}", path));
} catch (DirectoryNotFoundException e) {
throw RubyExceptions.CreateENOENT(e.Message, e);
} catch (PathTooLongException e) {
throw RubyExceptions.CreateENOENT(e.Message, e);
} catch (IOException) {
if ((mode & IOMode.ErrorIfExists) != 0) {
throw RubyExceptions.CreateEEXIST(path);
} else {
throw;
}
} catch (ArgumentException e) {
throw RubyExceptions.CreateEINVAL(e.Message, e);
}
}
if ((mode & IOMode.Truncate) != 0) {
stream.SetLength(0);
}
return stream;
}