2017-02-06 10 views
-3

ソースフォルダーからコピー先のフォルダーに複数のファイルを置き換える必要があります。ソースからデスティネーションへのファイルの置換

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 

    namespace bestem_re 
    { 
     public partial class Form1 : Form 
     { 
      string[] fnames; 
      string[] dfnames; 
      string destination; 
      public Form1() 
      { 
       InitializeComponent(); 
      } 

// In this I am fetching the source of the destination files.    

//using the for each loop to iterate to all the selected files 
private void btnsource_Click(object sender, EventArgs e) 
      { 
       OpenFileDialog opf = new OpenFileDialog(); 



       opf.Multiselect = true; 
       if (opf.ShowDialog() == DialogResult.OK) 
       { 
        fnames = opf.FileNames; 
        foreach (var item in fnames) 
        { 

         txtsource.Text = txtsource.Text + System.IO.Path.GetFileName(item) + " "; 
        } 

       } 


      } 
// I Am fetching the destination of the files that are to be replaced.  
      private void btndestination_Click(object sender, EventArgs e) 
      { 
       OpenFileDialog opf1 = new OpenFileDialog(); 



       opf1.Multiselect = true; 
       if (opf1.ShowDialog() == DialogResult.OK) 
       { 
        dfnames = opf1.FileNames; 
        foreach (var item in dfnames) 
        { 

         txtdestination.Text = txtdestination.Text + System.IO.Path.GetFileName(item) + " "; 
        } 
       } 

      } 
//this is the replace function where in I carry out the process of replacing the files  
      private void btnreplace_Click(object sender, EventArgs e) 
      { 

       string source; 
       int i = 0; 

       destination = System.IO.Path.GetDirectoryName(dfnames[0]); 
       MessageBox.Show(destination); 


       foreach (var item in fnames) 
       { 
        source = item; 
        string fname = System.IO.Path.GetFileName(fnames[i]); 
        string dfname = System.IO.Path.GetFileName(dfnames[i]); 
        string FileToBackUp = destination + @"\" + dfname + ".bac"; 
        MessageBox.Show("Bestem BOX"); 
        System.IO.File.Replace(source, destination, FileToBackUp); 
        System.IO.File.Copy(destination, source); 
        MessageBox.Show("Successfull"); 
        destination = ""; 
        i++; 
       } 

      } 

     } 
    } 

//replace files from source to destination 
+0

あなたはあなたの問題を記述するために多くの時間を取る必要があり、行動を望んでいた(入力、出力、副作用)と実際の行動(何が現在あなたが望んでいないことが起きている?) – grek40

+0

私はシングルを交換しようとした場合ファイルは正常に動作しますが、foreachループを使用して複数のファイルを置き換えようとすると、アクセス権がないというエラーがスローされます –

+0

'System.IO.File.Copy(destination、source)'あなたに連れて行こうか?置換後にコピー先からコピー元にコピーしようとする理由はありませんが、この 'Copy'オーバーロードはあなたにdestFileNameが存在する場合は[IOException']を約束します(https://msdn.microsoft.com/en-us/library)。 /c6cfw35a(v=vs.110).aspx#Exceptions)。私はそれを得たと思う...答えを書くつもりだ – grek40

答えて

0

あなたの質問は曖昧です。上のコードでは、ファイルを取得してから宛先を設定するためにOpenFileDialogを使用して質問する必要があります。これは奇妙に見えるだけでなく、不正な場所やファイル名にファイルを保存するために開かれています。

開いているファイルダイアログでx個のファイルを選択できるようにすると、別の開いているファイルダイアログを使用してバックアップ先ファイルを選択するようにユーザーに依頼すると重大な問題がありますか?このように表示される場合、ユーザーは、これが正しく機能するためには、同じ順序でSAMEの正確なファイルを選択する必要があります。ユーザーが宛先を選択するのが奇妙でエラーが発生するようです。

最初の開いているファイルダイアログでユーザーが選択したファイルを使用し、それらのファイルを設定先フォルダ(この場合はファイル名の末尾に ".bac"拡張子が付いた同じフォルダ)にバックアップします。以下のコードでは、開いているファイルダイアログを使用してファイルをバックアップします。 btnsource_ClickBackupFilesと呼ばれ、選択したファイルをバックアップするために1つのボタンだけが押されます。私はこれが理にかなってほしい。

private void btnsource_Click(object sender, EventArgs e) { 
    OpenFileDialog opf = new OpenFileDialog(); 
    opf.Multiselect = true; 
    if (opf.ShowDialog() == DialogResult.OK) { 
    string[] fnames = opf.FileNames; 
    foreach (var item in fnames) { 
     textBox1.Text = textBox1.Text + System.IO.Path.GetFileName(item) + Environment.NewLine; 
    } 
    BackupFiles(fnames); 
    } 
} 

private void BackupFiles(string[] fnames) { 
    string source; 
    int i = 0; 
    string destination = System.IO.Path.GetDirectoryName(fnames[0]); 
    string fname = ""; 
    string FileToBackUp = ""; 

    foreach (var item in fnames) { 
    source = item; 
    fname = System.IO.Path.GetFileName(fnames[i]); 
    FileToBackUp = destination + @"\" + fname + ".bac"; 
    File.Copy(source, FileToBackUp, true); 
    i++; 
    } 
    MessageBox.Show("Successfull"); 
} 
0

あなたの問題はdestinationです:

UnauthorizedAccessException

[...場合]ソースまたは宛先のパラメータはディレクトリを指定する代わりのため

destination = System.IO.Path.GetDirectoryName(dfnames[0]); // is a directory 
// usage: 
System.IO.File.Replace(source, destination, FileToBackUp); 

UnauthorizedAccessExceptionをスローしますa ファイル

関連する問題