⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 C#高级编程第6版随书源代码 值得下载
💻 CS
字号:
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.Win32.SafeHandles;

namespace Wrox.ProCSharp.Interop
{

   internal static class NativeMethods
   {
        [DllImport("kernel32.dll", SetLastError=true, EntryPoint="CreateHardLink", CharSet=CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CreateHardLink(string newFileName, string existingFileName, IntPtr securityAttributes);

        internal static void CreateHardLink(string oldFileName, string newFileName)
        {
            if (!CreateHardLink(newFileName, oldFileName, IntPtr.Zero))
            {
                Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error());
                throw new IOException(ex.Message, ex);
            }
        }

        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern SafeFileHandle CreateFile(
           string fileName,
           [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
           [MarshalAs(UnmanagedType.U4)] FileShare fileShare,
           IntPtr securityAttributes,
           [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
           int flags,
           SafeFileHandle template);

   }

    public static class FileUtility
    {
       [FileIOPermission(SecurityAction.LinkDemand, Unrestricted=true)]
       public static void CreateHardLink(string oldFileName, string newFileName)
       {
          NativeMethods.CreateHardLink(oldFileName, newFileName);
       }

    }

    class Program
    {
        static void Main()
        {
           try
           {
              FileUtility.CreateHardLink("a.txt", "b.txt");
           }
           catch (IOException ex)
           {
              Console.WriteLine(ex.Message);
           }
            
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -