帮助与libcurl .Net [英] Help with libcurl .Net
问题描述
我想使用libcurl .net,但是没有示例,作者提供了一些示例,但对于像我这样的新编码器还不够.我的代码:
I want to use libcurl .net but there aren´t examples, the author provided a few ones but aren´t enough to a new coder like me. My code:
private FileStream fileStream;
private void button2_Click(object sender, EventArgs e)
{
fileStream = new FileStream("aaaaa.txt", FileMode.Create);
Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
Easy easy = new Easy();
Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
easy.SetOpt(CURLoption.CURLOPT_URL, "http://www.google.com");
easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
easy.Perform();
easy.Cleanup();
Curl.GlobalCleanup();
}
public Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
FileStream.Write(buf, 0, buf.Length);
return size * nmemb;
}
如您所见,我声明了"private FileStream fileStream;".才能使用"OnWriteData"访问FileStream.
我的问题是我不想声明"private FileStream fileStream;".我想在"private void button2_Click"中声明FileStream并能够使用"OnWriteData"对其进行写入.
我希望有人能理解我并为我提供帮助.
As you can see I declare "private FileStream fileStream;" to be able to access the FileStream with "OnWriteData".
My problem is that I do not want to declare "private FileStream fileStream;" I want to declare the FileStream inside "private void button2_Click" and be able to write to it with "OnWriteData".
I hope that someone can understand me and can help me.
推荐答案
无需在使用此对象的方法外声明FileStream fileStream
.可能您永远都不应该这样做.这样,您将使用局部变量而不是类字段.尽可能始终选择本地.在using
块下执行-即使有异常,它也会在块末自动调用Dispose.
No need to declareFileStream fileStream
outside the method using this object. Probably, you never should do it. This way, you''re using a local variable instead of the class field. Always prefer local whenever possible. Do in underusing
block -- it will call Dispose automatically at the end of block even if there was an exception.
using (fileStream stream = new FileStream("aaaaa.txt", FileMode.Create) {
stream.Write//...
} //auto-call stream.Dispose();
无论如何,您都调用了Dispose
,这非常糟糕,您可能会丢失文件记录,因为没有进行刷新.
另外,您最好使用StreamWriter
代替:
You called Dispose
anyway, which is very bad, you could have lost file records because nothing did the flush.
Also, you could better use StreamWriter
instead:
using (StreamWriter writer = new StreamWriter("aaaaa.txt") {
writer.Write//...
//you can access underlying stream from writer if needed
} //auto-call fileStream.Dispose();
另外,关键字"private"是C#的默认值(对于类或结构成员-感谢Manfred的更正,请参见下面的评论),可以省略.
这是将相同原理应用于Easy的方法.首先,实现System.IDisposable
:
Also, keyword "private" is C# default (for class or structure members -- thanks to Manfred for correction, see his comment below), can be omitted.
This is how to apply the same principle to Easy. First, implement System.IDisposable
:
public delegate System.Int32
WriteData(
FileStream stream, //added parameter
byte[] buf, Int32 size, Int32 nmemb, bbject extraData);
class Easy : System.IDisposable {
public Easy(FileStream stream, WriteData onWriteData) {
this.Stream = stream;
this.OnWriteData = onWriteData;
} //Easy
public void Perform() {
//if (this.OnWriteData != null)
// OnWriteData(this.Stream, //...
} //Perform
void System.IDisposable.Dispose() {
try { //important to make sure...
CleanUp();
} finally { //make sure Stream is always disposed:
if (Stream != null)
Stream.Dispose();
} //exception
} //System.IDisposable.Dispose
void CleanUp() { /* ... */ }
FileStream Stream;
} //System.IDisposable
使用方法如下:
This is how to use it:
partial class MyForm {
void MyButtonClickHandler() {
using (Easy easy =
new Easy(
new FileStream("fname.aaa",
FileMode.Create), OnWriteData)) {
//..
Easy.Perform();
//..
} //using -- clean-up here
} //MyButtonClickHandler
System.Int32 OnWriteData(
FileStream stream,
byte[] buf, Int32 size, Int32 nmemb, bbject extraData)
{
stream.Write(buf, 0, buf.Length);
return size * nmemb;
} //OnWriteData
} //class MyForm
最后,这是使用MyButtonClickHandler
的方法-更好的方法:
Finally, this is how to use MyButtonClickHandler
-- better way:
MyButton.Click += (sender, eventArgs) => { MyButtonClickHandler(); };
我不知道您的C#版本,v.2.0没有lambda,但您最好还是使用匿名:
I don''t know your C# version, there is no lambda for v.2.0, but you still better of with anonymous:
MyButton.Click += delegate( object sender, System.EventArgs eventArgs) {
MyButtonClickHandler();
};
要点,但您不明白我的问题(可能是我的错,因为我无法用英语解释好).如果我听从您的建议,则可以使用"using",但范围是范围,并且无法从"OnWriteData"访问它.
(因为我无法评论Manfred的回答,所以我添加了一个疑问)
Thanks for the points but you not understand my question (perhaps is my fault since i can not explain good in english). If i follow your advice, i can use "using" but the scope is the scope, and i can´t access to it from "OnWriteData".
(I add as an asnwer because i can´t comment Manfred´s answer)
好吧,我想用libcurl .net编写一个多线程文件下载器/上传器,我是C#中非常新的东西(我使用的是.Net 3.5版本).为了使用线程,我需要一个调用每个线程的函数,该函数将要下载的文件名作为参数.由于我是.Net的新手,因此我必须非常缓慢地研究您的代码和单词,才能提供更好的答案.无论如何,感谢您的所有时间和所有的帮助.我搜索了libcurl示例,并找到了一个示例来下载文件,但是在c ++中非常简单:
Well, i want to code a multi-threaded file downloader/uploader with libcurl .net , i´m very new into c# (i´m using version .Net 3.5). And to work with threads i need a function that call each thread, and this function takes as argument the filename to download. Since i´m very new with .Net i have to study your code and your words very slowly to provide a better answer. Anyways thanks for all your time and all your help. I serached for libcurl examples and found one example to download a file but in c++ that was very simple:
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(void) {
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "http://localhost/aaa.txt";
char outfilename[FILENAME_MAX] = "C:\\bbb.txt";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
我试图用C#重写这段代码,这就是一切开始的方式.
I tryed to rewrite this code in c# and this is how everything started...
这篇关于帮助与libcurl .Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!