Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Deleting a file

Deleting a file

Scheduled Pinned Locked Moved C#
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saqib82
    wrote on last edited by
    #1

    How can i delete a file created by FileStream

    sAqIb

    J 1 Reply Last reply
    0
    • S saqib82

      How can i delete a file created by FileStream

      sAqIb

      J Offline
      J Offline
      Jakob Farian Krarup
      wrote on last edited by
      #2

      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

      C S 2 Replies Last reply
      0
      • J Jakob Farian Krarup

        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

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • J Jakob Farian Krarup

          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

          S Offline
          S Offline
          saqib82
          wrote on last edited by
          #4

          thanks buddy. System.IO.File.Delete("path"); worked for me.

          sAqIb

          J 1 Reply Last reply
          0
          • C Christian Graus

            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

            J Offline
            J Offline
            Jakob Farian Krarup
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S saqib82

              thanks buddy. System.IO.File.Delete("path"); worked for me.

              sAqIb

              J Offline
              J Offline
              Jakob Farian Krarup
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups