0

私はcsvファイルを作成し、それをメールに添付しています...

using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(csv)))
            {
                try
                {
                    to = "email@email.com";
                    string from = "user@email.co.uk";
                    string subject = "Order p"+ OrderNumber;
                    string body = result;
                    SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
                    MailMessage mailObj = new MailMessage(from, to, subject, body);
                    Attachment attachment = new Attachment(stream, new ContentType("text/csv"));
                    attachment.Name = "p" + OrderNo + "_" + CustomerReference+".csv";
                    mailObj.Attachments.Add(attachment);
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                { }
            }

これは正常に動作しますが、同じcsvファイルをフォルダーに保存するにはどうすればよいですか?ありがとう

4

2 に答える 2

1

File.WriteAllTextSystem.IOのメソッドは、おそらくこれを実現する最も簡単な方法です。

File.WriteAllText("<destination>", csv);

<destination>、CSV を保存するファイルのパスです。このファイルがまだ存在しない場合は作成され、存在しない場合は上書きされます。

于 2012-04-30T15:46:41.530 に答える
0

ストリームを次の場所に保存する必要があります。

var newfile = new StreamWriter(path_filename);

foreach (var l in stream)
   newfile.WriteLine(l);
newfile.Close();

編集:

代わりにこれを使用してください:

StreamWriter outputfile = new StreamWriter("c:\temp\outputfile.csv"); 

そして foreach ループ。

編集 2 (msdn サイトから):

   // Read the first 20 bytes from the stream.
   byteArray = new byte[memStream.Length];
   count = memStream.Read(byteArray, 0, 20);

   // Read the remaining bytes, byte by byte.
   while(count < memStream.Length)
            {
                byteArray[count++] = 
                    Convert.ToByte(memStream.ReadByte());
            }

   // Decode the byte array into a char array 
   // and write it to the console.
   charArray = new char[uniEncoding.GetCharCount(
   byteArray, 0, count)];
   uniEncoding.GetDecoder().GetChars(
                byteArray, 0, count, charArray, 0);
            Console.WriteLine(charArray); //here you should send to the file as the first example i wrote instead of to writing to console.
于 2012-04-30T15:41:07.923 に答える