Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:12:30+00:00 2026-06-15T19:12:30+00:00

I want to copy a file to a new file name. Sometimes the source

  • 0

I want to copy a file to a new file name. Sometimes the source file might be a symbolic (file) link, created with

mklink C:\MyPath\ThisIsASymbolicLink.xml C:\MyPath\ThisIsTheOriginal.xml

I’m using this code:

string from = @"C:\MyPath\ThisIsASymbolicLink.xml";
string to = @"C:\MyPath\WantCopyOfOriginalFileHere.xml";
File.Copy(from, to, true);

However, I receive an IOException

The name of the file cannot be resolved by the system.

when the from file is really a symbolic link.

How can I code for the cases where the source file might be a real file, or a symbolic link to a file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:12:31+00:00Added an answer on June 15, 2026 at 7:12 pm

    Expanding on this blog post, I created extension methods that take a DirectoryInfo or FileInfo that can refer to either an original or a symbolic link, and return a string indicating the fully qualified path name of the original file.

    App Code

    The application code is modified as follows:

    // Works whether or not file is a symbolic link
    string from = 
        new FileInfo(@"C:\MyPath\ThisIsASymbolicLink.xml").GetSymbolicLinkTarget();
    

    Extension Method Code

        private const int FILE_SHARE_READ = 1;
        private const int FILE_SHARE_WRITE = 2;
    
        private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;
    
        private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
    
        // http://msdn.microsoft.com/en-us/library/aa364962%28VS.85%29.aspx
        [DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags);
    
        // http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
        [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode,
        IntPtr SecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
    
        public static string GetSymbolicLinkTarget(this FileSystemInfo symlink)
        {
            using (SafeFileHandle fileHandle = CreateFile(symlink.FullName, 0, 2, System.IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, System.IntPtr.Zero))
            {
                if (fileHandle.IsInvalid)
                    throw new Win32Exception(Marshal.GetLastWin32Error());
    
                StringBuilder path = new StringBuilder(512);
                int size = GetFinalPathNameByHandle(fileHandle.DangerousGetHandle(), path, path.Capacity, 0);
                if (size < 0)
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                // The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\"
                // More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx
                if (path[0] == '\\' && path[1] == '\\' && path[2] == '?' && path[3] == '\\')
                    return path.ToString().Substring(4);
                else
                    return path.ToString();
            }
        }
    

    Unit Tests

    [TestClass]
    public class SymlinkTest
    {
        [TestInitialize]
        public void SetupFiles()
        {
            if (!File.Exists(@"C:\Temp\SymlinkUnitTest\Original.txt")) throw new Exception("Run Symlinksetup.bat as Admin to create test data.");
        }
    
        [TestMethod]
        public void OrdinaryFile()
        {
            string file = @"C:\Temp\SymlinkUnitTest\Original.txt";
            string actual = new FileInfo(file).GetSymbolicLinkTarget();
            Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest\Original.txt"));
        }
    
        [TestMethod]
        public void FileSymlink()
        {
            string file = @"C:\Temp\SymlinkUnitTest\Symlink.txt";
            string actual = new FileInfo(file).GetSymbolicLinkTarget();
            Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest\Original.txt"));
        }
    
        [TestMethod]
        public void OrdinaryDirectory()
        {
            string dir = @"C:\Temp\SymlinkUnitTest";
            string actual = new DirectoryInfo(dir).GetSymbolicLinkTarget();
            Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest"));
        }
    
        [TestMethod]
        public void DirectorySymlink()
        {
            string dir = @"C:\Temp\SymlinkUnitTest";
            string actual = new DirectoryInfo(dir).GetSymbolicLinkTarget();
            Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest"));
        }
    }
    

    Batch File to Create Unit Test Data

    Must be run as Administrator… a requirement of mklink.

    @Echo Off
    Echo Must be run as Administrator (due to mklink)
    mkdir C:\Temp
    mkdir C:\Temp\SymlinkUnitTest
    c:
    cd C:\Temp\SymlinkUnitTest
    
    echo Original File>Original.txt
    mklink Symlink.txt Original.txt
    
    mklink /D C:\Temp\SymlinkUnitTest\SymDir C:\Temp\SymlinkUnitTest
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to copy a binary master file in a new binary file. This
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
I want to copy a xml file from one remote box to a bunch
Hi I want to copy an xml file and insert in a specific element
I want to copy war file to tomcat web-app directory using NSIS setup. I
I want to copy a file from my one rails application to a remote/
I want to copy the newest file that is on a mapped network directory.
I want to monitor the copy file function using Delphi. I can do it
I want to copy a .properties file from a certain location to my WEB-INF/classes/com/infiniti
I am working on an android game. I want to copy a text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.