本文整理汇总了C#中System.IO.FileStream.Close方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.Close方法的具体用法?C# FileStream.Close怎么用?C# FileStream.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteToLog
/// <summary>
/// Запись в ЛОГ-файл
/// </summary>
/// <param name="str"></param>
public void WriteToLog(string str, bool doWrite = true)
{
if (doWrite)
{
StreamWriter sw = null;
FileStream fs = null;
try
{
string curDir = AppDomain.CurrentDomain.BaseDirectory;
fs = new FileStream(curDir + "teplouchetlog.pi", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
sw = new StreamWriter(fs, Encoding.Default);
if (m_vport == null) sw.WriteLine(DateTime.Now.ToString() + ": Unknown port: adress: " + m_address + ": " + str);
else sw.WriteLine(DateTime.Now.ToString() + ": " + m_vport.GetName() + ": adress: " + m_address + ": " + str);
sw.Close();
fs.Close();
}
catch
{
}
finally
{
if (sw != null)
{
sw.Close();
sw = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
}
}
}
示例2: while
try
{
byte[] lBuffer = new byte[BUFFER_SIZE];
int lBytesRead = lStream.Read(lBuffer, 0, BUFFER_SIZE);
while (lBytesRead > 0)
{
aToStream.Write(lBuffer,0,lBytesRead);
lBytesRead = lStream.Read(lBuffer, 0, BUFFER_SIZE);
}
}
finally
{
lStream.Close();
}
}
public override void CreateFile(Stream aStream)
{
if (File.Exists(LocalPath))
throw new Exception("Error savinf file to disk: file already exist.");
Stream lStream = new FileStream(LocalPath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
try
{
byte[] lBuffer = new byte[BUFFER_SIZE];
int lBytesRead = aStream.Read(lBuffer, 0, BUFFER_SIZE);
while (lBytesRead > 0)
示例3: Compare
public static bool Compare(string file1, string file2)
{
if (file1.Equals(file2))
return true;
if (!File.Exists(file1) || !File.Exists(file2))
return false;
FileStream first = new FileStream(file1, FileMode.Open);
FileStream second = new FileStream(file2, FileMode.Open);
if (first.Length != second.Length)
{
first.Close();
second.Close();
return false;
}
int byte1;
int byte2;
do
{
byte1 = first.ReadByte();
byte2 = second.ReadByte();
}
while ((byte1 == byte2) && (byte1 != -1));
first.Close();
second.Close();
return (byte1 == byte2);
}
示例4: getPicture
/* return null on error*/
public byte[] getPicture(string picname)
{
if (m_pictures.ContainsKey(picname))
return m_pictures[picname];
// try to load from dics
FileStream fs;
try
{
fs = new FileStream(m_dir + "\\" + picname, FileMode.Open);
}
catch (Exception)
{
return null;
}
try
{
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
m_pictures[picname] = data;
fs.Close();
return data;
}
catch (Exception)
{
fs.Close();
}
return null;
}
示例5: IpHelper
private static long _indexcount;//索引个数
static IpHelper() {
string filePath = Common.GetMapPath("/App_Data/Site/ipdata.config");
if (File.Exists(filePath)) {
try {
_ipdatefile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch {
_ipdatefile.Close();
_ipdatefile.Dispose();
return;
}
_ipdatefile.Position = 0;
_indexareabegin = ReadByte4();
_indexareaend = ReadByte4();
_indexcount = (_indexareaend - _indexareabegin) / LENGTH + 1;
if (_indexcount > 0) {
_state = true;
}
else {
_ipdatefile.Close();
_ipdatefile.Dispose();
}
}
}
示例6: Activate
public static bool Activate()
{
FileStream fs = new FileStream("license.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
try
{
StreamReader sr = new StreamReader(fs);
string a = sr.ReadLine();
string b = sr.ReadLine();
a = CryptorEngine.Decrypt(a);
b = CryptorEngine.Decrypt(b);
sr.Close();
fs.Close();
string[] tabs = a.Split('\n');
Statics.ValidTabletsIdList = new List<string>();
for (int i = 0; i < tabs.Length; i++)
Statics.ValidTabletsIdList.Add(tabs[i]);
if (b == Statics.HardwareId)
return true;
else
return false;
}
catch
{
fs.Close();
return false;
}
}
示例7: saveButton_Click
private void saveButton_Click(object sender, EventArgs e)
{
FileStream aStreamForReading = new FileStream(fileLocation, FileMode.Open);
CsvFileReader aReader = new CsvFileReader(aStreamForReading);
List<string> aRecord = new List<string>();
while (aReader.ReadRow(aRecord))
{
string regNo = aRecord[0];
if (regNoTextBox.Text == regNo)
{
MessageBox.Show(@"Reg no already exists");
aStreamForReading.Close();
return;
}
}
aStreamForReading.Close();
FileStream aStream = new FileStream(fileLocation, FileMode.Append);
CsvFileWriter aWriter = new CsvFileWriter(aStream);
List<string> aStudentRecord = new List<string>();
aStudentRecord.Add(regNoTextBox.Text);
aStudentRecord.Add(nameTextBox.Text);
aWriter.WriteRow(aStudentRecord);
aStream.Close();
}
示例8: FindString
public static bool FindString(FileInfo file, string str)
{
string currentStr;
FileStream fin = null;
StreamReader reader = null;
try
{
fin = new FileStream(file.DirectoryName + "\\" + file.Name, FileMode.Open);
reader = new StreamReader(fin);
while ((currentStr = reader.ReadLine()) != null)
{
if (currentStr.Contains(str)) return true;
}
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (fin != null) fin.Close();
if (reader != null) fin.Close();
}
return false;
}
示例9: CopyAsync
private static void CopyAsync(byte[] buffer, FileStream inputStream, Stream outputStream, TaskCompletionSource<object> tcs)
{
inputStream.ReadAsync(buffer).Then(read =>
{
if (read > 0)
{
outputStream.WriteAsync(buffer, 0, read)
.Then(() => CopyAsync(buffer, inputStream, outputStream, tcs))
.Catch(ex =>
{
inputStream.Close();
outputStream.Close();
tcs.SetException(ex);
});
}
else
{
inputStream.Close();
outputStream.Close();
tcs.SetResult(null);
}
})
.Catch(ex =>
{
inputStream.Close();
outputStream.Close();
tcs.SetException(ex);
});
}
示例10: load
public static bool load()
{
//---------------------------------输出数据库---------------------------
if (!(File.Exists(strDatabasePath)))
{
string dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ContorlSetting\\";
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
FileStream fs = new FileStream(strDatabasePath, FileMode.OpenOrCreate, FileAccess.Write);
try
{
Byte[] b = SAS.Properties.Resources.DataBase;
fs.Write(b, 0, b.Length);
if (fs != null)
fs.Close();
}
catch
{
if (fs != null)
fs.Close();
return false;
}
}
// -----------------------------第一次加载代码,------------------------------
//if (conn.State != ConnectionState.Open)
// conn.Open();
////-----------------------------操作完成后记得关闭连接------------------------------
//conn.Close();
return true;
}
示例11: loadOpsFile
public static IOperation[] loadOpsFile(string path, Type[] knownTypes)
{
//Open the operation file.
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
//Read in the file and store each item to a list. Type is "anyType" but this will be cast to IOperation.
DataContractSerializer serializer = new DataContractSerializer(typeof(IOperation), knownTypes);
XmlReader read = XmlReader.Create(fs);
read.ReadToDescendant("z:anyType");
List<IOperation> opList = new List<IOperation>();
//Blurahegle
while (serializer.IsStartObject(read))
{
//Check each type when deserializing. Make sure we can cast it.
try
{
opList.Add((IOperation)serializer.ReadObject(read));
}
catch (Exception e)
{
MessageBox.Show("Invalid operation type encountered. Please ensure all required libraies are installed \n" + e.Message, "An error has occured",MessageBoxButtons.OK,MessageBoxIcon.Error);
fs.Close();
return null;
}
}
fs.Close();
return opList.ToArray();
//Done
}
示例12: DisplayContent
private static void DisplayContent(HttpWebResponse response, string id)
{
Stream stream = response.GetResponseStream();
if (stream != null)
{
string filePath = "D:\\pic\\" + id + ".png";
FileStream fs = new FileStream(filePath, FileMode.Create);
GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress);
byte[] bytes = new byte[1024];
int len;
if ((len = gzip.Read(bytes, 0, bytes.Length)) > 0)
{
fs.Write(bytes, 0, len);
}
else
{
fs.Close();
File.Delete(filePath);
throw new Exception();
}
while ((len = gzip.Read(bytes, 0, bytes.Length)) > 0)
{
fs.Write(bytes, 0, len);
}
fs.Close();
}
}
示例13: EditFile
public static void EditFile(int curLine, string newLineValue, string patch)
{
FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
string line = sr.ReadLine();
StringBuilder sb = new StringBuilder();
if (curLine == 1)
{
line = newLineValue;
sb.Append(line + "\r\n");
}
else
{
for (int i = 1; line != null; i++)
{
sb.Append(line + "\r\n");
if (i != curLine - 1)
line = sr.ReadLine();
else
{
sr.ReadLine();
line = newLineValue;
}
}
}
sr.Close();
fs.Close();
FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs1);
sw.Write(sb.ToString());
sw.Close();
fs.Close();
}
示例14: main
private void main()
{
// Nome arquivo de saida. Mude para o valor que quiser
FileStream arquivo1Pdf = new FileStream(@"D:\Temp\teste1.pdf", FileMode.OpenOrCreate, FileAccess.Write);
ControladorPDF.Instancia.obterProgramacaoPDF(new DateTime(2010, 06, 28), TipoProgramacao.DiaDaSemana, arquivo1Pdf);
//arquivo1Pdf.Flush();
arquivo1Pdf.Close();
// mesmo teste, mudando o tipo da programacao
FileStream arquivo2Pdf = new FileStream(@"D:\Temp\teste2.pdf", FileMode.OpenOrCreate, FileAccess.Write);
ControladorPDF.Instancia.obterProgramacaoPDF(new DateTime(2010, 06, 28), TipoProgramacao.Diaria, arquivo2Pdf);
//arquivo2Pdf.Flush();
arquivo2Pdf.Close();
// testa periodo sem programacao
FileStream arquivo3Pdf = new FileStream(@"D:\Temp\teste3.pdf", FileMode.OpenOrCreate, FileAccess.Write);
ControladorPDF.Instancia.obterProgramacaoPDF(new DateTime(2020, 06, 28), TipoProgramacao.Diaria, arquivo3Pdf);
//arquivo2Pdf.Flush();
arquivo3Pdf.Close();
// Nome arquivo de saida. Mude para o valor que quiser
FileStream arquivo4Pdf = new FileStream(@"D:\Temp\teste4.pdf", FileMode.OpenOrCreate, FileAccess.Write);
ControladorPDF.Instancia.obterAtividadesPDF(TipoAgrupamentoAtividade.DiaDaSemana, arquivo4Pdf);
//arquivo1Pdf.Flush();
arquivo1Pdf.Close();
// Nome arquivo de saida. Mude para o valor que quiser
FileStream arquivo5Pdf = new FileStream(@"D:\Temp\teste5.pdf", FileMode.OpenOrCreate, FileAccess.Write);
ControladorPDF.Instancia.obterAtividadesPDF(TipoAgrupamentoAtividade.TipoAtividade, arquivo5Pdf);
//arquivo1Pdf.Flush();
arquivo1Pdf.Close();
}
示例15: DecompressFileLZMA
public static void DecompressFileLZMA(string inFile, string outFile)
{
SevenZip.Sdk.Compression.Lzma.Decoder coder = new SevenZip.Sdk.Compression.Lzma.Decoder();
FileStream input = new FileStream(inFile, FileMode.Open);
FileStream output = new FileStream(outFile, FileMode.Create);
// Read the decoder properties
byte[] properties = new byte[5];
input.Read(properties, 0, 5);
// Read in the decompress file size.
byte[] fileLengthBytes = new byte[8];
input.Read(fileLengthBytes, 0, 8);
long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
coder.SetDecoderProperties(properties);
coder.Code(input, output, input.Length, fileLength, null);
output.Flush();
output.Close();
try
{
output.Flush();
output.Close();
input.Flush();
input.Close();
}
catch
{
}
}