Uploading files to file server

北城余情 提交于 2019-12-07 05:33:23

Copying files from one folder to a different folder is very easy.
The below code is in C#.NET.
First add System.IO and System.NET to your namespace. Then add the following code.

string _pathForImages = "c:\inetpub\wwwroot\NewFolder\ExistingFolder\Images\";
   try
    {
        string[] f = Directory.GetFiles(_pathForImages);
        int k = f.Length;
        string _pathForImages_dest = "c:\inetpub\wwwroot\NewFolder\NewFolder1\Images\";

        for (int i = 0; i < k; i++)
        {
            var kl = f[i].Split('\\');

            string fname = kl[kl.Length - 1];
            string j = _pathForImages_test;
            System.IO.File.Copy(f[i], _pathForImages_dest + fname);


        }
    }
    catch (Exception ex)
    {

    }


If you want to copy new files and REPLACE existing files just add 'true' to the file.copy . The full code is:

string _pathForImages = "c:\inetpub\wwwroot\NewFolder\ExistingFolder\Images\";
   try
    {
        string[] f = Directory.GetFiles(_pathForImages);
        int k = f.Length;
        string _pathForImages_dest = "c:\inetpub\wwwroot\NewFolder\NewFolder1\Images\";

        for (int i = 0; i < k; i++)
        {
            var kl = f[i].Split('\\');

            string fname = kl[kl.Length - 1];
            string j = _pathForImages_test;
            System.IO.File.Copy(f[i], _pathForImages_dest + fname,true);


        }
    }
    catch (Exception ex)
    {

    }

Please read the following MSDN article, as it may help you out answering your question.

EDIT: Here is a (possible) answer to what you're looking for... To copy files from local machine you can simply use System.IO.File.Copy(), as you're are already logged in to that machine. However, to copy the files from a remote machine where you have not (yet) logged in, you have to provide domainname, username and password to proceed with authentication on the remote machine. Please test this and confirm if it does what you want it to do :)

public void copyRemoteFiles(string sourceFile, string destFile) {
    IntPtr admin_token = default(IntPtr);
    WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
    WindowsIdentity wid_admin = null;
    WindowsImpersonationContext wic = null;

    try {
        if (LogonUser(sUserName, sDomainName, sPassword, 9, 0, admin_token) != 0) {
            wid_admin = new WindowsIdentity(admin_token);
            wic = wid_admin.Impersonate();
            if (System.IO.File.Exists(sourceFile)) {
                System.IO.File.Copy(sourceFile, destFile, true);
            }
            else {
                //Copy Failed
                return;
            }
        }
        else {
            return;
        }
    }
    catch (System.Exception se) {
        int ret = Marshal.GetLastWin32Error();
        MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
        MessageBox.Show(se.Message);
        if (wic != null) {
            wic.Undo();
        }
        return;
    }
    finally {
        if (wic != null) {
            wic.Undo();
        }

    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!