使用c#桌面应用程序上传文件 [英] uploading file using c# desktop application

查看:96
本文介绍了使用c#桌面应用程序上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!!!

上传文件唱c#桌面应用程序,我使用

File.copy但这仅用于将文件复制到另一个文件..

我希望将文件复制到文件夹或目录中..所以请帮我这样做...

hi everyone!!!
in uploading a file sing c# desktop application , i m using
File.copy but this is used only to copying a file into another file..
and i want to copy a file into a folder or directory..so please help me in doing so...

推荐答案

尝试以下代码



Try the following code

public class SimpleFileCopy
{
    static void Main()
    {
        string fileName = "test.txt";
        string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";

        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and  
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);

        // To move a file or folder to a new location:
        System.IO.File.Move(sourceFile, destinationFile);
 
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);
 
            foreach (string s in files)
            {
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }
    }
}


File.Copy在文件夹和磁盘之间正常工作:只提供完整路径源和目的地:

File.Copy works fine between folders and across disks: just provide the full path of the source and destination:
File.Copy(@"D:\Temp\MyFile.txt", @"E:\Backups\Temporary\MyFile.txt.bak");

您可能遇到的唯一问题是目标文件夹是否不存在 - 如果是哪种情况,您必须先创建它。

The only problem you may get is if the destination folder does not exist - if which case you will have to create it first.


你的假设是错误的。

File.Copy可以将文件复制到另一个目录(又名文件夹)



Your assumption is wrong.
File.Copy can copy a file into another directory (a.k.a folder)

public static void Copy(
	string sourceFileName,
	string destFileName
)





参见示例代码:



See example code:

// Recursive function to copy a folder's content into another folder
void Copy(string sourceDir, string targetDir)
{
    Directory.CreateDirectory(targetDir);

    foreach(var file in Directory.GetFiles(sourceDir))
        File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));

    foreach(var directory in Directory.GetDirectories(sourceDir))
        Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
}





祝你好运,

Edo



Good luck,
Edo


这篇关于使用c#桌面应用程序上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆