2009-12-30 20 views
37

मुझे एक फ़ाइल को दूसरी पथ पर कॉपी करने की आवश्यकता है, जहां मूल है उसे छोड़ दें।किसी फ़ाइल को किसी अन्य पथ पर कॉपी करने के लिए कैसे करें?

मैं भी फ़ाइल का नाम बदलने में सक्षम होना चाहता हूं।

क्या FileInfo की CopyTo विधि काम करेगी?

+0

आप मूल या नई-नई कॉपी फ़ाइल का नाम बदलें करना चाहते हैं? –

+0

नई प्रतिलिपि फ़ाइल का नाम बदलें – mrblah

उत्तर

54

File.Copy()

पर एक नज़र का उपयोग करते हुए File.Copy आप गंतव्य स्ट्रिंग के भाग के रूप में नए फ़ाइल नाम निर्दिष्ट कर सकते हैं है।

तो

File.Copy(@"c:\test.txt", @"c:\test\foo.txt"); 

की तरह कुछ भी How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

+0

और ध्यान देने योग्य एक महत्वपूर्ण बात यह है कि आप निर्दिष्ट कर सकते हैं कि गंतव्य फ़ाइल को ओवरराइट किया जाना चाहिए या नहीं, तीसरे तर्क को सत्य या गलत के रूप में जोड़ना चाहिए। – Arman

+0

यह 2 सर्वर के लिए अलग नहीं है –

4

देखें तुम भी File.Copy इस्तेमाल कर सकते हैं कॉपी करने के लिए और File.Move यह समापन नाम बदलने के लिए।

// Copy the file (specify true or false to overwrite or not overwrite the destination file if it exists. 
File.Copy(mySourceFileAndPath, myDestinationFileAndPath, [true | false]); 

// EDIT: as "astander" notes correctly, this step is not necessary, as File.Copy can rename already... 
//  However, this code could be adapted to rename the original file after copying 
// Rename the file if the destination file doesn't exist. Throw exception otherwise 
//if (!File.Exists(myRenamedDestinationFileAndPath)) 
// File.Move(myDestinationFileAndPath, myRenamedDestinationFileAndPath); 
//else 
// throw new IOException("Failed to rename file after copying, because destination file exists!"); 

संपादित
"का नाम बदलने" कोड बाहर टिप्पणी की, क्योंकि File.Copy पहले से ही कॉपी और एक कदम में नाम बदल सकते हैं, के रूप में astander टिप्पणी में सही ढंग से उल्लेख किया।

हालांकि, यदि नाम ओपी को किसी नए स्थान पर कॉपी करने के बाद स्रोत फ़ाइल का नाम बदलना चाहता है तो नाम बदलना कोड अनुकूलित किया जा सकता है।

+0

आपको प्रतिलिपि बनाने और नाम बदलने की आवश्यकता नहीं है, आप इसे फ़ाइल.कॉपी –

+0

का उपयोग करके एक ही चरण में कर सकते हैं। समझ में आता है ... मैंने हाल ही में File.Move का उपयोग करके कोड में इतनी अधिक फ़ाइल नामांकन की है कि मैंने फ़ाइल के बारे में भी सोचा नहीं था। कॉपी करने में सक्षम होने के नाते भी :-) –

2

फ़ाइल :: प्रतिलिपि फ़ाइल को गंतव्य फ़ोल्डर में कॉपी करेगी और फ़ाइल :: मूव दोनों फ़ाइल को स्थानांतरित और नाम बदल सकते हैं।

6

हां। यह काम करेगा: FileInfo.CopyTo Method

किसी मौजूदा फ़ाइल की ओवरराइटिंग को अनुमति देने या रोकने के लिए इस विधि का उपयोग करें। डिफ़ॉल्ट रूप से किसी मौजूदा फ़ाइल की ओवरराइटिंग को रोकने के लिए CopyTo विधि का उपयोग करें।

अन्य सभी प्रतिक्रियाओं सही हैं, लेकिन जब से तुम FileInfo के लिए कहा है, यहाँ एक नमूना है:

FileInfo fi = new FileInfo(@"c:\yourfile.ext"); 
fi.CopyTo(@"d:\anotherfile.ext", true); // existing file will be overwritten 
7

मैं एक स्थान से दूसरे करने के लिए एक xml फ़ाइल को कॉपी करने की कोशिश की। यहाँ मेरी कोड है:

public void SaveStockInfoToAnotherFile() 
{ 
    string sourcePath = @"C:\inetpub\wwwroot"; 
    string destinationPath = @"G:\ProjectBO\ForFutureAnalysis"; 
    string sourceFileName = "startingStock.xml"; 
    string destinationFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".xml"; // Don't mind this. I did this because I needed to name the copied files with respect to time. 
    string sourceFile = System.IO.Path.Combine(sourcePath, sourceFileName); 
    string destinationFile = System.IO.Path.Combine(destinationPath, destinationFileName); 

    if (!System.IO.Directory.Exists(destinationPath)) 
     { 
     System.IO.Directory.CreateDirectory(destinationPath); 
     } 
    System.IO.File.Copy(sourceFile, destinationFile, true); 
} 

तो मैं निश्चित अंतराल जो मुझे लगता है कि आप को देखने के लिए की जरूरत नहीं है की एक timer_elapsed समारोह के अंदर इस समारोह कहा जाता है। इसने काम कर दिया। उम्मीद है की यह मदद करेगा।

-1

फ़ोल्डर मैं दो पाठ बॉक्स का उपयोग फ़ोल्डर की जगह और एंथर पाठ बॉक्स को पता है कि फ़ोल्डर प्रतिलिपि बनाने के लिए जानना कॉपी करने के लिए और इस संहिता

MessageBox.Show("The File is Create in The Place Of The Programe If you Don't Write The Place Of copy And You write Only Name Of Folder");// It Is To Help The User TO Know 
      if (Fromtb.Text=="") 
     { 
      MessageBox.Show("Ples You Should Write All Text Box"); 
      Fromtb.Select(); 
      return; 
     } 
     else if (Nametb.Text == "") 
     { 
      MessageBox.Show("Ples You Should Write The Third Text Box"); 
      Nametb.Select(); 
      return; 
     } 
     else if (Totb.Text == "") 
     { 
      MessageBox.Show("Ples You Should Write The Second Text Box"); 
      Totb.Select(); 
      return; 
     } 

     string fileName = Nametb.Text; 
     string sourcePath = @"" + Fromtb.Text; 
     string targetPath = @"" + Totb.Text; 


     string sourceFile = System.IO.Path.Combine(sourcePath, fileName); 
     string destFile = System.IO.Path.Combine(targetPath, fileName); 


     if (!System.IO.Directory.Exists(targetPath)) 
     { 
      System.IO.Directory.CreateDirectory(targetPath); 
      //when The User Write The New Folder It Will Create 
      MessageBox.Show("The File is Create in "+" "+Totb.Text); 
     } 


     System.IO.File.Copy(sourceFile, destFile, true); 


     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 


      foreach (string s in files) 
      { 
       fileName = System.IO.Path.GetFileName(s); 
       destFile = System.IO.Path.Combine(targetPath, fileName); 
       System.IO.File.Copy(s, destFile, true); 

      } 
      MessageBox.Show("The File is copy To " + Totb.Text); 

     } 
0

यह है कि मैं क्या ले जाने के लिए किया था डाउनलोड से डेस्कटॉप पर एक परीक्षण फ़ाइल। मुझे आशा है कि यह उपयोगी होगा।

private void button1_Click(object sender, EventArgs e)//Copy files to the desktop 
    { 
     string sourcePath = @"C:\Users\UsreName\Downloads"; 
     string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
     string[] shortcuts = { 
      "FileCopyTest.txt"}; 

     try 
     { 
      listbox1.Items.Add("Starting: Copy shortcuts to dektop."); 
      for (int i = 0; i < shortcuts.Length; i++) 
      { 
       if (shortcuts[i]!= null) 
       { 
        File.Copy(Path.Combine(sourcePath, shortcuts[i]), Path.Combine(targetPath, shortcuts[i]), true);       
        listbox1.Items.Add(shortcuts[i] + " was moved to desktop!"); 
       } 
       else 
       { 
        listbox1.Items.Add("Shortcut " + shortcuts[i] + " Not found!"); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      listbox1.Items.Add("Unable to Copy file. Error : " + ex); 
     } 
    }   
0
string directoryPath = Path.GetDirectoryName(destinationFileName); 

// If directory doesn't exist create one 
if (!Directory.Exists(directoryPath)) 
{ 
DirectoryInfo di = Directory.CreateDirectory(directoryPath); 
} 

File.Copy(sourceFileName, destinationFileName); 
संबंधित मुद्दे

 संबंधित मुद्दे