Deleting a file
-
You can delete all files in the same way:
System.IO.File.Delete("path");
When you ask specifically about files created by a FileStream, then I guess you have a problem with the file being locked. This can happen if an exception occurs while you are working with the filestream, or if you haven't released the file from the stream again before deleting it. You should always use Structured Exception Handling when using I/O operations in code:
using System;
using System.IO;namespace Testing
{
public class Test
{public static void Main() { FileStream fs = null; try { fs = new FileStream(@"c:\\test.txt", FileMode.Create); byte\[\] content = new System.Text.UTF8Encoding(true).GetBytes("Hi there!"); fs.Write(content, 0, 9); } catch (IOException ioe) { Console.WriteLine("Something happened"); if(fs != null) fs.Close(); } }
}
}In this case the file will always be closed, and you should be able to delete it.
Kind regards - Jakob :cool: ********************************************* Three kinds of people in the world: - Those who can count.. - Those who can't! 10 kinds of people in the world: - Those who understand binary - Those who don't
-
You can delete all files in the same way:
System.IO.File.Delete("path");
When you ask specifically about files created by a FileStream, then I guess you have a problem with the file being locked. This can happen if an exception occurs while you are working with the filestream, or if you haven't released the file from the stream again before deleting it. You should always use Structured Exception Handling when using I/O operations in code:
using System;
using System.IO;namespace Testing
{
public class Test
{public static void Main() { FileStream fs = null; try { fs = new FileStream(@"c:\\test.txt", FileMode.Create); byte\[\] content = new System.Text.UTF8Encoding(true).GetBytes("Hi there!"); fs.Write(content, 0, 9); } catch (IOException ioe) { Console.WriteLine("Something happened"); if(fs != null) fs.Close(); } }
}
}In this case the file will always be closed, and you should be able to delete it.
Kind regards - Jakob :cool: ********************************************* Three kinds of people in the world: - Those who can count.. - Those who can't! 10 kinds of people in the world: - Those who understand binary - Those who don't
Actually, this code won't work. Catch only runs when an exception occurs, I believe you meant to put the fs.Close code in a finally block ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
You can delete all files in the same way:
System.IO.File.Delete("path");
When you ask specifically about files created by a FileStream, then I guess you have a problem with the file being locked. This can happen if an exception occurs while you are working with the filestream, or if you haven't released the file from the stream again before deleting it. You should always use Structured Exception Handling when using I/O operations in code:
using System;
using System.IO;namespace Testing
{
public class Test
{public static void Main() { FileStream fs = null; try { fs = new FileStream(@"c:\\test.txt", FileMode.Create); byte\[\] content = new System.Text.UTF8Encoding(true).GetBytes("Hi there!"); fs.Write(content, 0, 9); } catch (IOException ioe) { Console.WriteLine("Something happened"); if(fs != null) fs.Close(); } }
}
}In this case the file will always be closed, and you should be able to delete it.
Kind regards - Jakob :cool: ********************************************* Three kinds of people in the world: - Those who can count.. - Those who can't! 10 kinds of people in the world: - Those who understand binary - Those who don't
-
Actually, this code won't work. Catch only runs when an exception occurs, I believe you meant to put the fs.Close code in a finally block ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Hehe... that's what you get for replying to a forum post before the first cup'o'mocca in the morning :-) You're right of course ;-) Corrected code reads:
using System;
using System.IO;
namespace Testing{
public class Test
{
public static void Main()
{
FileStream fs = null;
try
{
fs = new FileStream(@"c:\test.txt", FileMode.Create);
byte[] content = new System.Text.UTF8Encoding(true).GetBytes("Hi there!");
fs.Write(content, 0, 9);
}
catch (IOException ioe)
{
Console.WriteLine("Something happened");
}
finally
{
if(fs != null)
fs.Close();
}
}
}
}Thx for the correction Christian!
Kind regards - Jakob :cool: ********************************************* Three kinds of people in the world: - Those who can count.. - Those who can't! 10 kinds of people in the world: - Those who understand binary - Those who don't
-
Great! ;-)
Kind regards - Jakob :cool: ********************************************* Three kinds of people in the world: - Those who can count.. - Those who can't! 10 kinds of people in the world: - Those who understand binary - Those who don't