本文整理汇总了C#中FileShare类的典型用法代码示例。如果您正苦于以下问题:C# FileShare类的具体用法?C# FileShare怎么用?C# FileShare使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileShare类属于命名空间,在下文中一共展示了FileShare类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Lock
public static void Lock(string path, int millisecondsTimeout, Action<FileStream> action,
FileMode mode, FileAccess access, FileShare share)
{
var log = Program.Log;
var autoResetEvent = new AutoResetEvent(false);
const string pattern = "Lock('{0}', {1}, {2}, {3}, {4})";
string msg = string.Format(pattern, path, millisecondsTimeout, mode, access, share);
log.Trace(msg);
while (true)
{
try
{
using (var stream = File.Open(path, mode, access, share))
{
const string pattern2 = "Access to file: {0}, {1}, {2}, {3}";
string msg2 = string.Format(pattern2, path, mode, access, share);
log.Trace(msg2);
action(stream);
break;
}
}
catch (IOException exc)
{
const string pattern3 = "Cannot access to file: {0}";
string msg3 = string.Format(pattern3, path);
log.TraceException(msg3, exc);
AutoResetFileSystemWatcher(path, millisecondsTimeout, autoResetEvent);
}
}
}
示例2: CreateFileHandle
/// <summary>
/// Pass the file handle to the <see cref="System.IO.FileStream"/> constructor.
/// The <see cref="System.IO.FileStream"/> will close the handle.
/// </summary>
public static SafeFileHandle CreateFileHandle(
string filePath,
CreationDisposition creationDisposition,
FileAccess fileAccess,
FileShare fileShare)
{
filePath = CheckAddLongPathPrefix(filePath);
// Create a file with generic write access
var fileHandle =
PInvokeHelper.CreateFile(
filePath,
fileAccess,
fileShare,
IntPtr.Zero,
creationDisposition,
0,
IntPtr.Zero);
// Check for errors.
var lastWin32Error = Marshal.GetLastWin32Error();
if (fileHandle.IsInvalid)
{
throw new Win32Exception(
lastWin32Error,
string.Format(
"Error {0} creating file handle for file path '{1}': {2}",
lastWin32Error,
filePath,
CheckAddDotEnd(new Win32Exception(lastWin32Error).Message)));
}
// Pass the file handle to FileStream. FileStream will close the handle.
return fileHandle;
}
示例3: CreateFile
public static extern SafeFileHandle CreateFile(String fileName,
FileAccess desiredAccess,
FileShare shareMode,
IntPtr securityAttrs,
FileMode creationDisposition,
int flagsAndAttributes,
IntPtr templateFile);
示例4: OpenSequentialStream
public static FileStream OpenSequentialStream(string path, FileMode mode, FileAccess access, FileShare share)
{
var options = FileOptions.SequentialScan;
if (concurrency > 0)
{
options |= FileOptions.Asynchronous;
}
#if MONO
return new FileStream( path, mode, access, share, bufferSize, options );
#else
if (unbuffered)
{
options |= NoBuffering;
}
else
{
return new FileStream(path, mode, access, share, bufferSize, options);
}
SafeFileHandle fileHandle = CreateFile(path, (int)access, share, IntPtr.Zero, mode, (int)options, IntPtr.Zero);
if (fileHandle.IsInvalid)
{
throw new IOException();
}
return new UnbufferedFileStream(fileHandle, access, bufferSize, (concurrency > 0));
#endif
}
示例5: ToString
static internal string ToString(string path, FileMode mode, FileAccess access, FileShare share)
{
// http://ee.php.net/fopen
//'r' Open for reading only; place the file pointer at the beginning of the file.
//'r+' Open for reading and writing; place the file pointer at the beginning of the file.
//'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
//'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
//'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
//'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
//'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
//'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
if (mode == FileMode.OpenOrCreate)
{
if (access == FileAccess.Write)
{
if (File.Exists(path))
return "r+b";
else
return "x+b";
}
if (access == FileAccess.Read)
{
if (File.Exists(path))
return "rb";
else
return "xb";
}
}
var e = new { mode, access, share };
throw new NotImplementedException(e.ToString());
}
示例6: CheckFileAccessRights
/// <summary>
/// Check's if the file has the accessrights specified in the input parameters
/// </summary>
/// <param name="filename"></param>
/// <param name="fa">Read,Write,ReadWrite</param>
/// <param name="fs">Read,ReadWrite...</param>
/// <returns></returns>
public static void CheckFileAccessRights(string fileName, FileMode fm, FileAccess fa, FileShare fs)
{
FileStream fileStream = null;
StreamReader streamReader = null;
try
{
Encoding fileEncoding = Encoding.Default;
fileStream = File.Open(fileName, fm, fa, fs);
streamReader = new StreamReader(fileStream, fileEncoding, true);
}
finally
{
try
{
if (fileStream != null)
{
fileStream.Close();
fileStream.Dispose();
}
if (streamReader != null)
{
streamReader.Close();
streamReader.Dispose();
}
}
finally {}
}
}
示例7: LogStream
internal LogStream(string path, int bufferSize, LogRetentionOption retention, long maxFileSize, int maxNumOfFiles)
{
string fullPath = Path.GetFullPath(path);
this._fileName = fullPath;
if (fullPath.StartsWith(@"\\.\", StringComparison.Ordinal))
{
throw new NotSupportedException(System.SR.GetString("NotSupported_IONonFileDevices"));
}
Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(FileShare.Read);
int num = 0x100000;
this._canWrite = true;
this._pathSav = fullPath;
this._fAccessSav = 0x40000000;
this._shareSav = FileShare.Read;
this._secAttrsSav = secAttrs;
this._secAccessSav = FileIOPermissionAccess.Write;
this._modeSav = (retention != LogRetentionOption.SingleFileUnboundedSize) ? FileMode.Create : FileMode.OpenOrCreate;
this._flagsAndAttributesSav = num;
this._seekToEndSav = retention == LogRetentionOption.SingleFileUnboundedSize;
base.bufferSize = bufferSize;
this._retention = retention;
this._maxFileSize = maxFileSize;
this._maxNumberOfFiles = maxNumOfFiles;
this._Init(fullPath, this._fAccessSav, this._shareSav, this._secAttrsSav, this._secAccessSav, this._modeSav, this._flagsAndAttributesSav, this._seekToEndSav);
}
示例8: CreateFile
public static Stream CreateFile(
string fileName, FileAccess fileAccess, FileShare fileShare, FileMode fileMode, FileAttributes flags)
{
// TODO: This is not quite right, but it's close.
//
var nativeAccess = fileAccess;
if ((nativeAccess & FileAccess.Read) != 0)
{
nativeAccess &= ~FileAccess.Read;
nativeAccess |= (FileAccess)GENERIC_READ;
}
if ((nativeAccess & FileAccess.Write) != 0)
{
nativeAccess &= ~FileAccess.Write;
nativeAccess |= (FileAccess)GENERIC_WRITE;
}
var handle = _CreateFile(fileName, nativeAccess, fileShare, IntPtr.Zero, fileMode, flags, IntPtr.Zero);
if (handle.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
return new SimpleFileStream(handle);
}
示例9: CreateFile
private static extern IntPtr CreateFile(string fileName,
FILE_ACCESS_RIGHTS access,
FileShare share,
int securityAttributes,
FileMode creation,
FILE_FLAGS flags,
IntPtr templateFile);
示例10: CreateFile
private static extern SafeFileHandle CreateFile(string filename,
FileAccess desiredAccess,
FileShare shareMode,
IntPtr attributes,
FileMode creationDisposition,
uint flagsAndAttributes = 0,
IntPtr templateFile = default(IntPtr));
示例11: 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
}
示例12: Open
/// <summary>
/// Opens a <see cref="FileStream"/>
/// </summary>
/// <param name="path">The file to open. </param>
/// <param name="mode"><see cref="FileMode"/></param>
/// <param name="access"><see cref="FileAccess"/></param>
/// <param name="share"><see cref="FileShare"/> </param>
/// <returns>A <see cref="FileStream"/></returns>
/// <remarks>http://msdn.microsoft.com/en-us/library/y973b725(v=vs.110).aspx</remarks>
public static FileStream Open( string path, FileMode mode, FileAccess access, FileShare share )
{
Contract.Requires( !String.IsNullOrWhiteSpace( path ) );
Contract.Ensures( Contract.Result<FileStream>() != null );
return OpenFileStream( path, access, mode, share );
}
示例13: OpenInputFileStream
public override Stream OpenInputFileStream(string path, FileMode mode, FileAccess access, FileShare share)
{
//TODO: OpenInputFileStream with params
Stream st = null;
if (access == FileAccess.Read)
{
st = OpenInputFileStream(path);
if (st == null)
{
CRhoFile file = new CRhoFile();
file.open(path, CRhoFile.EOpenModes.OpenReadOnly);
st = file.getStream();
}
}
else
{
CRhoFile file = new CRhoFile();
file.open(path, CRhoFile.EOpenModes.OpenForReadWrite);
st = file.getStream();
}
return st;
}
示例14: SeqFileStreamReader
public SeqFileStreamReader(string fn, FileMode mode, FileAccess access, FileShare share, int bufferSize)
: base(fn, mode, access, share, bufferSize)
{
curfilename = fn;
bufsz = bufferSize;
_init();
}
示例15: IsolatedStorageFileStream
public IsolatedStorageFileStream(String path, FileMode mode,
FileAccess access, FileShare share,
int bufferSize)
: this(path, mode, access, share, bufferSize, null)
{
// Nothing to do here.
}