create a new file with exclusive owner mode

This is a discussion on create a new file with exclusive owner mode within the C# Programming forums, part of the General Programming Boards category; Hello everyone, I did some research for File.Create API to find some option which could be used to create a ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    create a new file with exclusive owner mode

    Hello everyone,


    I did some research for File.Create API to find some option which could be used to create a new file, with exclusive owner mode (e.g. other process/thread can not delete/open/modify when current thread keep the file open). But can not find it out.

    Could anyone let me know which option should I use or which API should I use?


    thanks in advance,
    George

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Code:
    FileStream newFile = File.Open("newfile.txt", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);
    It's the FileShare.None that prohibits any other process (or even other attempts to open it within the same process) from getting access to the file.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Considering the fact that this info is available just one click from the MSDN page about the File class, maybe you should spend more time learning how to look things up instead of learning C/C++ or C#. In the end, it will be a tremendous help learning ANY language.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Cat,


    I have found it out. I think I need to FileStream.Close(), or else other thread/process can not use it?

    Quote Originally Posted by Cat View Post
    Code:
    FileStream newFile = File.Open("newfile.txt", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);
    It's the FileShare.None that prohibits any other process (or even other attempts to open it within the same process) from getting access to the file.

    regards,
    George

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yes, you can .Close(), or you could put all access inside a using statement, like:

    Code:
    using (FileStream newFile = File.Open("newfile.txt", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
    {
    // Use the file here
    }
    Also you could do nothing and just wait until newFile gets garbage collected, but that only happens when the garbage collector decides to do it (which could be right after the last pointer is destroyed, or it could be much, much later).
    Last edited by Cat; 04-25-2008 at 04:58 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Cat,


    Question answered. Cool!!

    Quote Originally Posted by Cat View Post
    Yes, you can .Close(), or you could put all access inside a using statement, like:

    Code:
    using (FileStream newFile = File.Open("newfile.txt", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
    {
    // Use the file here
    }
    Also you could do nothing and just wait until newFile gets garbage collected, but that only happens when the garbage collector decides to do it (which could be right after the last pointer is destroyed, or it could be much, much later).

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. Saving with CFile (how)
    By jinx in forum Windows Programming
    Replies: 1
    Last Post: 10-18-2001, 10:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21