本文整理汇总了C#中System.IO.FileStream.Seek方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.FileStream.Seek方法的具体用法?C# System.IO.FileStream.Seek怎么用?C# System.IO.FileStream.Seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了System.IO.FileStream.Seek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDllMachineType
public static MachineType GetDllMachineType(this string dllPath)
{
// See http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
// Offset to PE header is always at 0x3C.
// The PE header starts with "PE\0\0" = 0x50 0x45 0x00 0x00,
// followed by a 2-byte machine type field (see the document above for the enum).
//
using (var fs = new System.IO.FileStream(dllPath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
using (var br = new System.IO.BinaryReader(fs)) {
MachineType machineType = MachineType.IMAGE_FILE_MACHINE_UNKNOWN;
// bool isgood = false;
try {
fs.Seek(0x3c, System.IO.SeekOrigin.Begin);
Int32 peOffset = br.ReadInt32();
fs.Seek(peOffset, System.IO.SeekOrigin.Begin);
UInt32 peHead = br.ReadUInt32();
if (peHead != 0x00004550)
// "PE\0\0", little-endian
throw new Exception("Can't find PE header");
machineType = (MachineType)br.ReadUInt16();
// isgood = true;
}
catch {
// isgood = false;
}
finally {
br.Close();
fs.Close();
}
return machineType;
}
}
示例2: LoadImage
public static BitmapImage LoadImage(string path)
{
try
{
var fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
fs.Seek(0, System.IO.SeekOrigin.Begin);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
fs.Close();
var ms = new System.IO.MemoryStream(bytes);
ms.Position = 0;
var image = new BitmapImage();
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
return image;
}
catch (Exception ex)
{
}
return null;
}
示例3: LogError
public void LogError(Exception ex, string source)
{
try
{
String LogFile = HttpContext.Current.Request.MapPath("/Errorlog.txt");
if (LogFile != "")
{
String Message = String.Format("{0}{0}=== {1} ==={0}{2}{0}{3}{0}{4}{0}{5}"
, Environment.NewLine
, DateTime.Now
, ex.Message
, source
, ex.InnerException
, ex.StackTrace);
byte[] binLogString = Encoding.Default.GetBytes(Message);
System.IO.FileStream loFile = new System.IO.FileStream(LogFile
, System.IO.FileMode.OpenOrCreate
, System.IO.FileAccess.Write, System.IO.FileShare.Write);
loFile.Seek(0, System.IO.SeekOrigin.End);
loFile.Write(binLogString, 0, binLogString.Length);
loFile.Close();
}
}
catch { /*No need to catch the error here. */}
}
示例4: DetermineVersion
private static SQLCEVersion DetermineVersion(string filename)
{
var versionDictionary = new System.Collections.Generic.Dictionary<int, SQLCEVersion>
{
{ 0x73616261, SQLCEVersion.SQLCE20 },
{ 0x002dd714, SQLCEVersion.SQLCE30},
{ 0x00357b9d, SQLCEVersion.SQLCE35},
{ 0x003d0900, SQLCEVersion.SQLCE40}
};
int versionLONGWORD = 0;
try
{
using (var fs = new System.IO.FileStream(filename, System.IO.FileMode.Open))
{
fs.Seek(16, System.IO.SeekOrigin.Begin);
using (System.IO.BinaryReader reader = new System.IO.BinaryReader(fs))
{
versionLONGWORD = reader.ReadInt32();
}
}
}
catch
{
throw;
}
if (versionDictionary.ContainsKey(versionLONGWORD))
{
return versionDictionary[versionLONGWORD];
}
else
{
throw new ApplicationException("Unable to determine database file version");
}
}
示例5: DeBug
/// <summary>
/// Debug
/// </summary>
/// <param name="strPath">文件路径</param>
/// <param name="msg">内容</param>
public static void DeBug(string strPath, object msg)
{
System.IO.StreamWriter sw = null;
System.IO.FileStream fs = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read);
fs.Seek(0, System.IO.SeekOrigin.End);
sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
string LineText = DateTime.Now.ToString() + ", " + msg.ToString();
sw.WriteLine(LineText);
sw.Close();
sw = null;
fs.Close();
fs = null;
}
示例6: CreateFileStream
public System.IO.Stream CreateFileStream(string path, System.IO.FileMode mode = System.IO.FileMode.Open, System.IO.FileAccess access = System.IO.FileAccess.Read, System.IO.FileShare share = System.IO.FileShare.ReadWrite)
{
if (path == null) throw new ArgumentNullException(nameof(path));
// Flags match what FileStream does internally
NativeMethods.FileManagement.AllFileAttributeFlags flags = NativeMethods.FileManagement.AllFileAttributeFlags.SECURITY_SQOS_PRESENT | NativeMethods.FileManagement.AllFileAttributeFlags.SECURITY_ANONYMOUS;
SafeFileHandle handle = NativeMethods.FileManagement.CreateFile(path, access, share, mode, flags);
System.IO.Stream stream = new System.IO.FileStream(handle, access);
if (mode == System.IO.FileMode.Append)
{
stream.Seek(0, System.IO.SeekOrigin.End);
}
return stream;
}
示例7: ValidateExe
protected static bool ValidateExe(string path, long time_stamp_offset, uint time_stamp)
{
using (var fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
using (var s = new System.IO.BinaryReader(fs))
{
if (fs.Length > (time_stamp_offset+4))
{
fs.Seek(time_stamp_offset, System.IO.SeekOrigin.Begin);
uint ts = s.ReadUInt32();
return ts == time_stamp;
}
}
return false;
}
示例8: StandardFileObject
public StandardFileObject(string sPath, bool fWrite)
{
try
{
m_theFile = new System.IO.FileStream(sPath,
(fWrite) ? System.IO.FileMode.OpenOrCreate : System.IO.FileMode.Open,
(fWrite) ? System.IO.FileAccess.Write : System.IO.FileAccess.Read);
if (fWrite)
{
m_theFile.Seek(0, System.IO.SeekOrigin.End);
}
m_fLoaded = true;
}
catch (System.IO.IOException)
{
m_theFile = null;
}
}
示例9: extractMp3Info
public void extractMp3Info()
{
if (System.IO.File.Exists(Path))
{
using (System.IO.FileStream fs = new System.IO.FileStream(Path, System.IO.FileMode.Open))
{
byte[] b = new byte[128];
fs.Seek(-128, System.IO.SeekOrigin.End);
fs.Read(b, 0, 128);
if (System.Text.Encoding.Default.GetString(b, 0, 3).CompareTo("TAG") == 0)
{
Title = System.Text.Encoding.Default.GetString(b, 3, 3).TrimEnd('\0');
Artists = System.Text.Encoding.Default.GetString(b, 33, 30).TrimEnd('\0');
Album = System.Text.Encoding.Default.GetString(b, 63, 30).TrimEnd('\0');
}
fs.Close();
}
}
}
示例10: getMusicInfos
public void getMusicInfos()
{
if (System.IO.File.Exists(Path))
{
/* USING DISPOSE FROM THE OBJ ONCE THE CODE SECTION IS OVER */
using (System.IO.FileStream fs = new System.IO.FileStream(Path, System.IO.FileMode.Open))
{
byte[] b = new byte[128];
fs.Seek(-128, System.IO.SeekOrigin.End);
fs.Read(b, 0, 128);
if (System.Text.Encoding.Default.GetString(b, 0, 3).CompareTo("TAG") == 0)
{
Title = System.Text.Encoding.Default.GetString(b, 3, 30).TrimEnd('\0');
Artist = System.Text.Encoding.Default.GetString(b, 33, 30).TrimEnd('\0');
Album = System.Text.Encoding.Default.GetString(b, 63, 30).TrimEnd('\0');
}
fs.Close();
}
}
}
示例11: WriteModifications
private void WriteModifications(string dataHistoryFile, IIntegrationResult result)
{
System.IO.FileStream fs = new System.IO.FileStream( dataHistoryFile, System.IO.FileMode.Append);
fs.Seek(0, System.IO.SeekOrigin.End);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
System.Xml.XmlTextWriter currentBuildInfoWriter = new System.Xml.XmlTextWriter(sw);
currentBuildInfoWriter.Formatting = System.Xml.Formatting.Indented;
currentBuildInfoWriter.WriteStartElement("Build");
WriteXMLAttributeAndValue(currentBuildInfoWriter, "BuildDate", Util.DateUtil.FormatDate(result.EndTime));
WriteXMLAttributeAndValue(currentBuildInfoWriter, "Success", result.Succeeded.ToString(CultureInfo.CurrentCulture));
WriteXMLAttributeAndValue(currentBuildInfoWriter, "Label", result.Label);
if (result.Modifications.Length > 0)
{
currentBuildInfoWriter.WriteStartElement("modifications");
for (int i = 0; i < result.Modifications.Length; i++)
{
result.Modifications[i].ToXml(currentBuildInfoWriter);
}
currentBuildInfoWriter.WriteEndElement();
}
currentBuildInfoWriter.WriteEndElement();
sw.WriteLine();
sw.Flush();
fs.Flush();
sw.Close();
fs.Close();
}
示例12: Seek
public override void Seek(long pos)
{
curBufIndex = (int) (pos / maxBufSize);
curBuf = buffers[curBufIndex];
int bufOffset = (int) (pos - (curBufIndex * maxBufSize));
curBuf.Seek(bufOffset, System.IO.SeekOrigin.Begin);
curAvail = bufSizes[curBufIndex] - bufOffset;
}
示例13: ReadBytes
public override void ReadBytes(byte[] b, int offset, int len)
{
while (len > curAvail)
{
// curBuf.get_Renamed(b, offset, curAvail); // {{Aroush-1.9}}
len -= curAvail;
offset += curAvail;
curBufIndex++;
curBuf = buffers[curBufIndex]; // index out of bounds when too many bytes requested
curBuf.Seek(0, System.IO.SeekOrigin.Begin);
curAvail = bufSizes[curBufIndex];
}
// curBuf.get_Renamed(b, offset, len); // {{Aroush-1.9}}
curAvail -= len;
}
示例14: ReadByte
public override byte ReadByte()
{
// Performance might be improved by reading ahead into an array of
// eg. 128 bytes and readByte() from there.
if (curAvail == 0)
{
curBufIndex++;
curBuf = buffers[curBufIndex]; // index out of bounds when too many bytes requested
curBuf.Seek(0, System.IO.SeekOrigin.Begin);
curAvail = bufSizes[curBufIndex];
}
curAvail--;
return 0; // return curBuf.get_Renamed(); // {{Aroush-1.9}}
}
示例15: readFile
//.........这里部分代码省略.........
this.SetErrorIdentification();
this.SetIdentificationWarning("File disappeared or cannot be read");
return;
}
try {
int numBytes = 100; //binStream.available();
if (numBytes > 0)
{
//BufferedInputStream buffStream = new BufferedInputStream(binStream);
System.IO.BufferedStream buffStream = new System.IO.BufferedStream(binStream);
fileBytes = new byte[numBytes];
int len = buffStream.Read(fileBytes, 0, numBytes);
if(len != numBytes) {
//This means that all bytes were not successfully read
this.SetErrorIdentification();
this.SetIdentificationWarning("Error reading file: "+ len.ToString() + " bytes read from file when " + numBytes.ToString() + " were expected");
}
else if(len != -1)
{
//This means that the end of the file was not reached
this.SetErrorIdentification();
this.SetIdentificationWarning("Error reading file: Unable to read to the end");
}
else
{
myNumBytes = (long) numBytes;
}
buffStream.Close();
} else {
//If file is empty , status is error
this.SetErrorIdentification();
myNumBytes = 0L;
this.SetIdentificationWarning("Zero-length file");
}
binStream.Close();
isRandomAccess = false;
} catch(System.IO.IOException e) {
this.SetErrorIdentification();
this.SetIdentificationWarning("Error reading file: " + e.ToString());
}
catch(System.OutOfMemoryException)
{
try {
myRandomAccessFile = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open); //RandomAccessFile(file,"r");
isRandomAccess = true;
//record the file size
myNumBytes = myRandomAccessFile.Length;
//try reading in a buffer
myRandomAccessFile.Seek(0, System.IO.SeekOrigin.Begin); //(0L);
bool tryAgain = true;
while(tryAgain) {
try
{
fileBytes = new byte[(int)randomFileBufferSize];
myRandomAccessFile.Read(fileBytes, 0, randomFileBufferSize);
// .read(fileBytes);
tryAgain = false;
}
catch(OutOfMemoryException e4)
{
randomFileBufferSize = randomFileBufferSize/RAF_BUFFER_REDUCTION_FACTOR;
if(randomFileBufferSize< MIN_RAF_BUFFER_SIZE) {
throw e4;
}
}
}
myRAFoffset = 0L;
}
catch (System.IO.FileNotFoundException)
{
this.SetErrorIdentification();
this.SetIdentificationWarning("File disappeared or cannot be read");
}
catch(Exception e2)
{
try
{
myRandomAccessFile.Close();
}
catch(System.IO.IOException)
{
}
this.SetErrorIdentification();
this.SetIdentificationWarning("Error reading file: " + e2.ToString());
}
}
}