Click here to Skip to main content
15,895,606 members
Home / Discussions / C#
   

C#

 
GeneralRe: Intercept Print Jobs From Other Apps Pin
johnstacey25-Jul-04 13:34
johnstacey25-Jul-04 13:34 
GeneralRe: Intercept Print Jobs From Other Apps Pin
Dave Kreskowiak25-Jul-04 15:54
mveDave Kreskowiak25-Jul-04 15:54 
GeneralRe: Intercept Print Jobs From Other Apps Pin
johnstacey4-Aug-04 0:22
johnstacey4-Aug-04 0:22 
GeneralRe: Intercept Print Jobs From Other Apps Pin
Small Rat24-Jul-04 5:55
Small Rat24-Jul-04 5:55 
GeneralSave text file as html Pin
kloepper23-Jul-04 11:40
kloepper23-Jul-04 11:40 
GeneralC# services security Pin
TalkingBabb0t23-Jul-04 10:13
TalkingBabb0t23-Jul-04 10:13 
GeneralFilewatcher, additional need to monitor User making changes Pin
Ruchi Gupta23-Jul-04 6:06
Ruchi Gupta23-Jul-04 6:06 
GeneralRe: Filewatcher, additional need to monitor User making changes Pin
Heath Stewart23-Jul-04 9:23
protectorHeath Stewart23-Jul-04 9:23 
The FileSystemWatcher component - nor the underlying ReadDirectoryChangesW API - record this information. In fact, IIRC, NTFS filesystem doesn't even record this information. There's no entries in it for the journal, at least (NTFS is now a journaling file system).

If you want to see who's got what files open (which may help indicate who changed what) and since this is on a server (so I assume people are accessing files via a UNC share), you could P/Invoke NetFileEnum. I've included some code below that can help and that I wrote some time back to assist with closing open file handles (for executables) in our shared build directory. I make no warranty of it in any way:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Security;

namespace Example
{
  public class NetFiles
  {
    private const int ErrorFileNotFound = 2;
    private const int ErrorAccessDenied = 5;
    private const int ErrorMoreData = 234;
    private const int MaxPreferredLength = -1; // Get all records
    private const int BufferSize = 4000; // (sizeof(FileInfo) == 160) * 25 < 4096 (under 4 kb)

    [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();

      // Enum the files on the server with the path and username.
      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 each address, marshal the struct and add to the list.
        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);
          }
        }

        // Free the buffer.
        NetApiBufferFree(buffer);

        // If more data is available, get it and repeat.
        if (retVal == ErrorMoreData)
          retVal = NetFileEnum(server, basePath, username, 3, out buffer,
            MaxPreferredLength, out entriesRead, out totalEntries,
              ref resumeHandle);
        else break;
      }

      // Copy the ArrayList to an array and return it.
      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);
    }
  }

  [Serializable]
  [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
  }
}
This information - combined with what you track using the FileSystemWatcher, could help you make adequate guesses as to who changed what file.

 

Microsoft MVP, Visual C#
My Articles
GeneralEmergency. problems in serial communication Pin
wk_vigorous23-Jul-04 5:50
wk_vigorous23-Jul-04 5:50 
GeneralRe: Emergency. problems in serial communication Pin
Heath Stewart23-Jul-04 9:28
protectorHeath Stewart23-Jul-04 9:28 
GeneralRe: Emergency. problems in serial communication Pin
Nick Parker23-Jul-04 11:08
protectorNick Parker23-Jul-04 11:08 
GeneralRe: Emergency. problems in serial communication Pin
Heath Stewart23-Jul-04 11:12
protectorHeath Stewart23-Jul-04 11:12 
GeneralNeed handle to ComboBox dropdown button Pin
ddelapasse23-Jul-04 4:20
ddelapasse23-Jul-04 4:20 
GeneralRe: Need handle to ComboBox dropdown button Pin
Heath Stewart23-Jul-04 6:17
protectorHeath Stewart23-Jul-04 6:17 
GeneralRe: Need handle to ComboBox dropdown button Pin
ddelapasse23-Jul-04 6:48
ddelapasse23-Jul-04 6:48 
GeneralRe: Need handle to ComboBox dropdown button Pin
Nick Parker23-Jul-04 7:58
protectorNick Parker23-Jul-04 7:58 
GeneralRe: Need handle to ComboBox dropdown button Pin
Heath Stewart23-Jul-04 8:39
protectorHeath Stewart23-Jul-04 8:39 
GeneralDirecroty search Pin
Ronni Marker23-Jul-04 4:03
Ronni Marker23-Jul-04 4:03 
GeneralRe: Direcroty search Pin
Colin Angus Mackay23-Jul-04 4:54
Colin Angus Mackay23-Jul-04 4:54 
GeneralReturning objects to another class Pin
Paul Kiddie23-Jul-04 1:19
Paul Kiddie23-Jul-04 1:19 
GeneralRe: Returning objects to another class Pin
Heath Stewart23-Jul-04 9:05
protectorHeath Stewart23-Jul-04 9:05 
Generalcapturing the modifications in a datagrid Pin
samithas23-Jul-04 0:29
samithas23-Jul-04 0:29 
GeneralRe: capturing the modifications in a datagrid Pin
Heath Stewart23-Jul-04 8:59
protectorHeath Stewart23-Jul-04 8:59 
GeneralRe: capturing the modifications in a datagrid Pin
samithas23-Jul-04 19:35
samithas23-Jul-04 19:35 
GeneralRe: capturing the modifications in a datagrid Pin
Heath Stewart26-Jul-04 2:25
protectorHeath Stewart26-Jul-04 2:25 

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.