Click here to Skip to main content
15,892,537 members
Home / Discussions / C#
   

C#

 
Generalc# opengl *.obj Pin
pertiu11-Mar-04 7:07
pertiu11-Mar-04 7:07 
GeneralRe: c# opengl *.obj Pin
Judah Gabriel Himango11-Mar-04 16:39
sponsorJudah Gabriel Himango11-Mar-04 16:39 
GeneralRe: c# opengl *.obj Pin
Heath Stewart12-Mar-04 3:32
protectorHeath Stewart12-Mar-04 3:32 
GeneralOOP - Multiple Inheritance Pin
MrEyes11-Mar-04 7:07
MrEyes11-Mar-04 7:07 
GeneralRe: OOP - Multiple Inheritance Pin
John Fisher11-Mar-04 16:24
John Fisher11-Mar-04 16:24 
GeneralRe: OOP - Multiple Inheritance Pin
Michael Flanakin17-Mar-04 19:52
Michael Flanakin17-Mar-04 19:52 
Question Determine which users have a file locked? Pin
Member 59390311-Mar-04 6:54
Member 59390311-Mar-04 6:54 
AnswerRe: Determine which users have a file locked? Pin
Heath Stewart12-Mar-04 3:26
protectorHeath Stewart12-Mar-04 3:26 
If you're talking about those files that are open across network shares, you can use the following code since WMI doesn't expose these handles (that I can find or already know about):
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct FileInfo
{
	[MarshalAs(UnmanagedType.U4)]public int ID;
	[MarshalAs(UnmanagedType.U4)]public FilePermissions Permissions;
	[MarshalAs(UnmanagedType.U4)]public int Locks;
	[MarshalAs(UnmanagedType.LPWStr)]public string Path;
	[MarshalAs(UnmanagedType.LPWStr)]public string Username;
}
[Flags]
public enum FilePermissions
{
	Read = 1,
	Write = 2,
	Create = 4
}
public class NetFiles
{
  private const int ErrorFileNotFound = 2;
  private const int ErrorAccessDenied = 5;
  private const int ErrorMoreData = 234;
  private const int MaxPreferredLength = -1;
  private const int BufferSize = 4000;

  [DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
  private static extern int NetFileEnum
  (
    [MarshalAs(UnmanagedType.LPWStr)]string serverName,
    [MarshalAs(UnmanagedType.LPWStr)]string basePath,
    [MarshalAs(UnmanagedType.LPWStr)]string userName,
    [MarshalAs(UnmanagedType.U4)]int level,
    [MarshalAs(UnmanagedType.SysUInt), Out]out IntPtr buffer,
    [MarshalAs(UnmanagedType.U4)]int preferredMaxLength,
    [MarshalAs(UnmanagedType.U4), Out]out int entriesRead,
    [MarshalAs(UnmanagedType.U4), Out]out int totalEntries,
    [MarshalAs(UnmanagedType.U4), In, Out]ref int resumeHandle
  );

  [DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
  private static extern int NetFileClose
  (
    [MarshalAs(UnmanagedType.LPWStr)]string serverName,
    [MarshalAs(UnmanagedType.U4)]int fileID
  );

  [DllImport("netapi32.dll")]
  private static extern int NetApiBufferFree
  (
    IntPtr buffer
  );
  private NetFiles()
  {
  }
  public static IList GetOpenFiles()
  {
    return GetOpenFiles(null, null, null);
  }
  public static IList GetOpenFilesOnServer(string server)
  {
    return GetOpenFiles(server, null, null);
  }
  public static IList GetOpenFilesWithPath(string basePath)
  {
    return GetOpenFiles(null, basePath, null);
  }
  public static IList GetOpenFilesForUsername(string username)
  {
    return GetOpenFiles(null, null, username);
  }
  public static IList GetOpenFiles(string server, string basePath,
  	string username)
  {
    IntPtr buffer;
    int entriesRead;
    int totalEntries;
    int resumeHandle = 0;
    ArrayList list = new ArrayList();

    int retVal = NetFileEnum(server, basePath, username, 3, out buffer,
      BufferSize, out entriesRead, out totalEntries, ref resumeHandle);
    while ((retVal == 0 || retVal == ErrorMoreData) && buffer != IntPtr.Zero)
    {
      for (int i=0; i<entriesRead; i++)
      {
        IntPtr address = new IntPtr(buffer.ToInt32() + i *
			Marshal.SizeOf(typeof(FileInfo)));
        if (address != IntPtr.Zero)
        {
          FileInfo info = (FileInfo)Marshal.PtrToStructure(address,
		  	typeof(FileInfo));
          list.Add(info);
        }
      }

      NetApiBufferFree(buffer);

      if (retVal == ErrorMoreData)
        retVal = NetFileEnum(server, basePath, username, 3, out buffer,
          MaxPreferredLength, out entriesRead, out totalEntries, ref resumeHandle);
      else break;
    }

    if (retVal == 0) return list;
    else if (retVal == 5) throw new SecurityException("Access denied");
    else throw new System.ComponentModel.Win32Exception(retVal);
  }
  public static void CloseFile(int handle)
  {
    CloseFile(handle, null);
  }
  public static void CloseFile(int handle, string server)
  {
    int retVal = NetFileClose(server, handle);
    if (retVal == 0) return;
    else if (retVal == ErrorAccessDenied)
      throw new SecurityException("Access denied");
    else if (retVal == ErrorFileNotFound)
      throw new System.IO.FileNotFoundException("The file was not found.");
    else throw new System.ComponentModel.Win32Exception(retVal);
  }
}
This is just part of an in-house application I wrote and I offer no warranty of any kind. Use it as an example.

To get open file handles on the local machine is much more difficult and uses a bunch of undocumented APIs. I haven't figured out what exactly to pass for parameters. You can, however, find an application that does this called "Handle" on http://www.sysinternals.com[^].

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Determine which users have a file locked? Pin
Member 59390312-Mar-04 3:34
Member 59390312-Mar-04 3:34 
QuestionRe: Determine which users have a file locked? Pin
Martin#18-Jun-07 0:16
Martin#18-Jun-07 0:16 
Generalthis.ServiceName Pin
CraigSch11-Mar-04 6:42
CraigSch11-Mar-04 6:42 
GeneralRe: this.ServiceName Pin
John Fisher11-Mar-04 16:32
John Fisher11-Mar-04 16:32 
GeneralBindtoObject translation Issues Pin
Tristan Rhodes11-Mar-04 5:39
Tristan Rhodes11-Mar-04 5:39 
General- Solved. - ref GUID? Pin
Tristan Rhodes12-Mar-04 1:02
Tristan Rhodes12-Mar-04 1:02 
GeneralRe: - Solved. - ref GUID? Pin
Mike Dimmick12-Mar-04 2:25
Mike Dimmick12-Mar-04 2:25 
GeneralRe: - Solved. - ref GUID? Pin
Tristan Rhodes12-Mar-04 3:20
Tristan Rhodes12-Mar-04 3:20 
Generalfill a textbox from a dataset Pin
krisman11-Mar-04 4:43
krisman11-Mar-04 4:43 
GeneralRe: fill a textbox from a dataset Pin
Guinness4Strength11-Mar-04 4:47
Guinness4Strength11-Mar-04 4:47 
GeneralRe: fill a textbox from a dataset Pin
krisman11-Mar-04 5:28
krisman11-Mar-04 5:28 
GeneralRe: fill a textbox from a dataset Pin
partyganger11-Mar-04 5:56
partyganger11-Mar-04 5:56 
GeneralRe: fill a textbox from a dataset Pin
Edbert P11-Mar-04 16:22
Edbert P11-Mar-04 16:22 
GeneralRe: fill a textbox from a dataset Pin
krisman15-Mar-04 2:56
krisman15-Mar-04 2:56 
GeneralRe: fill a textbox from a dataset Pin
Edbert P15-Mar-04 11:11
Edbert P15-Mar-04 11:11 
GeneralRe: fill a textbox from a dataset Pin
krisman16-Mar-04 2:51
krisman16-Mar-04 2:51 
GeneralWindows XP and Shell Pin
Guinness4Strength11-Mar-04 4:11
Guinness4Strength11-Mar-04 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.