zoukankan      html  css  js  c++  java
  • 根据用户名/密码Copy局域网中指定文件夹里的文件

    代码如下:
            public static void OpenDirectory(string str_Directory,string to_Directory, string str_UserName, string str_Password)
            
    {
                
    string str_LogoOn = "net use " + str_Directory + " /user:" + str_UserName + " \"" + str_Password + "\"";
                
    string str_OpenDirectory = "start " + str_Directory;

                Process p_Tmp 
    = new Process();
                p_Tmp.StartInfo.RedirectStandardError 
    = true;
                p_Tmp.StartInfo.RedirectStandardInput 
    = true;
                p_Tmp.StartInfo.RedirectStandardOutput 
    = true;
                p_Tmp.StartInfo.UseShellExecute 
    = false;
                p_Tmp.StartInfo.CreateNoWindow 
    = true;
                p_Tmp.StartInfo.FileName 
    = "cmd.exe";
                p_Tmp.Start();
                p_Tmp.StandardInput.WriteLine(str_LogoOn);
                p_Tmp.StandardInput.WriteLine(str_OpenDirectory);
                p_Tmp.StandardInput.WriteLine(
    "exit");
                p_Tmp.Close();
                p_Tmp.Dispose();

                
    string[] files = Directory.GetFiles(fromDirectory);
                
    if (files.Length > 0)
                
    {
                    
    foreach (string s in files)
                    
    {
                        FileInfo objFI 
    = new System.IO.FileInfo(s);
                        File.Copy(str_Directory 
    + "\\" + objFI.Name.ToString(), to_Directory + "\\" + objFI.Name.ToString());
                    }

                }

            }
    加入以下namespace:
    using System.Diagnostics;
    还可以通过using System.Management;实现具体代码还未完成
    -------------------------------------------------------------------------------------------------
    以上方法做成服务在本机运行正常,可放在服务器怎么也执行不了,没发了,换招.
    用WNetAddConnection2函数
    using System.Runtime.InteropServices;
            [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;
            }

    調用方法:
                NETRESOURCE myNetResource = new NETRESOURCE();        
                myNetResource.dwScope 
    = 2;       //2:RESOURCE_GLOBALNET            
                myNetResource.dwType = 1 ;       //1:RESOURCETYPE_ANY             
                myNetResource.dwDisplayType = 3//3:RESOURCEDISPLAYTYPE_GENERIC           
                myNetResource.dwUsage = 1;       //1: RESOURCEUSAGE_CONNECTABLE     
                myNetResource.LocalName = "T:";
                myNetResource.RemoteName 
    = @"\\172.16.20.12\e$\CCL_Development\ProExcelTest\Engg_Index_DFS\Report";        
                myNetResource.Provider 
    = null;
                WNetAddConnection2(myNetResource, 
    "jinseyd00""cclsjpdc1\\wusf",0);
                WNetCancelConnection2(
    "T:",1true);
    映射为本机,然后再操作,完成后删除.
    参考:愚翁专栏
  • 相关阅读:
    [mysql] 5.1和5.5版本区别
    selenium-51job高级搜索
    selenium-xpath选择操作web元素
    selenium-css选择器高级用法
    selenium-51job自动化测试(css选择器选择元素)
    API测试
    接口测试
    selenium-百度新歌榜
    selenium-frame切换
    selenium等待元素出现和代码抛出异常继续执行
  • 原文地址:https://www.cnblogs.com/cnaspnet/p/465573.html
Copyright © 2011-2022 走看看