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 8435233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:51:36+00:00 2026-06-10T06:51:36+00:00

In my application objects created by user can be serialized and saved as file.

  • 0

In my application objects created by user can be serialized and saved as file. User can also open them and continue working on them (Just like a project file!). I noticed that I can delete files when they are opened by the program, by that I mean I just deserialize the files into objects so application doesn’t care about the files anymore.

But how can I keep that file busy while my application is running and that file is loaded into program? Here is my Save and Load methods:

public static bool SaveProject(Project proj, string pathAndName)
{
    bool success = true;
    proj.FileVersion = CurrentFileVersion;

    try
    {
        using (var stream = new FileStream(pathAndName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            using(var zipper = new ZlibStream(stream, CompressionMode.Compress, CompressionLevel.BestCompression, true))
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(zipper, proj);
            }
        }

    }
    catch (Exception e)
    {
        MessageBox.Show("Can not save project!" + Environment.NewLine + "Reason: " + e.Message, "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        success = false;
    }

    return success;
}

public static Project LoadProject(string path)
{
    try
    {
        using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            using (var zipper = new ZlibStream(stream, CompressionMode.Decompress))
            {
                var formatter = new BinaryFormatter();
                var obj = (Project)formatter.Deserialize(zipper);

                if (obj.FileVersion != CurrentFileVersion)
                {
                    MessageBox.Show("Can not load project!" + Environment.NewLine + "Reason: File version belongs to an older version of the program." +
                        Environment.NewLine + "File version: " + obj.FileVersion +
                        Environment.NewLine + "Current version: " + CurrentFileVersion,
                        "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //throw new InvalidFileVersionException("File version belongs to an older version of the program.");
                }

                return obj;
            }
        }
    }
    catch (Exception ex)
    {

        MessageBox.Show("Can not load project!" + Environment.NewLine + "Reason: " + ex.Message, "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }

    return null;
}

So what can I do? How to make a fake handle for the file? I mean how to make it so if someone tries to delete the file, windows prevent it by that ‘file is being used…` message?

  • 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-10T06:51:37+00:00Added an answer on June 10, 2026 at 6:51 am

    Don’t do this. If the user intentionally deletes the file then he’ll have a good reason for it.

    If you prevent this by keeping a lock on the file then he’ll simply kill your app and delete the file anyway. All you accomplished is creating an inconvenience, the kind that drives users nuts and gives them a reason to run the uninstaller. If you need to know that the file got deleted while your program is running then you can find out with FileSystemWatcher.

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

Sidebar

Related Questions

In my application the user can create multiple objects (so called drawings) each of
I've created a graphing application that calls a web service. The user can zoom
My application creates custom URIs (or URLs?) to identify objects and resolve them. The
I have created an ASP.net application with several aspx. Some of them are used
Application: This is a workshop proposal system for a conference. A user can create
I am working on a c# application that is serially encrypted when user install
I've developed a nice rich application interface using Backbone.js where users can add objects
I'm working on an interactive web application, currently set up on http://picselbocs.com/projects/goalcandy (user: demo@demo.com,
I am working on an iPhone application in which I have HTML pages created
I am working in an application that is mostly single-thread, single user. There are

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.