Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8635637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:01:25+00:00 2026-06-12T10:01:25+00:00

I would like to open a file, and if it does not exist create

  • 0

I would like to open a file, and if it does not exist create it, similar to this question

The catch is that if the file was newly created I perform some additional initialization logic.

I can use

await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists)

to open the file if it already exists, but how can I tell if the result of this operation was a newly created file or just opening an existing file? Is there a best practice here or am I stuck looking at the file create date or file size?

Ideally the code would look something like this:

var file = await folder.CreateFileAsync(fileName, CreationCollsionOption.OpenIfExists);

//How can I reliably tell if the file was new?
if (fileIsNew)
  await InitializeFile(file);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:01:26+00:00Added an answer on June 12, 2026 at 10:01 am

    With using the CreationCollsionOption.OpenIfExists there is no way to tell what’s happened other than looking the CreatedDate or the Size of the file.

    So I think you are better if you use the GetFileAsync (like in the linked question) and do your initilaize logic in the catch FileNotFoundException branch, because there is no File.Exists in WinRt

    However there is a third solution where you enumerate the files in the directory and check for existence by hand:

    var files = await folder.GetFilesAsync(CommonFileQuery.OrderByName);
    var file = files.FirstOrDefault(x => x.Name == fileName);
    if (file == null)
    {
        file = await GetFilesAsync(fileName);
        await InitializeFile(file);    
    }
    

    Note: in this case potentially you have to prepare to handle race conditions becase it can happen while you are processing the file list somebody creates the file before you (that’s why there is no File.Exists method exists)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to open an OleDbConnection to an Excel file that's in memory
I would like to to create a 2-step file uploader: Open dialog. Select one
If I do (e.g.) open(/snafu/fnord) in Python (and the file does not exist), I
I would like to open a file using open file dialog. If the opened
I have an WinMo app and I would like to open a file from
i dont have microsoft access but would like to open an mdb file, is
I would like to delete every double space in a file open in vim,
I would like to know if a file is open by any process (including
I'm would like to know how I can open a specific file using a
I would like the designer to be able to open the same solution file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.