jack Chen jack Chen - 27 days ago 5
C# Question

UNC path error in C# when output the file

The programme is developed with C# and the error occurs during the process of outputing.
When the software runs on the local PC,there is no error however when runs on the Service an error occurs :UNC path should be \server\share.

I guess the error happens on this sentence:

Directory.CreateDirectory(m_path);

Answer

You have to convert a local path to a UNC path. Network Shares and UNC paths Encapsulates information about a single network share, including the server name, share name, share type, local path and comment. Also has some utility methods to determine if it is a file-system share, whether it matches part of a local path, and returns the root directory.

You can also use WMI to get the info like

using (ManagementClass shares = new ManagementClass(@"\\NameOfTheRemoteComputer\root\cimv2", "Win32_Share", new ObjectGetOptions())) {
    foreach (ManagementObject share in shares.GetInstances()) {
        Console.WriteLine(share["Name"]);
    }
}

but Appropriate permissions are required.

Comments