• 根据用户名/密码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);
    映射为本机,然后再操作,完成后删除.
    参考:愚翁专栏
  • 相关阅读:
    CentOS Linux使用crontab运行定时任务详解
    pycharm自定义代码片段
    迭代器:斐波那契数列
    mysql复习-来源考试
    复杂和遗留的数据库schema
    ubuntu系统AndroidStudio修改内存大小
    Hibernate设置时间戳的默认值和更新时间的自动更新
    Android 自定义view --圆形百分比(进度条)
    MYSQL 更新时间自动同步与创建时间默认值共存问题
    ubuntu virtualbox xp无声音解决
  • 原文地址:https://www.cnblogs.com/cnaspnet/p/465573.html
Copyright © 2020-2023  润新知