C#远程打开服务器文件夹

2024-12-19 03:38:11
推荐回答(3个)
回答1:

C#编程可以通过与服务器建立网络映射,来获取服务器文件夹,代码如下:

[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
public static extern uint WNetAddConnection2([In] NETRESOURCE lpNetResource,string lpPassword,string lpUsername,uint dwFlags);
        [DllImport("Mpr.dll")]
        public static extern uint WNetCancelConnection2(string lpName,uint dwFlags,bool fForce);
        [StructLayout(LayoutKind.Sequential)]
        public class NETRESOURCE
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            public string LocalName;
            public string RemoteName;
            public string Comment;
            public string Provider;
        }
            
            statusBarLab.Text = "文件复制开始...";
            NETRESOURCE myNetResource1 = new NETRESOURCE();
            myNetResource1.dwScope = 2;
            myNetResource1.dwType = 1;
            myNetResource1.dwDisplayType = 3;
            myNetResource1.dwUsage = 1;
            myNetResource1.LocalName = "W:";
            myNetResource1.RemoteName = fromDirectory1;
            myNetResource1.Provider = null;
            WNetAddConnection2(myNetResource1, fromPwd, fromUser, 0);
            string[] files1 = Directory.GetFiles(fromDirectory1);
            if (files1.Length > 0)
            {
                foreach (string s in files1)
                {
                    FileInfo objFI = new System.IO.FileInfo(s);
                    File.Delete(toDirectory1 + "\\" + objFI.Name.ToString());
                    statusBarLab.Text = "1.删除/" + objFI.Name.ToString();

                    File.Copy("W:" + "\\" + objFI.Name.ToString(), toDirectory1 + "\\" + objFI.Name.ToString());
                    statusBarLab.Text = "1.复制/" + objFI.Name.ToString();
                }
            }
WNetCancelConnection2("W:", 1, true);

回答2:

string path = "xxx.xxx.xxx.xxx\PicFolder\pic.jpg" ; //共享文件目录,xxx.xxx.xxx.xxx为服务器IP
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)

回答3:

看你怎么链接了,问问题前建议先看文档,找Demo