[C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

Results 1 to 9 of 9
  1. #1
    Enthusiast Niv123123 is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    Well, i made in C# that it will create a file, then to delete it, and then to create the file again, and when it have to create the file again, it says that the file already being used by another procces (maybe C# using it)
    so what to do? how to refresh the project that the project won't use it?
    and more question, how can i change the name of a file?


  2. #2
    Enthusiast doqunbop is offline
    MemberRank
    Nov 2008 Join Date
    35Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    Can you post the code, there probably is something in it that should be changed.

  3. #3
    Enthusiast Niv123123 is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                FileInfo file1 = new FileInfo(@"C:\Text12341234");
                file1.CreateText();
                Console.WriteLine("Created.");
                file1.Delete();
                Console.WriteLine("Deleted.");
            }
        }
    }
    and at:
    file1.Delete();
    it says that it already use by another procces.
    please help

  4. #4
    Enthusiast doqunbop is offline
    MemberRank
    Nov 2008 Join Date
    35Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    Try "file1.Close();" before deleting.

  5. #5
    Enthusiast Niv123123 is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    "FileInfo does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type FileInfo could be found (are you missing a using directive or an assembly reference?)"
    I can't use Close() for FileInfo.. please help

  6. #6
    Enthusiast doqunbop is offline
    MemberRank
    Nov 2008 Join Date
    35Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    Oh, didn't notice it was of type FileInfo... Anyway, try this, I think it would work:
    Code:
    FileInfo file1 = new FileInfo (@"C:\Text12341234");
    StreamWriter sw = file1.CreateText ();
    Console.WriteLine ("Created.");
    
    sw.Close ();
    file1.Delete ();
    Console.WriteLine ("Deleted.");
    The method "CreateText" opens the file, preparing it for writing, and returns a StreamWriter, which can be used to write contents to the file. If you don't close it through StreamWriter, then the file will be marked as still in use.

  7. #7
    Enthusiast Niv123123 is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    yeh thats what i did.
    working now..
    1 more thing, how can i change the name of a file?

  8. #8
    Enthusiast doqunbop is offline
    MemberRank
    Nov 2008 Join Date
    35Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    Code:
    file1.MoveTo ("C:/ANewName.txt");
    Just make sure the file isn't open for writing or is closed before attempting to "move" it. (Moving the file can be understood as moving the contents to a new file)

  9. #9
    Enthusiast Niv123123 is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    Re: [C#] [NEED HELP] :[ (FileInfo) how to fix "being used by another procces"?

    Thanks you!
    you can close this thread..



Advertisement