2017-05-12 6 views
-2

私は別のフォルダにフォルダのコピーファイルを必要と私は使用このファイルコードをコピーしますか?

string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath); 
      foreach (string file in files) 
      { 
       folderBrowserDialog1.ShowDialog(); 
       string xx = folderBrowserDialog1.SelectedPath; 
       folderBrowserDialog1.ShowDialog(); 
       string yy = folderBrowserDialog1.SelectedPath; 
       File.Copy(xx, yy); 

しかし、機能していませんよ。 なぜですか?

+2

、あなたは何exaclty「動作しない」んを意味し、ファイル – EpicKip

+5

をコピーしていませんか?少し具体的にしてください。 – HimBromBeere

+0

xxとyyに関する情報はほとんどありませんか?それで、File.Copyが何をしようとしているのかは明らかです。 –

答えて

0

このコードを試してください。

ファイルを読み込んで新しいファイルに書き出したいとします。

希望します。

string sourceFile; 
string newFile = "C:\NewFile\NewFile.txt"; 
string fileToRead = "C:\ReadFile\ReadFile.txt"; 
bool overwriteExistingFile = true; //change to false if you want no to overwrite the existing file. 

bool isReadSuccess = getDataFromFile(fileToRead, ref sourceFile); 

if (isReadSuccess) 
{ 
    File.Copy(sourceFile, newFile, overwriteExistingFile); 
} 
else 
{ 
    Console.WriteLine("An error occured :" + sourceFile); 
} 



//Reader Method you can use this or modify it depending on your needs. 
public static bool getDataFromFile(string FileToRead, ref string readMessage) 
{ 
    try 
    { 
     readMessage = ""; 
     if (!File.Exists(FileToRead)) 
     { 
      readMessage = "File not found: " + FileToRead; 
      return false; 
     } 
     using (StreamReader r = new StreamReader(FileToRead)) 
     { 
      readMessage = r.ReadToEnd(); 
     } 
     return true; 
    } 
    catch (Exception ex) 
    { 
     readMessage = ex.Message; 
     return false; 
    } 
} 
0

ソースコードにファイル名を使用していないようです。
例を作成しました。ファイルコピー機能。あなたがfolderbrowserなくfilebrowserを使用しているため

public void SaveStockInfoToAnotherFile(string sPath, string dPath, string filename) 
    { 
     string sourcePath = sPath; 
     string destinationPath = dPath; 
     string sourceFile = System.IO.Path.Combine(sourcePath, filename); 
     string destinationFile = System.IO.Path.Combine(destinationPath, filename); 

     if (!System.IO.Directory.Exists(destinationPath)) 
     { 
      System.IO.Directory.CreateDirectory(destinationPath); 
     } 
     System.IO.File.Copy(sourceFile, destinationFile, true); 
    } 
0
//folderBrowserDialog1.ShowDialog(); 
    //string xx = folderBrowserDialog1.SelectedPath; 
    //string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath); 

    //folderBrowserDialog1.ShowDialog(); 
    //string yy = folderBrowserDialog1.SelectedPath; 
    //foreach (string file in files) 
    //{ 
    // File.Copy(xx + "\\" + Path.GetFileName(file), yy + "\\" + Path.GetFileName(file)); 
関連する問題