2014-11-25 42 views
0

好了,所以我用C#的WinForms工作,我有這樣的代碼到目前爲止創建一個readme.txt和文件夾本身如何添加一個.js到C#中的文件

string folderName = @"C:\Test"; 
     string text = "This is used Test."; 
     if (!System.IO.File.Exists(folderName)) 
     { 
      System.IO.Directory.CreateDirectory(folderName); 
      File.Create(@"C:\Test\ReadMe.txt"); 
      System.IO.File.WriteAllText(@"C:\Test\ReadMe.txt", text); 
      //Need to add Test.js to the folder C:\Test Here 
     } 

,你可以見上面我需要將Test.js添加到文件夾中。

請和謝謝。

(P.S是的,它被添加作爲已經是資源)

+0

你的意思是你的Debug目錄中有一個名爲「Test.js」的文件,你想在這個例子中複製到「C:\ Test」目錄下? – amyn 2014-11-25 07:02:32

+0

是的,我做@amyn :) – Programerszz 2014-11-25 07:04:18

+0

[this](http://msdn.microsoft.com/en-us/library/system.io.file.copy%28v=vs.110%29.aspx)可能會幫助你 – amyn 2014-11-25 07:05:21

回答

1

您的代碼在第3行輕微的錯誤它是採用File.Exists代替Directory.Exists測試,如果測試文件夾存在。

string folderName = @"C:\Test"; 

if (!Directory.Exists(folderName)) 
{ 
    Directory.CreateDirectory(folderName); 
} 

string readMeFileName = Path.Combine(folderName, "ReadMe.txt"); 
string text = "This is used Test."; 
File.WriteAllText(readMeFileName , text); 

string jsFileName = Path.Combine(folderName, "Test.JS"); 
File.Copy("Test.js", jsFileName); 
相關問題