本文整理汇总了C#中FileMode类的典型用法代码示例。如果您正苦于以下问题:C# FileMode类的具体用法?C# FileMode怎么用?C# FileMode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FileMode类属于命名空间,在下文中一共展示了FileMode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compress
public void Compress(string sourceFilename, string targetFilename, FileMode fileMode, OutArchiveFormat archiveFormat,
CompressionMethod compressionMethod, CompressionLevel compressionLevel, ZipEncryptionMethod zipEncryptionMethod,
string password, int bufferSize, int preallocationPercent, bool check, Dictionary<string, string> customParameters)
{
bufferSize *= this._sectorSize;
SevenZipCompressor compressor = new SevenZipCompressor();
compressor.FastCompression = true;
compressor.ArchiveFormat = archiveFormat;
compressor.CompressionMethod = compressionMethod;
compressor.CompressionLevel = compressionLevel;
compressor.DefaultItemName = Path.GetFileName(sourceFilename);
compressor.DirectoryStructure = false;
compressor.ZipEncryptionMethod = zipEncryptionMethod;
foreach (var pair in customParameters)
{
compressor.CustomParameters[pair.Key] = pair.Value;
}
using (FileStream sourceFileStream = new FileStream(sourceFilename,
FileMode.Open, FileAccess.Read, FileShare.None, bufferSize,
Win32.FileFlagNoBuffering | FileOptions.SequentialScan))
{
using (FileStream targetFileStream = new FileStream(targetFilename,
fileMode, FileAccess.ReadWrite, FileShare.ReadWrite, 8,
FileOptions.WriteThrough | Win32.FileFlagNoBuffering))
{
this.Compress(compressor, sourceFileStream, targetFileStream,
password, preallocationPercent, check, bufferSize);
}
}
}
示例2: FileModeChanged
private static void FileModeChanged(string[] assets, FileMode mode)
{
if (Provider.enabled && Provider.PromptAndCheckoutIfNeeded(assets, ""))
{
Provider.SetFileMode(assets, mode);
}
}
示例3: Open
/**
* open file.
* @param void.
* @return void.
*/
public bool Open(string strFileName, FileMode eMode, FileAccess eAccess)
{
if (string.IsNullOrEmpty(strFileName))
{
return false;
}
if ((FileMode.Open == eMode) && !File.Exists(strFileName))
{
return false;
}
try
{
m_cStream = new FileStream(strFileName, eMode, eAccess);
}
catch (Exception cEx)
{
Console.Write(cEx.Message);
}
if (null == m_cStream)
{
return false;
}
m_bOpen = true;
return true;
}
示例4: FileOpen
public Stream FileOpen(string filePath, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
{
#if WINDOWS_STORE_APP
var folder = ApplicationData.Current.LocalFolder;
if (fileMode == FileMode.Create || fileMode == FileMode.CreateNew)
{
return folder.OpenStreamForWriteAsync(filePath, CreationCollisionOption.ReplaceExisting).GetAwaiter().GetResult();
}
else if (fileMode == FileMode.OpenOrCreate)
{
if (fileAccess == FileAccess.Read)
return folder.OpenStreamForReadAsync(filePath).GetAwaiter().GetResult();
else
{
// Not using OpenStreamForReadAsync because the stream position is placed at the end of the file, instead of the beginning
var f = folder.CreateFileAsync(filePath, CreationCollisionOption.OpenIfExists).AsTask().GetAwaiter().GetResult();
return f.OpenAsync(FileAccessMode.ReadWrite).AsTask().GetAwaiter().GetResult().AsStream();
}
}
else if (fileMode == FileMode.Truncate)
{
return folder.OpenStreamForWriteAsync(filePath, CreationCollisionOption.ReplaceExisting).GetAwaiter().GetResult();
}
else
{
//if (fileMode == FileMode.Append)
// Not using OpenStreamForReadAsync because the stream position is placed at the end of the file, instead of the beginning
folder.CreateFileAsync(filePath, CreationCollisionOption.OpenIfExists).AsTask().GetAwaiter().GetResult().OpenAsync(FileAccessMode.ReadWrite).AsTask().GetAwaiter().GetResult().AsStream();
var f = folder.CreateFileAsync(filePath, CreationCollisionOption.OpenIfExists).AsTask().GetAwaiter().GetResult();
return f.OpenAsync(FileAccessMode.ReadWrite).AsTask().GetAwaiter().GetResult().AsStream();
}
#else
return File.Open(filePath, fileMode, fileAccess, fileShare);
#endif
}
示例5: OpenFileStream
/// <summary>
/// File might be locked when attempting to open it. This will attempt to open the file the number of times specified by <paramref name="retry"/>
/// </summary>
/// <param name="filePath">The file to attempt to get a file stream for</param>
/// <param name="retry">The number of times a file open should be attempted</param>
/// <param name="fileMode">The file mode to use</param>
/// <param name="fileAccess">The file access to use</param>
/// <param name="fileShare">The file sharing to use</param>
/// <returns>A file stream of the file</returns>
/// <remarks>
/// It attempt to open the file in increasingly longer periods and throw an exception if it cannot open it within the
/// specified number of retries.
/// </remarks>
public Stream OpenFileStream(string filePath, int retry, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
{
var delay = 0;
for(var i = 0; i < retry; i++)
{
try
{
var stream = new FileStream(filePath, fileMode, fileAccess, fileShare);
return stream;
}
catch(DirectoryNotFoundException)
{
CreateDirectoryStructure(filePath);
}
catch(FileNotFoundException)
{
throw;
}
catch(IOException)
{
delay += 100;
if(i == retry) throw;
}
Thread.Sleep(delay);
}
//We will never get here
throw new IOException(string.Format("Unable to open file '{0}'", filePath));
}
示例6: RemoteFileSystemFileStream
public RemoteFileSystemFileStream(RemoteFileSystem RemoteFileSystem, String FileName, FileMode FileMode)
: base(RemoteFileSystem, (Lazy<Stream>)null)
{
this.RemoteFileSystem = RemoteFileSystem;
this.FileName = FileName;
this.FileMode = FileMode;
}
示例7: CreateFile
private static extern SafeFileHandle CreateFile(string filename,
FileAccess desiredAccess,
FileShare shareMode,
IntPtr attributes,
FileMode creationDisposition,
uint flagsAndAttributes = 0,
IntPtr templateFile = default(IntPtr));
示例8: FileAccess
internal static void FileAccess(FileMode fileMode, FileAccess fileAccess)
{
// exception if:
// !write && append
// !write && create
// !write && createNew
// !write && truncate
var noWrite = (fileAccess & System.IO.FileAccess.Write) == 0;
if (noWrite && fileMode == FileMode.CreateNew)
throw new ArgumentException(string.Format(
"Can only open files in {0} mode when requesting FileAccess.Write access.", fileMode));
if (noWrite && fileMode == FileMode.Truncate)
throw new IOException("Cannot truncate a file if file mode doesn't include WRITE.");
// or if:
// readwrite && append
// read && append
if (fileAccess == System.IO.FileAccess.Read && fileMode == FileMode.Append)
throw new ArgumentException("Cannot open file in read-mode when having FileMode.Append");
//if (
// ((fileMode == FileMode.Append) && fileAccess != FileAccess.Write) ||
// ((fileMode == FileMode.CreateNew || fileMode == FileMode.Create || fileMode == FileMode.Truncate)
// && (fileAccess != FileAccess.Write && fileAccess != FileAccess.ReadWrite)) ||
// false //((Exists && fileMode == FileMode.OpenOrCreate && fileAccess == FileAccess.Write))
// )
}
示例9: FromFile
public static IStream FromFile(string fileName, FileMode mode, StreamInfo streamInfo)
{
if (fileName == null)
throw new ArgumentNullException("fileName");
return FromStream(File.Open(fileName, mode), streamInfo ?? StreamInfo.FromFile(fileName));
}
示例10: SerializeToFile
public static void SerializeToFile(object obj, string file, FileMode fileMode, XmlSerializerNamespaces namespaces, bool omitXmlDeclaration)
{
using (FileStream fileStream = new FileStream(file, fileMode))
{
SerializeToStream(obj, fileStream, namespaces, omitXmlDeclaration);
}
}
示例11: GetMode
// uint GetMode( FileMode mode )
// Converts the filemode constant to win32 constant
#region GetMode
private static uint GetMode(FileMode mode)
{
uint umode = 0;
switch (mode)
{
case FileMode.CreateNew:
umode = CREATE_NEW;
break;
case FileMode.Create:
umode = CREATE_ALWAYS;
break;
case FileMode.Append:
umode = OPEN_ALWAYS;
break;
case FileMode.Open:
umode = OPEN_EXISTING;
break;
case FileMode.OpenOrCreate:
umode = OPEN_ALWAYS;
break;
case FileMode.Truncate:
umode = TRUNCATE_EXISTING;
break;
}
return umode;
}
示例12: Output
public Output(string filename, FileMode mode)
{
StandardOutput = IsStandardOutput(filename);
Name = StandardOutput ? "(standard output)" : filename;
Mode = mode;
Open = () => StandardOutput ? Console.OpenStandardOutput() : File.Open(Name, Mode);
}
示例13: CreateFile
private static extern IntPtr CreateFile(string fileName,
FILE_ACCESS_RIGHTS access,
FileShare share,
int securityAttributes,
FileMode creation,
FILE_FLAGS flags,
IntPtr templateFile);
示例14: CreateFile
public static extern SafeFileHandle CreateFile(String fileName,
FileAccess desiredAccess,
FileShare shareMode,
IntPtr securityAttrs,
FileMode creationDisposition,
int flagsAndAttributes,
IntPtr templateFile);
示例15: ShapefileWriter
protected ShapefileWriter(string path, ShapefileHeader header, FileMode fileMode, FileAccess fileAccess)
{
_writerShape = new BinaryWriter(new FileStream(path, fileMode, fileAccess));
_writerIndex = new BinaryWriter(new FileStream(Path.ChangeExtension(path, ".shx"), fileMode, fileAccess));
_header = header;
}