『壹』 C# FileStream讀寫文件(.txt)時候用excel操作此文件,寫入數據線程會被終止
private void WriteIntoFile(object obj,string filename)
{
lock (o)
{
using (FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
{
byte[] bytData = rawSerialize(obj);
fs.Write(bytData, 0, bytData.Length);
fs.Close();
}
}
}
第一,嘗試下釋放fs?
第二,嘗試下運行共版享寫權?using (FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write,FileShare.Write))?
『貳』 C#FileStream循環讀取大文件有什麼好處
雖然是在 while 循環內實現讀寫,但不能說是循環讀取(循環讀取說的是讀到尾後有從頭讀),而是分段讀取
分段讀取可以減少緩存空間的內存開銷,合適的緩存空間可提高硬碟的尋道效率
『叄』 FileStream怎樣獲得文件相對路徑
string strAppPath = Application.StartupPath; //獲得可來執行文件的自路徑。
string strConfigPath = strAppPath + "\\Config\\DBConfig.ini"; //自己調整一下相對路徑。
『肆』 C#用FileStream類文件讀出指定txt文件內容時,怎麼前面多了「」從哪兒來的
應該是編碼問題, 請以默認編碼格式讀取文本內容
『伍』 C#,怎樣利用filestream流查詢文件中的某幾個字
private string SelectStr(string str)
{
FileStream fs = new FileStream("d:\\a.txt", FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312"));
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = m_streamReader.ReadLine();
do
{
if (strLine.Contains(str))
{
return strLine.Substring(strLine.IndexOf(str) - 4, 4);
}
strLine = m_streamReader.ReadLine();
} while (strLine != null && strLine != "");
m_streamReader.Close();
m_streamReader.Dispose();
fs.Close();
fs.Dispose();
return "";
}
}
『陸』 C# 用filestream讀取文本的問題
剛好前天寫了一個類,送你了.
public enum ArchiveMode
{
store,
load,
}
public static string BytesToString(byte[] bs, int start, int bsLen)
{
string ss = Encoding.Unicode.GetString(bs, start, bsLen);
return ss;
}
public static void StringToBytes(string text, byte[] bs, out int bsLen)
{
bsLen = Encoding.Unicode.GetBytes(text, 0, text.Length, bs, 0);
}
public class Archive
{
FileStream m_fs = null;
ArchiveMode m_Mode;
public Archive(FileStream fs, ArchiveMode mode)
{
m_Mode = mode;
m_fs = fs;
}
public void Close()
{
m_fs = null;
}
public bool IsStore
{
get { return m_Mode == ArchiveMode.store; }
}
public bool IsLoad
{
get { return m_Mode == ArchiveMode.load; }
}
public DateTime ReadDataTime()
{
if (IsStore)
throw new Exception("store模式下不允許讀取");
byte[] bs8 = new byte[8];
m_fs.Read(bs8, 0, 8);
long l = System.BitConverter.ToInt64(bs8, 0);
DateTime dt = new DateTime(l);
return dt;
}
public void WriteDataTime(DateTime dt)
{
if (IsLoad)
throw new Exception("load模式下不允許寫入");
byte[] bs = System.BitConverter.GetBytes(dt.ToBinary());
m_fs.Write(bs, 0, 8);
}
public int ReadCount()
{
if (IsStore)
throw new Exception("store模式下不允許讀取");
byte[] bs = new byte[4];
m_fs.Read(bs, 0, 4);
return System.BitConverter.ToInt32(bs, 0);
}
public void WriteCount(int n)
{
if (IsLoad)
throw new Exception("load模式下不允許寫入");
byte[] bs = System.BitConverter.GetBytes(n);
//if (n > 0xFFFF)
//{
// m_fs.Write(System.BitConverter.GetBytes(0xFFFF), 0, 2);
//}
m_fs.Write(bs, 0, bs.Length);
}
public long ReadLong()
{
if (IsStore)
throw new Exception("store模式下不允許讀取");
byte[] bs = new byte[8];
m_fs.Read(bs, 0, 8);
return System.BitConverter.ToInt64(bs, 0);
}
public void WriteLong(long n)
{
if (IsLoad)
throw new Exception("load模式下不允許寫入");
byte[] bs = System.BitConverter.GetBytes(n);
m_fs.Write(bs, 0, bs.Length);
}
public int Read(byte[] bs, int count)
{
if (IsStore)
throw new Exception("store模式下不允許讀取");
return m_fs.Read(bs, 0, count);
}
public void Write(byte[] bs, int count)
{
if (IsLoad)
throw new Exception("load模式下不允許寫入");
m_fs.Write(bs, 0, count);
}
public void WriteString(string text)
{
int len = text.Length * 2;
byte[] bs = new byte[len + 8];
int oulen = 0;
F.StringToBytes(text, bs, out oulen);
WriteCount(oulen);
Write(bs, oulen);
}
public string ReadString()
{
int len = ReadCount();
byte[] bs = new byte[len];
Read(bs, len);
return F.BytesToString(bs, 0, len);
}
}
『柒』 asp.net filestream 如何讀取pdf文件,這個該怎麼做
<%@ Page Language="C#"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%
Response.Clear();
string path = @"F:\TDDOWNLOAD\Book\C#游戲編程入門.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disponstion", "filename=C#游戲編程入門.pdf");
byte[] buffer = new byte[256];
FileStream fs = File.Open(path, FileMode.Open);
Response.AddHeader("content-length", fs.Length.ToString());
int length = (int)fs.Length;
if (Response.IsClientConnected)
{
while (length > -1)
{
length = length - buffer.Length;
fs.Read(buffer, 0, buffer.Length);
Response.OutputStream.Write(buffer, 0, buffer.Length);
}
}
fs.Close();
Response.End();
%>
是哦 這個就是顯示到網頁上的。
『捌』 C#怎麼用FileStream一行一行的讀取文本
FileStream fs = New FileStream (文件路來徑, FileMode.Open);
StreamReader streamReader = new StreamReader(fileStream);
string line = "";
while ((line = streamReader.ReadLine()) !源= null)
{
//line就是一行一行的文本
。。。
}