本文整理汇总了C#中FileStream.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.Dispose方法的具体用法?C# FileStream.Dispose怎么用?C# FileStream.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileStream
的用法示例。
在下文中一共展示了FileStream.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
//Create text file
string filePath = @"../../UsingFiles/01.ReadOddLines.txt";
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Dispose();
//Write text in file
using (StreamWriter write = new StreamWriter(filePath,false,Encoding.GetEncoding("Windows-1251")))
{
for (int i = 0; i < 20; i++)
{
write.WriteLine("Line {0}", i);
}
}
//Read text
using (StreamReader read = new StreamReader(filePath))
{
string line = read.ReadLine();
int lineCounter = 0;
while (line != null)
{
if (lineCounter % 2 != 0)
{
Console.WriteLine(line);
}
line = read.ReadLine();
lineCounter++;
}
}
}
示例2: InserteWalletDepositForm
public int InserteWalletDepositForm(string Enumber, string Formtype, string paymentdate, string amountpaid, string trnum, string bankname, string OnlineBankAccountName, string BankTransactionNo, byte[] imagefile,string imageName)
{
int id=-1;
try {
MemoryStream ms = new MemoryStream(imagefile);
FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
("~/depositSlipScans/") + imageName, FileMode.Create);
ms.WriteTo(fs);
DatabaseFunctions db = new DatabaseFunctions();
Enumber = utilities.Encrypt(Enumber);
Formtype = utilities.Encrypt(Formtype);
amountpaid= utilities.Encrypt(amountpaid);
trnum =utilities.Encrypt(trnum);
bankname = utilities.Encrypt(bankname);
OnlineBankAccountName = utilities.Encrypt(OnlineBankAccountName);
BankTransactionNo = utilities.Encrypt(BankTransactionNo);
imageName = utilities.Encrypt(imageName);
id= db.InserteWalletDepositForm(Enumber,Formtype,paymentdate,amountpaid,trnum,bankname,OnlineBankAccountName,BankTransactionNo,imageName);
ms.Close();
fs.Close();
fs.Dispose();
}
catch (Exception e) {
}
return id;
}
示例3: CreateSharedBackingObject
private static FileStream CreateSharedBackingObject(Interop.libc.MemoryMappedProtections protections, long capacity)
{
Directory.CreateDirectory(s_tempMapsDirectory);
string path = Path.Combine(s_tempMapsDirectory, Guid.NewGuid().ToString("N"));
FileAccess access =
(protections & (Interop.libc.MemoryMappedProtections.PROT_READ | Interop.libc.MemoryMappedProtections.PROT_WRITE)) != 0 ? FileAccess.ReadWrite :
(protections & (Interop.libc.MemoryMappedProtections.PROT_WRITE)) != 0 ? FileAccess.Write :
FileAccess.Read;
// Create the backing file, then immediately unlink it so that it'll be cleaned up when no longer in use.
// Then enlarge it to the requested capacity.
const int DefaultBufferSize = 0x1000;
var fs = new FileStream(path, FileMode.CreateNew, TranslateProtectionsToFileAccess(protections), FileShare.ReadWrite, DefaultBufferSize);
try
{
Interop.CheckIo(Interop.Sys.Unlink(path));
fs.SetLength(capacity);
}
catch
{
fs.Dispose();
throw;
}
return fs;
}
示例4: SetIconSprite
//load by io
public void SetIconSprite(string name)
{
string [email protected]"/Sprite Atlas/Icon/"[email protected]".png";
//Debug.Log(path);
FileStream fileSteam = new FileStream(path, FileMode.Open, FileAccess.Read);
fileSteam.Seek(0, SeekOrigin.Begin);
byte[] bytes = new byte[fileSteam.Length];
fileSteam.Read(bytes, 0, (int)fileSteam.Length);
fileSteam.Close();
fileSteam.Dispose();
fileSteam = null;
//create textures
int width = 128;
int height = 128;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
//create sprite
Sprite _sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
sprite.sprite = _sprite;
}
示例5: LoadTheScore
public static int LoadTheScore()
{
#if UNITY_STANDALONE || UNITY_EDITOR
string fullpath = Path.Combine(Application.dataPath, HighScoreFilename);
#elif UNITY_IPHONE
string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5);
path = path.Substring(0, path.LastIndexOf('/'));
string fullpath = path + "/Documents/"+HighScoreFilename;
#elif UNITY_ANDROID
string fullpath = Path.Combine(Application.persistentDataPath , HighScoreFilename);
#endif
using (FileStream fs = new FileStream(fullpath, FileMode.OpenOrCreate, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
try
{
highscore = br.ReadInt32();
//Debug.Log("Highscore " + highscore);
}
catch(EndOfStreamException e)
{
Debug.Log("{0} caught and ignored. " + "Using default values."+ e.Message+" "+e.StackTrace);
}
finally
{
br.Close();
}
}
fs.Dispose();
}
return (highscore);
}
示例6: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
string stringremove = "bakkupavankumarsonofapparao";
string path = Server.MapPath(string.Format("~/inventory/{0}.json", "lastfile"));
if (!File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
StreamWriter str11 = new StreamWriter(fs);
str11.WriteLine(stringremove);
str11.Flush();
str11.Close();
fs.Dispose();
fs.Close();
}
else if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
StreamWriter str12 = new StreamWriter(fs);
str12.WriteLine(stringremove);
str12.Flush();
str12.Close();
fs.Dispose();
fs.Close();
}
Response.Clear();
Response.CacheControl = "no-cache";
Response.ContentType = "application/json";
Response.Write("hi" + path);
Response.End();
}
示例7: word2Html
public string word2Html(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
{
string filePath = uploadWord(wordFilePath);
Stream f = new FileStream(filePath, FileMode.Open);
Document doc = new Document(f);
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
//获得要保存的文件路径
string serverFileName = _datePath + filename;
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath("~/upload/" + _datePath);
//检查是否有该路径没有就创建
if (!Directory.Exists(toFileFullPath)) {
Directory.CreateDirectory(toFileFullPath);
}
//被转换的html文档保存的位置
string ConfigPath = HttpContext.Current.Server.MapPath("~/upload/" + serverFileName + ".htm");
doc.Save(ConfigPath, SaveFormat.Html);
Thread.Sleep(3000);//
f.Dispose();
this.GetHtmText(ConfigPath);
File.Delete(filePath);
File.Delete(ConfigPath);
return ("/html");
}
示例8: InvalidArgs_Throws
public void InvalidArgs_Throws(bool useAsync)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
{
Assert.Throws<ArgumentNullException>("destination", () => { fs.CopyToAsync(null); });
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => { fs.CopyToAsync(new MemoryStream(), 0); });
Assert.Throws<NotSupportedException>(() => { fs.CopyToAsync(new MemoryStream(new byte[1], writable: false)); });
fs.Dispose();
Assert.Throws<ObjectDisposedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
{
fs.SafeFileHandle.Dispose();
Assert.Throws<ObjectDisposedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
Assert.Throws<NotSupportedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
using (FileStream src = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
using (FileStream dst = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
{
dst.Dispose();
Assert.Throws<ObjectDisposedException>(() => { src.CopyToAsync(dst); });
}
}
示例9: Main
static void Main()
{
string fileWithoutLineNumbers = @"../../../UsingFiles/03.WithoutLineNumbers.txt";
FileStream fs = new FileStream(fileWithoutLineNumbers, FileMode.Create);
fs.Dispose();
string fileWithLineNumbers = @"../../../UsingFiles/03.WithLineNumbers.txt";
FileStream firstFile = new FileStream(fileWithLineNumbers, FileMode.Create);
firstFile.Dispose();
using (StreamWriter writeFirstFile = new StreamWriter(fileWithoutLineNumbers, false))
{
for (int i = 0; i < 10; i++)
{
writeFirstFile.WriteLine("line");
}
}
using (StreamWriter writeNewFile = new StreamWriter(fileWithLineNumbers, false))
{
using (StreamReader readFile = new StreamReader(fileWithoutLineNumbers))
{
int lineCounter = 1;
string str = readFile.ReadLine();
while (str != null)
{
writeNewFile.WriteLine("{0} - {1}", lineCounter, str);
str = readFile.ReadLine();
lineCounter++;
}
}
}
Console.WriteLine("Job is DONE!");
}
示例10: PosTest1
public bool PosTest1()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest1: Make sure Dispose can be called");
try
{
Stream stream = new FileStream(c_READ_FILE_NAME, FileMode.OpenOrCreate);
stream.Dispose();
try
{
stream.ReadByte();
TestLibrary.TestFramework.LogError("001.1", "Call Dispose takes no effect");
retVal = false;
}
catch (ObjectDisposedException)
{
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
return retVal;
}
示例11: Main
static void Main()
{
string input = @"../../../UsingFiles/Input.txt";
string output = @"../../../UsingFiles/Output.txt";
FileStream inputFile = new FileStream(input, FileMode.Create);
inputFile.Dispose();
FileStream outputFile = new FileStream(output, FileMode.Create);
outputFile.Dispose();
using (StreamWriter writeInputFile = new StreamWriter(input, false))
{
writeInputFile.WriteLine("4");
writeInputFile.WriteLine("2 3 3 4");
writeInputFile.WriteLine("0 2 3 4");
writeInputFile.WriteLine("3 7 1 2");
writeInputFile.WriteLine("4 3 3 2");
}
using (StreamReader readInputFile = new StreamReader(input))
{
string sizeOfMatrix = readInputFile.ReadLine();
int n = int.Parse(sizeOfMatrix);
int[,] matrix = new int[n, n];
for (int i = 0; i < n; i++)
{
string line = readInputFile.ReadLine();
string[] lines = line.Split(' ');
for (int j = 0; j < n; j++)
{
matrix[i, j] = int.Parse(lines[j]);
}
}
int bestSum = int.MinValue;
for (int i = 0; i < n - 1; i++)
{
int tempSum = 0;
for (int j = 0; j < n - 1; j++)
{
tempSum = matrix[i, j] + matrix[i + 1, j] + matrix[i, j + 1] + matrix[i + 1, j + 1];
if (tempSum > bestSum)
{
bestSum = tempSum;
}
}
}
using (StreamWriter writeResult = new StreamWriter(output, false))
{
writeResult.WriteLine(bestSum);
}
}
Console.WriteLine("Job is DONE!");
}
示例12: ReadDisposedThrows
public void ReadDisposedThrows()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.Dispose();
Assert.Throws<ObjectDisposedException>(() => fs.WriteByte(0));
}
}
示例13: FlushAsyncThrowsForDisposedStream
public async Task FlushAsyncThrowsForDisposedStream()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => fs.FlushAsync());
}
}
示例14: FlushThrowsForDisposedStream
public void FlushThrowsForDisposedStream(bool? flushToDisk)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.Dispose();
Assert.Throws<ObjectDisposedException>(() => Flush(fs, flushToDisk));
}
}
示例15: WriteFile
public static void WriteFile(string message)
{
var filename = Process.GetCurrentProcess().Id.ToString() + ".txt";
var stream = new FileStream(filename, FileMode.Append);
var writer = new StreamWriter(stream);
writer.WriteLine(message);
writer.Dispose();
stream.Dispose();
}