Hi everybody,

I have a quite simple question, at least I think it is, even though I don't have a clue how to do it.
I have to do a project at university and I am supposed to compare differences of a newspaper. Anyway I can download every single page - but that would take me days to finish - so I want to automate it like this:

1.	Enter Date: 2010-02-03 (for example)
2.	Create Folder: 2010-02-03 
3.	Download 
•	J01-B.JPG
•	to
•	J36-B.JPG (if available)
                  From: [url]http://www.nanfangdaily.com.cn/epaper/21cn/IMAGE/20100203/[/url]
4.	Save to Folder: 2010-02-03
5.	If download J01-B.JPG to J36-B.JPG complete
•	Create/Print PDF with/via “Free PDF” from whole Folder “2010-02-03”.

Is there anyone who can actually code me such a thing or give me a hint what software I could use and how I can start this by myself? Recently I downloaded C# but got no clue about what to do!

Any help (the sooner the better) would be great - or is it to difficult?

Thanks!
Malaoshi

We don't do homework you know. If you want some help then show us that you have put some work into your project.

System.IO.Directory you can use that static class to manipulate directories.
System.Net.WebClient class to download your files.

Use google to find a helpful lib to print the files.

I guess you downloaded Visual Studio 2008 C# Express.
It has a "Getting Started" section, well... to get you started. Try it!

We don't do homework you know. If you want some help then show us that you have put some work into your project.

System.IO.Directory you can use that static class to manipulate directories.
System.Net.WebClient class to download your files.

Use google to find a helpful lib to print the files.

Thanks for your help. I know you don't do homework. It was just that I had no time, but now it's to late anyway. I won't make both - the presentation and the programming.

But still when I have time mid of march I will give it a try. System.IO.Directory and System.Net.WebClient seem to be the classes I need. Thanks.

I will let you know when I am in progress. And I already started a little bit with windows forms, but still a long road to go ;-)

Thanks and sorry for bothering!
Malaoshi

As I am stuck with the presentation, I tried a little bit:

Here it is. I made a Windows Forms thing including:

1) a "start"-button
2) a "date"-text-input-field
3) a "label"-for information

and then I put all the code in the start button - that on Click...some things should happen. Here it is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void start_Click(object sender, EventArgs e)
        {
            string remoteUri = "http://www.nanfangdaily.com.cn/epaper/21cn/IMAGE/";
            string datum = dateBox.Text;
            string fileName = "J01-B.JPG", myStringWebResource = null;
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            // Concatenate the domain with the Web resource filename.
            myStringWebResource = remoteUri + datum + fileName;
            label1.Text = "Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource;
            // Download the Web resource and save it into the current filesystem folder.
            myWebClient.DownloadFile(myStringWebResource,fileName);        
            label1.Text = "Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource;
            label1.Text = "\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath;
            
        }
    }
}

I actually don't understand the whole webClient stuff. I just surfed the msdn Database to find some source and that's what seemed to be the right thing.

I know it's pretty much to ask about, but maybe you guys could help me a little more. I guess most of the code I made/copied sucks and won't work. But maybe you could tell me, if it is the right direction or that I am heading towards a totally wrong way.

Looking forward to your replies!
Malaoshi

I think, after line 28 in your code you need a label1.Refresh(); statement or you won't see the label update before you start downloading a file.

I think, after line 28 in your code you need a label1.Refresh(); statement or you won't see the label update before you start downloading a file.

private void start_Click(object sender, EventArgs e)
        {
            string remoteUri = "http://nf.nfdaily.cn/epaper/21cn/IMAGE/";
            string datum = dateBox.Text + "/";
            int count = 1;
            for (int i = 10; count < i; count++)
            {
                label1.Refresh();
                Convert.ToString(count);
                string fileName = "J0" + count + "-B.JPG", myStringWebResource = null;
                // Create a new WebClient instance.
                System.Net.WebClient myWebClient = new WebClient();
                // Concatenate the domain with the Web resource filename.
                myStringWebResource = remoteUri + datum + fileName;
                label1.Text = myStringWebResource + " ==> downloading";
                label1.Refresh();
                // Download the Web resource and save it into the current filesystem folder.
                myWebClient.DownloadFile(myStringWebResource, fileName);
                label1.Text = myStringWebResource;
                label1.Text = Application.StartupPath;
            };

            for (int i = 37; count < i; count++)
            {
                label1.Refresh();
                Convert.ToString(count);
                string fileName = "J" + count + "-B.JPG", myStringWebResource = null;
                // Create a new WebClient instance.
                System.Net.WebClient myWebClient = new WebClient();
                // Concatenate the domain with the Web resource filename.
                myStringWebResource = remoteUri + datum + fileName;
                label1.Text = myStringWebResource + " ==> downloading";
                label1.Refresh();
                // Download the Web resource and save it into the current filesystem folder.
                myWebClient.DownloadFile(myStringWebResource, fileName);
                label1.Text = myStringWebResource;
                label1.Text = Application.StartupPath;
            };

        }

Okay, it works now, but still a long way to go. I want to make him count the Picturenumber "01" to "40" if available and than instead of saving the file into the project folder into a new folder. ==> Made a for-thing so he downloads all the files now. But what can I do if he doesn't find the file. I need to make some kind of if-statement, right?

But so far - thanks for your help/pushing me to at least try it on my own ;-)

I will work on it. Any further ideas appreciated!
Malaoshi

Edited 6 Years Ago by Malaoshi: n/a

What can I do if he can't find the file - then the programm crashes I need some way of stopping this?

You could put the webclient.DownloadFile() in a try catch block:

try{
//your code here
}
catch(Exception error)
{
//print error
}

Also its a good idea to make the download async like this(which will also handle errors):

private void start_Click(object sender, EventArgs e)
        {
            string StringWithURLtoDownload = "http://ravenlabs.org/storage/EVEOnline-TheSun.png";
            string StringWhereToSaveFile = "local.png";

            // declare our WebClient and declare some events for async to work in our favor
            WebClient web = new WebClient();
            web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
            web.DownloadFileCompleted += new AsyncCompletedEventHandler(web_DownloadFileCompleted);
            web.DownloadFileAsync(new Uri(StringWithURLtoDownload), StringWhereToSaveFile);
        }

        void web_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // no error was encountered so everything is OK and our file was downloaded
            }
            else
            {
                // we had an error, you can either handle it here or just remove the whole else statement
            }
        }

        void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            LabelToUpdate.Text = e.BytesReceived + " bytes downloaded out of " + 
                e.TotalBytesToReceive + " " + e.ProgressPercentage + "/100";
        }

I kinda made it. It's working. I bet there is still a lot of stupid coding in there. But anyway - for a start, I am satisfied.

One more question left: How can I compile it now and give it to a friend? I have the C# Express Version and I can debug it and run it them but how can I put it into one file and send to it friends?

@all: thanks for your help so far!

private void start_Click(object sender, EventArgs e)
        {
            string remoteUri = "http://nf.nfdaily.cn/epaper/21cn/IMAGE/";
            string datum = dateBox.Text + "/";
            int count;
            // Specify the directory you want to manipulate.
            string path = PathBox.Text + @"\" + dateBox.Text + @"\";

            try
            {
                // Determine whether the directory exists.
                if (Directory.Exists(path))
                {
                    MessageBox.Show("That path exists already:" + path);
                }
                else
                {
                    // Try to create the directory.
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    label4.Text = "The directory was created successfully at: " + Directory.GetCreationTime(path);
                }
            }
            catch
            {
                MessageBox.Show("There was a problem");
            }
            
                    try
                    {
                        count = Convert.ToInt32(nummerBox.Text);
                    }
                    catch
                    { count = 1; }
           
            for (int i = 37; count < i; count++)
            {
                label1.Refresh();
                Convert.ToString(count);
                string fileName = null, myStringWebResource = null;
                        
                        if (count < 10)
                        {
                            fileName = "J0" + count + "-B.JPG";
                        }
                        else 
                        { 
                            fileName = "J" + count + "-B.JPG"; 
                        }
                
                System.Net.WebClient myWebClient = new WebClient();
                myStringWebResource = remoteUri + datum + fileName;
                label1.Text = myStringWebResource + " ==> downloading";
                label1.Refresh();

                        try
                        {
                            myWebClient.DownloadFile(myStringWebResource,path + fileName);
                        }
                        catch { }
                
                label1.Text = myStringWebResource;
                label5.Text = path;
            };

Malaoshi

This article has been dead for over six months. Start a new discussion instead.