创建文件如果文件不存在 [英] Create File If File Does Not Exist

查看:167
本文介绍了创建文件如果文件不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让我的代码来读取,如果犯规存在的文件创建其他追加。现在,如果它确实存在创建和附加是读书。下面是代码:

 如果(File.Exists(路径))
{
使用(的StreamWriter SW = File.CreateText(路径))
{

我会做到这一点?

 如果(!File.Exists(路径))
{
使用(StreamWriter的SW = File.CreateText(路径))
{

编辑:

 路径字符串= txtFilePath.Text; 

如果(!File.Exists(路径))
{
使用(StreamWriter的SW = File.CreateText(路径))
{
的foreach(在employeeList.Items)
{
sw.WriteLine(((员工)线).FirstName)VAR线;
sw.WriteLine(((员工)线).LastName);
sw.WriteLine(((员工)线).JobTitle);
}
}
}
,否则
{
的StreamWriter SW = File.AppendText(路径);

的foreach(在employeeList.Items VAR线)
{
sw.WriteLine(((员工)线).FirstName);
sw.WriteLine(((员工)线).LastName);
sw.WriteLine(((员工)线).JobTitle);
}
sw.Close();
}



}


解决方案
<>您可以简单地调用

 点使用(StreamWriter的W = File.AppendText(日志。 TXT))

如果不存在,打开文件进行追加它它会创建该文件



编辑:



这是不够的:

 路径字符串= txtFilePath.Text;使用
(StreamWriter的SW = File.AppendText(路径))
{
的foreach(在employeeList.Items VAR线)
{
员工E =(雇员)行; //拆箱一次
sw.WriteLine(e.FirstName);
sw.WriteLine(e.LastName);
sw.WriteLine(e.JobTitle);
}
}



但如果你坚持先检查,你可以做这样的事情,但我不明白这一点。

 路径字符串= txtFilePath.Text; 
{
的foreach:使用(?File.CreateText(路径)的StreamWriter SW =(File.Exists(路径))File.AppendText(路径))


(在employeeList.Items VAR线)
{
sw.WriteLine(((员工)线).FirstName);
sw.WriteLine(((员工)线).LastName);
sw.WriteLine(((员工)线).JobTitle);
}
}



此外,有一件事与您的代码要指出的是你正在做很多不必要的拆箱。如果你必须使用像的ArrayList 普通(非通用)集合,然后拆箱对象一次,并使用参考。



不过,我perfer使用列表<> 我的集合:

 公共类EmployeeList的:列表<员工> 


I need to get my code to read if file doesnt exist create else append. Right now it is reading if it does exist create and append. Here is the code:

if (File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {

Would I do this?

if (! File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {

Edit:

string path = txtFilePath.Text;

if (!File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {
        foreach (var line in employeeList.Items)
        {
            sw.WriteLine(((Employee)line).FirstName);
            sw.WriteLine(((Employee)line).LastName);
            sw.WriteLine(((Employee)line).JobTitle);
        }
    }
}
else
{
    StreamWriter sw = File.AppendText(path);

    foreach (var line in employeeList.Items)
    {
        sw.WriteLine(((Employee)line).FirstName);
        sw.WriteLine(((Employee)line).LastName);
        sw.WriteLine(((Employee)line).JobTitle);
    }
    sw.Close();
}

}

解决方案

You can simply call

using (StreamWriter w = File.AppendText("log.txt"))

It will create the file if it doesn't exist and open the file for appending.

Edit:

This is sufficient:

string path = txtFilePath.Text;               
using(StreamWriter sw = File.AppendText(path))
{
  foreach (var line in employeeList.Items)                 
  {                    
    Employee e = (Employee)line; // unbox once
    sw.WriteLine(e.FirstName);                     
    sw.WriteLine(e.LastName);                     
    sw.WriteLine(e.JobTitle); 
  }                
}     

But if you insist on checking first, you can do something like this, but I don't see the point.

string path = txtFilePath.Text;               


using (StreamWriter sw = (File.Exists(path)) ? File.AppendText(path) : File.CreateText(path))                 
{                      
    foreach (var line in employeeList.Items)                     
    {                         
      sw.WriteLine(((Employee)line).FirstName);                         
      sw.WriteLine(((Employee)line).LastName);                         
      sw.WriteLine(((Employee)line).JobTitle);                     
    }                  
} 

Also, one thing to point out with your code is that you're doing a lot of unnecessary unboxing. If you have to use a plain (non-generic) collection like ArrayList, then unbox the object once and use the reference.

However, I perfer to use List<> for my collections:

public class EmployeeList : List<Employee>

这篇关于创建文件如果文件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆