如何在c#中设置路径?

时间:2012-05-19 11:02:01

标签: c# visual-studio-2010

我的解决方案中有一个名为Template的文件夹。我想要将一些文件复制到其中并从中进行访问。我该如何设置路径?部署应用程序时,此文件夹是否存在?

这有用吗?

File.Move(@"DebriefReportTemplate.docx", @"~\Template\DebriefReportTemplate.docx");

4 个答案:

答案 0 :(得分:1)

除非您在安装时构建安装/部署项目以创建它,或者在应用程序中添加代码以在首次调用时创建它,否则不会创建它。

答案 1 :(得分:0)

编辑:这个答案适用于ASP.NET应用程序。

如果模板文件夹(包括其内容)是Web项目的一部分,则部署应自动运行。如果要在运行时将文件添加到此文件夹,可以使用

Server.MapPath(@"~\Template\DebriefReportTemplate.docx")

,但要小心,Web应用程序通常以一种对本地资源的访问权限有限的身份运行。

如果您有一个Win应用程序,同样适用。您需要做的是将文件夹和文件添加到项目中,如内容。您需要一个安装项目。

答案 2 :(得分:0)

如果您担心模板文件夹的存在,您可以在代码中的某个位置创建它。

string path = System.IO.Path.Combine("", "Template");
System.IO.Directory.CreateDirectory(path);

然后移动文件

File.Move(@"DebriefReportTemplate.docx", @"Template\DebriefReportTemplate.docx");

答案 3 :(得分:0)

您可以使用

    string sourceFile = Path.GetDirectoryName(Application.ExecutablePath)+@"\Template\DebriefReportTemplate.docx";
    string destinationFile = @"C:\DebriefReportTemplate.docx";

    // To move a file or folder to a new location:
    System.IO.File.Move(sourceFile, destinationFile);
  

参考文献:

     
相关问题