[英]How to pass UNC path from server to client
I have a problem when send a UNC path from server to client. 从服务器到客户端发送UNC路径时出现问题。
part of my code from server 来自服务器的部分代码
if (data == "file")
{
// Process the data sent by the client.
data = String.Format("Request: {0}", data);
byte[] mssg = System.Text.Encoding.ASCII.GetBytes(UNCPathing.GetUNCPath(readPath()));
// Send back a response.
stream.Write(mssg, 0, mssg.Length);
bw.ReportProgress(0, String.Format("Sent: {0}", data));
bw.ReportProgress(0, String.Format("File path : {0}", mssg));
}
if the client sent request "file" the server will send back a path that convert to a UNC path. 如果客户端发送了请求“文件”,则服务器将发送回转换为UNC路径的路径。 Z:\\aaaaaaaaaaaa\\file to \\MyServer\\ShareFolder\\File and it's success when convert the path.
Z:\\ aaaaaaaaaaaaaa \\ file到\\ MyServer \\ ShareFolder \\ File,并且在转换路径时成功。
But when it's received by the client it's become Z:\\ShareFolder\\File 但是当客户端收到它时,它就变成了Z:\\ ShareFolder \\ File
it's my part of code from client 这是我来自客户端的代码部分
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
if (message == "file")
{
lstProgress.Items.Add(String.Format("{0}", responseData));
fPath.getPath = (String.Format("{0}", responseData));
label4.Text = UNCPathing.GetUNCPath(fPath.getPath);
}
And it's my UNCPathing.cs 这是我的UNCPathing.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ServerClientProject
{
public static class UNCPathing
{
[DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int WNetGetConnection(
[MarshalAs(UnmanagedType.LPTStr)] string localName,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
ref int length);
/// <summary>
/// Given a path, returns the UNC path or the original. (No exceptions
/// are raised by this function directly). For example, "P:\2008-02-29"
/// might return: "\\networkserver\Shares\Photos\2008-02-09"
/// </summary>
/// <param name="originalPath">The path to convert to a UNC Path</param>
/// <returns>A UNC path. If a network drive letter is specified, the
/// drive letter is converted to a UNC or network path. If the
/// originalPath cannot be converted, it is returned unchanged.</returns>
public static string GetUNCPath(string originalPath)
{
StringBuilder sb = new StringBuilder(512);
int size = sb.Capacity;
// look for the {LETTER}: combination ...
if (originalPath.Length > 2 && originalPath[1] == ':')
{
// don't use char.IsLetter here - as that can be misleading
// the only valid drive letters are a-z && A-Z.
char c = originalPath[0];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
int error = WNetGetConnection(originalPath.Substring(0, 2),
sb, ref size);
if (error == 0)
{
DirectoryInfo dir = new DirectoryInfo(originalPath);
string path = Path.GetFullPath(originalPath)
.Substring(Path.GetPathRoot(originalPath).Length);
return Path.Combine(sb.ToString().TrimEnd(), path);
}
}
}
return originalPath;
}
}
} }
Sorry for my Bad English, Thanks for your attentions 对不起,我的英语不好,谢谢您的关注
You are receiving a path that uses a mapped drive. 您收到使用映射的驱动器的路径。 See this post for information about retrieving the actual UNC path:
有关获取实际UNC路径的信息,请参见此帖子:
How do I determine a mapped drive's actual path? 如何确定映射驱动器的实际路径?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.