简体   繁体   English

将资源文件复制到Windows目录

[英]Copy Resource File to Windows dir

I want to copy a resource file to another folder. 我想将资源文件复制到另一个文件夹。

                    System.IO.File.Copy(@"Resources\empty.jpg", destFile);

Thought this might be as easy as that. 以为这可能就这么简单。 But it throws me an error like: "Part of the path Resources\\empty.jpg could not be found. 但这会引发类似以下错误:“找不到路径Resources \\ empty.jpg的一部分。

Any help here? 这里有什么帮助吗?

You need to know the file/folder in relation to where you currently are. 您需要了解与当前位置有关的文件/文件夹。 As Francis said in the comment above, by default in VS you will be in the debug folder. 正如弗朗西斯在上述评论中所述,默认情况下,在VS中,您将位于debug文件夹中。 If Resources is off the root of C:, you need to get yourself to the root of C: First somehow, either by referencing C: directly, or by using ..\\ to work your way up, parent folder by parent folder. 如果资源不在C:的根目录下,则需要以某种方式使自己进入C:的根目录,直接引用C:或使用.. \\逐个移至父文件夹。

If it is a sub-directory from where you are at, what you show above does work correctly when the file exists. 如果它是您所在的子目录,则在文件存在时,上面显示的内容正确运行。 I tried it myself, which means you are not likely referencing a path that exists within your project\\bin\\debug path. 我自己尝试过,这意味着您不太可能引用project \\ bin \\ debug路径中存在的路径。

If it is a folder off the project, then you need to get up to the project folder itself first, ie "..\\..\\Resources\\empty.jog" . 如果它是项目之外的文件夹,则需要首先进入项目文件夹本身,即"..\\..\\Resources\\empty.jog"

The answer is here https://msdn.microsoft.com/en-us/library/cc148994.aspx . 答案是在这里https://msdn.microsoft.com/en-us/library/cc148994.aspx Looks like you need a drive letter in front of your source folder name and possibly target folder name. 看起来您在源文件夹名称和可能的目标文件夹名称之前需要一个驱动器号。

string fileName = "test237.txt";
string sourcePath = @"C:\temp";
string targetPath = @"C:\temp2";

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

System.IO.File.Copy(sourceFile, destFile, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM