示例#1
0
        // WinNT only. Win9x this code will not work.
        private static IntPtr OpenHandle(String path)
        {
            String fullPath = Path.GetFullPathInternal(path);
            String root     = Path.GetPathRoot(fullPath);

            if (root == fullPath && root[1] == Path.VolumeSeparatorChar)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_PathIsVolume"));
            }

            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullPath + "\\." }, false, false).Demand();

            IntPtr handle = Win32Native.CreateFile(
                fullPath,
                GENERIC_WRITE,
                FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                Win32Native.NULL,
                OPEN_EXISTING,
                FILE_FLAG_BACKUP_SEMANTICS,
                Win32Native.NULL
                );

            if (handle == Win32Native.INVALID_HANDLE_VALUE)
            {
                int hr = Marshal.GetLastWin32Error();
                __Error.WinIOError(hr, path);
            }
            return(handle);
        }
    public static FileStream Open(string path, FileMode mode, FileAccess access)
    {
        SafeFileHandle handle = Win32Native.CreateFile(String.Concat(@"\\?\", path), (int)0x10000000, FileShare.None, null, mode, (int)0x00000080, IntPtr.Zero);

        if (handle.IsInvalid)
        {
            throw new System.ComponentModel.Win32Exception();
        }
        return(new FileStream(handle, access));
    }
示例#3
0
    public static FileStream OpenForOnlyRead(string path)
    {
        path = GetUNCPath(path);
        SafeFileHandle handle = Win32Native.CreateFile(path, (uint)0x80000000, FileShare.Read, null, FileMode.Open, (int)0x00000080, IntPtr.Zero);

        if (handle.IsInvalid)
        {
            throw new System.ComponentModel.Win32Exception();
        }
        return(new FileStream(handle, FileAccess.Read));
    }
示例#4
0
    public static bool CreateFile(string path)
    {
        SafeFileHandle handle = Win32Native.CreateFile(String.Concat(@"\\?\", path), (int)0x10000000, FileShare.None, null, FileMode.Create, (int)0x00000080, IntPtr.Zero);

        if (handle.IsInvalid)
        {
            return(false);
        }

        return(true);
    }
示例#5
0
    public static bool CreateFile(string path)
    {
        path = GetUNCPath(path);
        SafeFileHandle handle = Win32Native.CreateFile(path, (int)0x10000000, FileShare.None, null, FileMode.Create, (int)0x00000080, IntPtr.Zero);

        if (handle.IsInvalid)
        {
            return(false);
        }

        return(true);
    }
示例#6
0
        public SharpTap(TapProfile Profile)
        {
            this.Profile = Profile;
            if (!this.Profile.IsInstalled)
            {
                throw new Exception("Tuntap driver not installed");
            }
            mTapHandle = Win32Native.CreateFile(this.Profile.UsermodeDeviceSpace + this.Profile.DeviceGUID + ".tap", FileAccess.ReadWrite,
                                                FileShare.ReadWrite, 0, FileMode.Open, 0x4 | 0x40000000, IntPtr.Zero); // FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED 0x40000000 |FILE_FLAG_WRITE_THROUGH 0x80000000
            // rewrite a filestream for async read write with overlap, seprate read write buffer size
            //TapDriver = new FileStream(new SafeFileHandle(mTapHandle, true), FileAccess.ReadWrite, 0xFFFF, true);

            TapDriver = new TapStream(mTapHandle);

            //string path = this.Profile.UsermodeDeviceSpace + this.Profile.DeviceGUID + ".tap";
            //TapDriver = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.WriteThrough);
        }