Visual Studio C#创建目录/复制文件 [英] Visual studio C# create directory / copy files

查看:80
本文介绍了Visual Studio C#创建目录/复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Visual Studio 2015 C#应用程序中有一段代码应该在启动时在用户的文档文件夹中创建名为用户文件的目录。然后它应该将3个文件复制到该目录(如果它们已经存在则覆盖它们)UserFile1.kpt,UserFile2,kpt,UserFile3.kpt。这些文件将与编译的exe程序位于同一目录中。我对编码很陌生,所以我不相信这是正确的,但我希望我走在正确的轨道上并得到一些指导。获取'应用程序'不包含'StartupPath'的定义并可能创建目录两次。



我尝试过:



  public   static   string  DosesLocation 
{
get
{
string myDocsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
string dosesLocation = Path.Combine(myDocsPath, 用户文件);
Directory.CreateDirectory(dosesLocation);
return doseLocation;

string target_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 用户文件);
Directory.CreateDirectory(target_path);

foreach var name in new [] { UserFile1。 kpt Userfile2.kpt Userfile3.kpt})
{
string source_fill_path = Path.Combine(Application.StartupPath,name);
string target_fill_path = Path.Combine(target_path,name);

File.Copy(source_fill_path,target_fill_path, false );
}

}
}





想到也许这样,但不知道怎么样实现。



 string source_full_path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()。Location),name); 

解决方案

你的getter中有太多代码。还有一个返回语句在中间,所以第二个代码块永远不会被执行。 DosesLocation 是(应该是)一个属性,所以它应该包含一个 set 方法来创建路径,并且获取只返回它。创建目录并复制文件的其他代码应该在您的类的单独方法中。



至于您的错误消息,您忘记显示代码在哪里它会发生。


嗯...这是一些奇怪的代码 - 它甚至不会编译为行后的所有内容

  return  doseLocation; 

将被标记为无法访问的代码。

加上编译错误,Application.StartupPath为

'Application'不包含'StartupPath'的定义

意味着您有一个名为Application的变量或类,这意味着您正在尝试访问错误的静态属性。 br />


我怀疑你得到的任何运行时错误 - 例如尝试创建文件夹两次 - 实际上是在编译的代码的先前版本上发生的,因为编译错误会阻止生成EXE文件。 Directory.CreateDirectory 并不关心文件夹是否已存在,无论如何它都可以正常工作。

但是,除非使用三参数重载版本,否则 File.Copy 将不允许您覆盖现有文件:文件复制方法(字符串,字符串,布尔值)(System.IO) [< a href =https://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]


I have a piece of code in my Visual Studio 2015 C# app that should create a directory in the user's Documents Folder on launch called "User Files." Then it should copy 3 files to that directory (overwriting them if they are already there) UserFile1.kpt, UserFile2,kpt, UserFile3.kpt. These files will be located in the same directory as the compiled exe program. I am very new to coding so I don't believe this is right, but I hope I am on the right track and can get some guidance. Getting an " 'Application' does not contain a definition for 'StartupPath' " and maybe creating the directory twice.

What I have tried:

public static string DosesLocation
    {
        get
        {
            string myDocsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            string dosesLocation = Path.Combine(myDocsPath, "User Files");
            Directory.CreateDirectory(dosesLocation);
            return dosesLocation;

            string target_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "User Files");
            Directory.CreateDirectory(target_path);

            foreach (var name in new[] { "UserFile1.kpt", "Userfile2.kpt", "Userfile3.kpt" })
            {
                string source_fill_path = Path.Combine(Application.StartupPath, name);
                string target_fill_path = Path.Combine(target_path, name);

                File.Copy(source_fill_path, target_fill_path, false);
            }

        }
    }



Thought maybe this, but dont know how to implement.

string source_full_path = Path.Combine( Path.GetDirectoryName( Assembly.GetEntryAssembly().Location ), name );

解决方案

You have too much code inside your getter. And also a return statement bang in the middle, so the second code block never gets executed. DosesLocation is (supposed to be) a property, so it should contain a set method that creates the path, and a get that just returns it. The other code that creates the directory and copies the files should be in a separate method of your class.

As to your error message, you forgot to show the code where it occurs.


Um ... that's some odd code - it won't even compile as everything following the line

return dosesLocation;

will be flagged as "unreachable code".
Plus a compilation error an Application.StartupPath as

'Application' does not contain a definition for 'StartupPath'

Implies that you have a variable or class called Application which means you are trying to access the wrong static property.

I suspect that any run time errors you are getting - such as trying to create the folder twice - are actually occurring on a previous version of the code which did compile, since compilation errors prevent the EXE file being produced. Directory.CreateDirectory doesn't care if the folder already exists, it will work anyway.
However, File.Copy will not let you overwrite an existing file, unless you use the three-parameter overload version: File.Copy Method (String, String, Boolean) (System.IO)[^]


这篇关于Visual Studio C#创建目录/复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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