C#使用Streamwriter打开文件的方法

本文实例讲述了C#使用Streamwriter打开文件的方法。分享给大家供大家参考。具体如下:

using System;
using System.IO;
public class KtoD1 {
 public static void Main() {
  string str;
  StreamWriter fstr_out;
  // Open the file directly using StreamWriter.
  try {
   fstr_out = new StreamWriter("test.txt");
  }
  catch(IOException exc) {
   Console.WriteLine(exc.Message + "Cannot open file.");
   return ;
  }
  Console.WriteLine("Enter text ('stop' to quit).");
  do {
   Console.Write(": ");
   str = Console.ReadLine();
   if(str != "stop") {
    str = str + "\r\n"; // add newline
    try {
     fstr_out.Write(str);
    } catch(IOException exc) {
     Console.WriteLine(exc.Message + "File Error");
     return ;
    }
   }
  } while(str != "stop");
  fstr_out.Close();
 }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • C#如何打开并读取usb的文件目录

    下面我通过一个小例子并结合一段代码片段给大家展示下,有需要的朋友可以来借鉴下. 界面上拖一个 button 和treeview  ,运行的时候直接插入优盘,单机按钮就会出现目录文件.不过只能在u盘插入,U盘拔出时,才能接收到信息. 1.[C#]代码 using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; usi

  • 详解C#打开和关闭可执行文件

    一.打开程序 先介绍System.Diagnostics.Process类:用来启动和停止进程的. Process pr = new Process();//声明一个进程类对象 pr.StartInfo.FileName = "E://Program Files//Tencent//QQ//QQ.exe";//指定运行的程序,我的QQ的物理路径. pr.Start();//运行QQ 还可以简单点:Process的静态方法Start(); Process.Start(String fil

  • C#中执行批处理文件(*.bat)的方法代码

    复制代码 代码如下: static void Main(string[] args){    Process proc = null;    try    {                        string targetDir = string.Format(@"D:\adapters\setup");//this is where mybatch.bat lies        proc = new Process();        proc.StartInfo.Wor

  • C#实现将记事本中的代码编译成可执行文件的方法

    本文实例讲述了C#实现将记事本中的代码编译成可执行文件的方法,运行环境为VS2012,具体方法如下: 1.在记事本中编写如下代码: using System; namespace HelloWorld { public class Program { static void Main(string[] args) { Console.WriteLine("Hello,World"); Console.ReadKey(); } } } 2.保存为HelloWorld.txt,位置在F:\

  • C#使用FolderBrowserDialog类实现选择打开文件夹方法详解

    1.在C#中使用FolderBrowserDialog类,就可以实现选择文件夹的功能,并将所选择的的文件夹路径记录下来. (1).首先先引入命名空间System.Windows.Forms; (2).然后在应用程序的主入口点,也就是static void Main()方法上面加上[STAThread]属性; 复制代码 代码如下: /// <summary>        /// 应用程序的主入口点.        /// </summary>        [STAThread] 

  • C#使用Streamwriter打开文件的方法

    本文实例讲述了C#使用Streamwriter打开文件的方法.分享给大家供大家参考.具体如下: using System; using System.IO; public class KtoD1 { public static void Main() { string str; StreamWriter fstr_out; // Open the file directly using StreamWriter. try { fstr_out = new StreamWriter("test.t

  • php实现以只读方式打开文件的方法

    本文实例讲述了php实现以只读方式打开文件的方法.分享给大家供大家参考.具体分析如下: php中可以通过fopen()打开一个文件,第二个参数设置为"r"表示已只读方式打开,函数返回一个文件句柄,其它函数就可以通过这个文件句柄对文件进行不同方式的读取 <?php $file = fopen("/tmp/file.txt", "r"); print("Type of file handle: " . gettype($fi

  • C#使用StreamWriter写入文件的方法

    本文实例讲述了C#使用StreamWriter写入文件的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace W { class Program { static void Main(string[] args) { u

  • VB打开与保存txt文件的方法

    本文实例讲述了VB打开与保存txt文件的方法.分享给大家供大家参考.具体如下: Private Sub cmdsave_Click() Dim filelocation As String ' loads save as box commondialog1.ShowSave filelocation = commondialog1.FileName ' append saves over file if it assists Open filelocation For Append As #1

  • python使用wxPython打开并播放wav文件的方法

    本文实例讲述了python使用wxPython打开并播放wav文件的方法.分享给大家供大家参考.具体实现方法如下: ''' wx_lib_filebrowsebutton_sound.py select a sound file and play it wx.lib.filebrowsebutton.FileBrowseButton(parent, labelText, fileMask) (combines wx.TextCtrl and wxFileDialog widgets) wx.So

  • JavaScript实现控制打开文件另存为对话框的方法

    本文实例讲述了JavaScript实现控制打开文件另存为对话框的方法.分享给大家供大家参考.具体如下: 这里通过JS打开图片另存为对话框,提示用户保存文件 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> New Document </title> <meta name="Generator&

  • Android编程开发之打开文件的Intent及使用方法

    本文实例讲述了Android编程开发之打开文件的Intent及使用方法.分享给大家供大家参考,具体如下: 在写文件管理系统时会用到各种打开不同格式的文件的需求,由于Android系统默认内置了一些可以打开的系统应用,但还是不能满足需求,比如打开视频文件.word等,需要安装相应的播放软件才可以使用,这时程序会通过Intent查找可以使用的软件实现通过代码打开一个文件需要2部分,一部分是要获取到不同文件的后缀,以便根据需求匹配相应的Intent,另一个就是不同格式的文件打开的Intent不同 1.

  • VC WinExec打开指定程序或者文件的方法

    ⑴ 函数原型: UINT Win Exec(LPCSTR lpCmdLine, UINT uCmdShow); ⑵ 参数: lpCmdLine:指向一个空结束的字符串,串中包含将要执行的应用程序的命令行(文件名加上可选参数). uCmdShow:定义Windows应用程序的窗口如何显示,并为CreateProcess函数提供STARTUPINFO参数的wShowWindow成员的值. ⑶ 返回值: 若函数调用成功,则返回值大于31.若函数调用失败,则返回值为下列之一: ① 0:系统内存或资源已耗

  • python打开文件并获取文件相关属性的方法

    本文实例讲述了python打开文件并获取文件相关属性的方法.分享给大家供大家参考.具体分析如下: 下面的代码通过open函数打开文件,并输出文件名.打开状态.打开模式等属性 #!/usr/bin/python # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name print "Closed or not : ", fo.clos

随机推荐