-1

我正在使用以下代码写入我的项目根文件夹中的文件..

public class Service2 : IService2
{

    public Boolean SaveInfo(String firstName, String lastName, DateTime dateOfBirth,
    String email, String streetAddress, String suburb, String state, int postcode,
    Job job)
    {
        string text = firstName + " " + lastName + " " + dateOfBirth + " " + email + " " + streetAddress + " " + suburb + " " + state + " " + postcode + " " + job;
        String[] people = Regex.Split(text, " ");
        using (System.IO.StreamWriter file = new System.IO.StreamWriter("Person.txt", true))
        {
            foreach (string c in people)
            {
            file.Write(c);
            file.Close();
            return true;
            }
            return false;
        }
    }

基本上它返回一个取决于文件是否被写入的布尔值......我实际上很难找到写入文件的内容。执行时它确实返回 true - 我在 WCF 测试器中运行 WCF 服务并使用值调用它以写入文件...但是当我从右侧的解决方案资源管理器中打开 Person.txt 文件时,什么也没有写在里面...

所以我的问题是,这是将文本行写入文本文件的最佳方法吗?如果是,我哪里出错了?

问候

4

4 回答 4

1
    public Boolean SaveInfo(String firstName, String lastName, DateTime dateOfBirth,
String email, String streetAddress, String suburb, String state, int postcode)
    {
        string text = firstName + " " + lastName + " " + dateOfBirth + " " + email + " " + streetAddress + " " + suburb + " " + state + " " + postcode + " ";
        String[] people = Regex.Split(text, " ");
        try
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("Person.txt", true))
            {
                foreach (string c in people)
                {
                    file.Write(c);
                }
                file.Close();
            }
            return true;
        }
        catch (Exception)
        {

            return  false;
        }

    }

You were closing your file after the first string in the Array, but have to close it after you've looped through every string in the Array. For me it's working, but we may need to see your function call

于 2013-08-28T12:06:12.150 回答
1

尝试使用此代码

string text = firstName + " " + lastName + " " + dateOfBirth + " " + email + " " + streetAddress + " " + suburb + " " + state + " " + postcode + " " + job;
        String[] people = text.Split(" ");

if (!File.Exists("Person.txt"))
        {
            // Create a file to write to. 

            File.WriteAllLines(path, people , Encoding.UTF8);
        }
else
 File.AppendAllText("Person.Text", people , Encoding.UTF8);
于 2013-08-28T11:58:31.607 回答
0

一种老式的方式

    using (FileStream fs = File.Create(yourFilePath)) 
    {
        byte[] info = new UTF8Encoding(true).GetBytes(text);
        fs.Write(info, 0, info.Length);
    }
于 2013-08-28T12:31:42.853 回答
0

尝试这个

public Boolean SaveInfo(String firstName, String lastName, DateTime dateOfBirth,
    String email, String streetAddress, String suburb, String state, int postcode,
    Job job)
    {
     try{
            string text = firstName + "\r\n" + lastName + "\r\n" + dateOfBirth + "\r\n" + email + "\r\n" + streetAddress + "\r\n" + suburb + "\r\n" + state + "\r\n" + postcode + "\r\n" + job;

            using (System.IO.StreamWriter file = new System.IO.StreamWriter("Person.txt"))
           {
               file.WriteLine(text );
               file.Close();
               return true;   
           }
       }
       catch(Exception ex)
       {
           return false;
       }
    }
于 2013-08-28T11:59:43.847 回答