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

  • Home
  • SEARCH
  • 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 931027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:26:07+00:00 2026-05-15T20:26:07+00:00

I using C# .NET , vs 2008 , .net 3.5 For me, is difficult,

  • 0

I using C# .NET , vs 2008 , .net 3.5

For me, is difficult, but I need sample code in C# for this:

  1. Check if a file or a folder is in use

  2. If file or a folder is in use, the name of Process that use it

For example, in my issue.

I try delete file, and I get “The process cannot access the file ‘XYZ’ because it is being used by another process.” Exception.

File.Delete(infoFichero.Ruta);

I want check if a file is in use, and the name of Process that use it.

I need sample code, source code, please. I dont want use c++, I dont know c, c++, unmanaged code, or WinApi. I want use only C# code (managed code .net).

I have read several references but not get sample code source,

How to check if a file is in use?

Emulate waiting on File.Open in C# when file is locked

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9dabc172-237a-42db-850e-ada08885a5d5

How to check if a file is in use?

Easiest way to read text file which is locked by another application

Using C# is it possible to test if a lock is held on a file

EDIT:
From Yan Jun – MSFT

  string path = "D:\\temp2.xlsx";
            foreach (Process c in Process.GetProcesses()) {

                if (c.MainWindowTitle.Contains(Path.GetFileName(path))){
                    MessageBox.Show(c.ProcessName);
                    return;
                }
            }

            try{
                FileInfo f = new FileInfo(path);

                f.Delete();
            }
            catch (Exception ex){

                MessageBox.Show(ex.Message);
            }

…
But it is difficult get solution for all 100% issues.

  1. Problem if c.MainWindowTitle == null or not contains filename.

  2. Problem for shared folder in another machine, PC, server,… like:

File.Delete(@\desiis\TEmporal\Project\script.targets);

any sample code, I ask for help gurus, MVPs, anyone.

UPDATE: the same issue for a folder

  • 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-05-15T20:26:08+00:00Added an answer on May 15, 2026 at 8:26 pm

    There’s not going to be a way to find the process that has the file opened without stepping into the WinApi, I don’t think. And as far as checking whether its in use, the only thing you can really do, as the SO questions you linked to state, is to wrap the file access attempts in a try/catch block.

    The code to find which file has it opened is likely to be ugly, but there may be an API out there that wraps this up nicely. There are 3rd party utilities that will tell you this (Unlocker being the best known example). You can also use ProcessExplorer to search for open file handles by the filename. Those don’t really help you though.

    The short answer of what I’m trying to get across here is you have the answer for the first part of your question in the SO questions you already linked, and the second part would probably require WIN32 calls, which you want to avoid, but you’re probably going to have to get your hands dirty in Win32… Still want help?

    EDIT: You could shell out to sysinternals Handle utility. You would need to get the output of that command and parse it yourself. You can read the executed process’s output like this

    string result = proc.StandardOutput.ReadToEnd();
    

    The issue with this is you’re going to get a license agreement popup the first time you run the Handle utility. Not to mention the whole licensing issues if this is something you hope to deploy…

    If you’re still interested, I can show you how you’d go about this.

    EDIT: Here’s a runnable program that will find the exe name and pid of any program that has an open handle to a file. I added comments, but can elaborate further if necessary. I use Regular Expressions here to parse the output as that makes the most sense given the task at hand.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ProcessStartInfo si = new ProcessStartInfo();
                si.FileName = "handle.exe";     //name of the handle program from sysinternals
                                                //assumes that its in the exe directory or in your path 
                                                //environment variable
    
                //the following three lines are required to be able to read the output (StandardOutput)
                //and hide the exe window.
                si.RedirectStandardOutput = true;
                si.WindowStyle = ProcessWindowStyle.Hidden;
                si.UseShellExecute = false;
    
                si.Arguments = "test.xlsx";     //this is the file you're trying to access that is locked
    
                //these 4 lines create a process object, start it, then read the output to 
                //a new string variable "s"
                Process p = new Process();
                p.StartInfo = si;
                p.Start();
                string s = p.StandardOutput.ReadToEnd();
    
                //this will use regular expressions to search the output for process name
                //and print it out to the console window
                string regex = @"^\w*\.EXE";
                MatchCollection matches = Regex.Matches(s, regex, RegexOptions.Multiline);
                foreach (var match in matches)
                {
                    Console.WriteLine(match);
                }
    
                //this will use regex to search the output for the process id (pid)
                //and print it to the console window.
                regex = @"pid: (?<pid>[0-9]*)";
                matches = Regex.Matches(s, regex, RegexOptions.Multiline);
                foreach (var obj in matches)
                {
                    Match match = (Match)obj;   //i have to cast to a Match object
                                                //to be able to get the named group out
                    Console.WriteLine(match.Groups["pid"].Value.ToString());
                }
    
                Console.Read();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 479k
  • Answers 479k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Start with the Event Viewer. Does the Silverlight stand alone… May 16, 2026 at 5:43 am
  • Editorial Team
    Editorial Team added an answer I would guess that the original SMB protocol does not… May 16, 2026 at 5:43 am
  • Editorial Team
    Editorial Team added an answer When creating an emulator u can set the properties, and… May 16, 2026 at 5:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.