2012-05-19 58 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當前應用程序的執行路徑。 –