如何在csharp中复制和写入文件 [英] how can i copy and write a file in csharp
本文介绍了如何在csharp中复制和写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在c sharp window应用程序中如何从一个文本框中读取文本,然后如何使用system.io ....... ??
解决方案<将这些文本框复制到另一个文本框中/ div>
// 从一个复制到另一个
textbox2.Text = textbox1.Text;
您可以使用TextWriter
Class [< a href =http://msdn.microsoft.com/en-gb/library/system.io.textwriter(VS.90).aspx\"target =_ blanktitle =New Window> ^ ]。
从一个文本框复制到另一个文本框:
使用RMC的解决方案1 [ ^ ]:
// 从一个文本框复制到另一个文本框
textbox2.Text = textbox1.Text;
复制文件可以使用:
使用系统;
使用 System.IO;
命名空间 FileSystemIO
{
class 计划
{
静态 void Main( string [] args)
{
string sourceFile = C:\Temp \test.txt; // 原始文件路径
string destinationFile = C:\Temp \ test_copied.txt; // 路径复制文件
尝试
{
File.Copy(sourceFile,destinationFile); // 复制文件
Console.WriteLine( 文件已成功复制); // 如果没有引发异常,则写入成功消息
}
catch (例外情况)
{
Console.WriteLine( 复制文件失败: + ex.Message); // 如果没有异常,则写入错误消息。
}
Console.ReadKey(); // 在结束程序前等待用户按键
}
}
}
有关 System.IO.File.Copy()方法 [ ^ ]。
欢呼,
Marco Alessandro Bertschi
in c sharp window application how can read text from one text box and then how can copy those to another text box using system.io.......??
解决方案
// copying from one to another textbox2.Text = textbox1.Text;
You can write your text to a file using theTextWriter
Class[^].
Copying from one to another text box:
Use the solution 1 from RMC[^]:
// copying from one to another text box textbox2.Text = textbox1.Text;
For copying files you can use this:
using System; using System.IO; namespace FileSystemIO { class Program { static void Main(string[] args) { string sourceFile = "C:\Temp\test.txt";//Original file path string destinationFile = "C:\Temp\test_copied.txt";//path of the copied file try { File.Copy(sourceFile, destinationFile); //Copy the file Console.WriteLine("File copied successfully");//Write succesful message if no exception raised } catch(Exception ex) { Console.WriteLine("Copying file failed: " + ex.Message); //Write error message if no exception raised. } Console.ReadKey();//Wait for user pressing key before ending program } } }
More info about the System.IO.File.Copy() method[^] can you find on the MSDN page.
cheers,
Marco Alessandro Bertschi
这篇关于如何在csharp中复制和写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文