2012-05-19 57 views
0

我在我的解决方案中有一个名为Template的文件夹。我想要将一些文件复制到它并从中访问。我如何设置路径?当我部署应用程序时,此文件夹会在那里吗?如何在c#中设置路径?

能完成这项工作?

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

它是一个Web应用程序或Windows应用程序? –

+0

是的,它是一个Windows应用程序 – aroshlakshan

回答

0

编辑:这个答案是为ASP.NET应用程序。

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

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

,但要小心,Web应用程序通常在其具有对本地资源的有限访问的身份运行。

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

+0

在这篇文章中没有提及asp.net,那么为什么会假设他正在使用它? –

+0

你说得对,但为什么要用@“〜”作为本地路径?如果它不是ASP.NET,我欠你一杯啤酒:) –

+0

这是赢取窗体应用程序的家伙! – aroshlakshan

1

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

0

如果您担心Template文件夹的存在,您可以在代码中的某处创建它。

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

然后将文件

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

如何获取cuurent应用程序目录(有exe文件的目录)? – aroshlakshan

+0

上面的代码应该在包含.exe文件的文件夹中创建文件夹。 如果你需要其他用途的路径,你可以看看这里:[link](http://msdn.microsoft.com/en-us/library/aa457089.aspx) – MAV

0

您可以使用

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

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

参考文献:

+0

'Path.GetDirectoryName(Application.ExecutablePath)'returns当前应用程序的执行路径。 –