创建文件如果文件不存在

编程入门 行业动态 更新时间:2024-10-28 09:22:16
本文介绍了创建文件如果文件不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

如果(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>

更多推荐

创建文件如果文件不存在

本文发布于:2023-05-27 19:06:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/299902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!