Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Can someone help me and show me a way of checking if a file is open such that i don't open the same file that is open...may be display a notification that the file is already open. Am using a Datagrid double click event...
here is my code..
 
private void dgvDocs_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
       {
           Document document;
 
           string path;
           document = (Document)dgvDocs.CurrentRow.DataBoundItem;
           path = document.Documentpath;
 

 
           System.Diagnostics.Process.Start(path);
 
       }
Posted 13-Nov-12 4:46am
Comments
joshrduncan2012 at 13-Nov-12 9:54am
   
try this:
 
Process[] open_procs = Process.GetProcessesByName("nameOfProcess");
if(open_procs.Length > 0)
{
foreach(var proc in open_procs)
{
proc.Kill();
}
}
This kills any open processes you specify.
Code_Linc at 16-Nov-12 9:47am
   
Thanks for all the solutions however the first solution only works on documents yet i want to check for any file types....Please anyone with a solution help!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

protected virtual bool IsFileinUse(FileInfo file)
{
     FileStream stream = null;
 
     try
     {
         stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
     }
     catch (IOException)
     {
         //the file is unavailable because it is:
         //still being written to
         //or being processed by another thread
         //or does not exist (has already been processed)
         return true;
     }
     finally
     {
         if (stream != null)
         stream.Close();
     }
     return false; 
}
 
Source: 1st Answer in this thread.[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100