有没有办法将CSVHelper CSVWriter与Dropbox一起使用?

发布于 2025-01-21 22:50:12 字数 562 浏览 3 评论 0原文

我以前曾使用过Dropbox,但读取文件。现在,我想编写一个文件,并发现这并不是那么简单。 ie this code, with FolderPath as relative to the Dropbox root folder...

using (StreamWriter writer=new StreamWriter(${FolderPath}MyCSVFile.csv"))
using (CsvWriter csv=new CsvWriter(writer,leaveOpen:false)) {csv.WriteRecords(records);}

Treats the path as relative to the device's root folder.使用读取很容易做到这一点,因为dropbox readfile返回字符串,因此所有csvhelper See都是字符串,但是在这种情况下,需要写入由Dropbox控制的位置,这意味着必须使用Dropbox Client。

我弄清楚了如何做到这一点,但是根据建议“分享您的知识Q&一种样式”,我将继续前进并为遇到此遇到的其他任何人回答自己的问题...

I've used CSVHelper with Dropbox previously, but reading files. Now I want to write a file, and have discovered that this is not so simple. i.e. this code, with FolderPath as relative to the Dropbox root folder...

using (StreamWriter writer=new StreamWriter(${FolderPath}MyCSVFile.csv"))
using (CsvWriter csv=new CsvWriter(writer,leaveOpen:false)) {csv.WriteRecords(records);}

Treats the path as relative to the device's root folder. It's easy to do this with read, because the Dropbox ReadFile returns a string, so all CSVHelper sees is a string, but in this case need to write to a location controlled by Dropbox, which means having to use the Dropbox client.

I worked out how to do it, but as per the suggestion "share your knowledge Q&A style", I'll keep going and answer my own question for anyone else who runs into this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2025-01-28 22:50:12

在键入我的问题时,我认为“如果只有一种方法可以将其作为字符串而不是写入设备”,但是CSVHelper DOCO仅使用StreamWriter向设备显示写作。因此,我决定只单击其他一些写作方案,然后在我可以使用stringwriter的“写入动态对象”中(我都不想看在那里,因为我曾经想过。与动态对象一起工作!)。

因此,这是我的工作代码(tempsb2是我在整个代码中重复使用的StringBuilder)...

TempSB2.Clear();
using (StringWriter writer=new StringWriter())
using (CsvWriter csv=new CsvWriter(writer,leaveOpen:false)) {
    csv.WriteRecords(records);
    TempSB2.Append(writer.ToString());
    }
await DropboxService.WriteFileAsync(DropboxService.StringToStream(TempSB2.ToString()),$"{FolderPath}MyCSVFile.csv");

Whilst typing my question, I thought "if only there was a way to get this as a string instead of writing to device", but the CSVHelper doco only showed writing to device, using StreamWriter. So I decided to just click around some of the other Write scenario's, and in the "writing to a Dynamic object" I found in that example that you could use StringWriter instead (I wouldn't have thought to look in there, since I wasn't working with Dynamic objects!).

So here is my working code (TempSB2 is a StringBuilder that I re-use throughout the code)...

TempSB2.Clear();
using (StringWriter writer=new StringWriter())
using (CsvWriter csv=new CsvWriter(writer,leaveOpen:false)) {
    csv.WriteRecords(records);
    TempSB2.Append(writer.ToString());
    }
await DropboxService.WriteFileAsync(DropboxService.StringToStream(TempSB2.ToString()),
quot;{FolderPath}MyCSVFile.csv");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文