string filePath = Application.StartupPath; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } try { CopyDirectory("\\\\192.168.0.23\\printUpdateFile", filePath); MessageBox.Show("更新成功!"); Application.Exit(); } catch (Exception) { MessageBox.Show("更新失败!"); Application.Exit(); }
private void CopyDirectory(string srcdir, string desdir) { string[] filenames = Directory.GetFileSystemEntries(srcdir); foreach (string file in filenames)// 遍历所有的文件和目录 { if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string currentdir = desdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1); if (!Directory.Exists(currentdir)) { Directory.CreateDirectory(currentdir); } CopyDirectory(file, desdir); } else // 否则直接copy文件 { string srcfileName = file.Substring(file.LastIndexOf("\\") + 1); srcfileName = desdir + "\\" + srcfileName; if (!Directory.Exists(desdir)) { Directory.CreateDirectory(desdir); } File.Copy(file, srcfileName, true); } } }
如果访问共享文件夹需要输入验证添加下面代码
方法1: ManagementScope ms = new ManagementScope(@"\\"); ConnectionOptions conn = new ConnectionOptions(); conn.Username = ""; conn.Password = ""; ms.Options = conn; ms.Connect();方法2:System.Diagnostics.Process.Start("net.exe","use \\\\"+ strServer +" /user:\""+ strUserName +"\" \""+ strUserPD +"\""); 然后就可以随时访问这台机器上的共享文件夹了 string strPath = "\\\\10.1.12.3\\MS\\"; ......