将文件保存到文件夹

时间:2012-02-15 21:26:36

标签: c# visual-studio-2010 download

它做得很好:<,这是我的代码:

        public void buttonSaveTo_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.ShowDialog();
        richTextBox1.Text = fbd.SelectedPath;
        string destination = fbd.SelectedPath;
    }

这就是我尝试保存文件的方式

                {
                webClient.DownloadFile("http://i.imgur.com/" + picture, @"destionation" + picture);
            }

编辑 //好的谢谢你的答案,但它仍然无法正常工作:<,也许我做错了,看看这是我写的所有代码

命名空间Imgur {     公共部分类Form1:表格     {         bool flag = true;         int downloadedNumber = 0;

    public Form1()
    {
        InitializeComponent();
    }

    public void buttonStart_Click(object sender, EventArgs e)
    {
        buttonStart.Enabled = false;
        buttonStop.Enabled = true;
        if (!flag)
        {
            flag = true;
        }

        for (int i=0;i<100000 && flag;i++)
        {
            WebClient webClient = new WebClient();
            string pic1 = rnd_str(5);
            string pic2 = ".jpg";
            string picture = pic1 + pic2;

            //********** GETTING SIZE OF IMAGE ***********
            Size sz = GetSize("http://i.imgur.com/" + picture);
            string imageSize = (sz.Width.ToString() + " " + sz.Height.ToString()); ;
            //********************************************

            if(imageSize != "161 81")
            {
                webClient.DownloadFile("http://i.imgur.com/" + picture, destination + picture);

                richTextBox1.Text += String.Format("Downloaded picture: {0}\r\n", picture);
                downloadedNumber++;
                textBoxDownloadedNumber.Text = string.Format("{0}", downloadedNumber);
            }
            webClient.Dispose();
            Application.DoEvents();
            if (i == 999995)
            {
                flag = false;
            }
        }
        richTextBox1.Text += "theend\n";
        buttonStart.Enabled = true;
        buttonStop.Enabled = false;
    }

    public static Size GetSize(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.Accept = "image/gif";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream s = response.GetResponseStream();
        Bitmap bmp = new Bitmap(s);
        Size sz = new Size(bmp.Width, bmp.Height);
        return sz;
    }

    public static string rnd_str(int liczba_liter)
    {
        Random r = new Random();
        int char_type;
        string return_string = "";
        int i =0;
        for (i = 0; i < liczba_liter; i++)
        {
            if (r.Next(1, 3) == 1)
            {
                char_type = r.Next(1, 4);
                switch (char_type)
                {
                    case 1:
                        return_string += (char)r.Next(48, 58); // convertion int -> ASCII character; 48-57 are ASCII digits
                        break;
                    case 2:
                        return_string += (char)r.Next(97, 123); // convertion int -> ASCII character; as above but small letters
                        break;
                    case 3:
                        return_string += (char)r.Next(65, 91); // as above; large letters
                        break;
                    default:
                        i -= 1;
                        break;//do not add any letter if no type is allowed
                }
            }
            else
            {
                i -= 1;
                return_string += "";
            }
        }
        return return_string;
    }

    private void buttonStop_Click(object sender, EventArgs e)
    {
        flag = false;
        buttonStart.Enabled = true;
    }

    public void buttonSaveTo_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.ShowDialog();
        richTextBox1.Text = fbd.SelectedPath;
        string destination = fbd.SelectedPath;
    }
}

}

2 个答案:

答案 0 :(得分:1)

您目前只是连接字符串,但您的文件夹名称可能不会以目录分隔符char结尾。假设picture是您图片的文件名(例如foo.jpg),请使用Path.Combine()代替让框架为您完成工作:

var localFileName = Path.Combine(destination, picture);
webClient.DownloadFile("http://i.imgur.com/" + picture, localFileName);

答案 1 :(得分:0)

DownloadFile-Call中的“目标”是字符串而不是实际变量。目标变量也必须在类级别上。就像这样:

private string destination;
public void buttonSaveTo_Click(object sender, EventArgs e)
{
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.ShowDialog();
        richTextBox1.Text = fbd.SelectedPath;
        destination = fbd.SelectedPath;
}

webClient.DownloadFile("http://i.imgur.com/" + picture, System.IO.Path.Combine(destionation, picture));