locked
Cannot implicitly convert type 'string' to 'System.Uri'

    Question

  • I know this was working before I wanted to add a progressbar1. can some one tell me what I'm doing wrong. This is in a backgroundworker1

    Joe

     

     

    try
                {
                    
                    Uri sUrlToReadFileFrom = null;
    
    
                    if (!SamplesOn)
                        sUrlToReadFileFrom = "ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename;
                    else
                        sUrlToReadFileFrom = "ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename;
    
    
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sUrlToReadFileFrom);
                    request.Credentials = new NetworkCredential("maps@frontline-nova.com", "");
                    request.UseBinary = true; // Use binary to ensure correct dlv!
                    request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    FileStream writer = new FileStream(dr + DLFilename, FileMode.Create);
    
                    long length = response.ContentLength;
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[2048];
    
                    readCount = responseStream.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                        writer.Write(buffer, 0, readCount);
                        readCount = responseStream.Read(buffer, 0, bufferSize);
    
                        backgroundWorker1.ReportProgress(readCount);
    
                    }
    
                    responseStream.Close();
                    response.Close();
                    writer.Close();
    
                    /////////////////////////////////////////////
                    File.Move(dr + DLFilename, dr + ChangeFileToo);
    
                    btnDownload.Invoke( new Action( () =>
                    {
                        ChangeFileToo = file_name.Text;
                    }));
                                  
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Error : " + ee.Message.ToString() + "\r\n", "Med Little Helper");
                    return;
                }
    
    
    


     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines

    • Edited by Joesoft11a Sunday, September 04, 2011 5:19 AM Updated it
    Sunday, September 04, 2011 5:18 AM

Answers

  • Hi

    You are trying to assign string to Uri..

    you should do this way

    Uri sUrlToReadFileFrom = new Uri(!SamplesOn ?  "ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename :  "ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename);
    
    

     or

     Uri sUrlToReadFileFrom = null;
    
    
            if (!SamplesOn)
                sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename);
            else
                sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename);
    

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Edited by Kris444 Sunday, September 04, 2011 5:23 AM
    • Marked as answer by Joesoft11a Sunday, September 04, 2011 11:50 AM
    Sunday, September 04, 2011 5:22 AM

All replies

  • Hi

    You are trying to assign string to Uri..

    you should do this way

    Uri sUrlToReadFileFrom = new Uri(!SamplesOn ?  "ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename :  "ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename);
    
    

     or

     Uri sUrlToReadFileFrom = null;
    
    
            if (!SamplesOn)
                sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename);
            else
                sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename);
    

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Edited by Kris444 Sunday, September 04, 2011 5:23 AM
    • Marked as answer by Joesoft11a Sunday, September 04, 2011 11:50 AM
    Sunday, September 04, 2011 5:22 AM
  • Hi,

    If Kris' answer was not the one you wanted, please tell us specifically what isn't working that was working before.  It is great that you are providing code, but if you provide additional info if would be event better, as "isn't working" is kind of vague :)

    You refer to a progressBar1, but there is no reference to it in this code.  backgroundWorker.ReportProgress won't work unless you also set backgroundWorker.WorkerReportsProgress, though I believe you will get a usefull error if you report progress and forgot to set this property.


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Sunday, September 04, 2011 5:40 AM
  • Thank you, That fixed one problem and now I have a new problem. I put all the code below.

     

    Error : Invalid URI: The format of the URI could not be determined.

    I'll post all my code below

    Blow is the button that starts the backgroungworker1.

     

    private void btnDownload_Click(object sender, EventArgs e)
            {
                if (listMaps.SelectedItems.Count == 0)
                {
                    MessageBox.Show("You must select a map 1st?", "Med Little Helper");
                    return;
                }            
    
                ChangeFileToo = file_name.Text;
                downloadedMaps.Items.Clear();
                
                btnDownload.Text = "Downloading...";
                backgroundWorker1.RunWorkerAsync(DoMainList.Text.ToLower());
            }
    
    
    


    Next is the code you help me to fix

     

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
    
                DirectoryInfo dr = null;
    
                if (!SamplesOn)
                    dr = new DirectoryInfo(FullPathWrite + @"Downloads\" + Category + @"\");
                else
                    dr = new DirectoryInfo(FullPathWrite + @"Downloads\" + Category + @"\" + SampleMapsPath + @"\");
    
                if (!dr.Exists)
                    Directory.CreateDirectory(dr.ToString());
                            
                try
                {
                    
                    Uri sUrlToReadFileFrom = null;
    
    
                    if (!SamplesOn)
                        sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename);
                    else
                        sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename);
    
    
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sUrlToReadFileFrom);
                    request.Credentials = new NetworkCredential("maps@frontline-nova.com", "");
                    request.UseBinary = true; // Use binary to ensure correct dlv!
                    request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    FileStream writer = new FileStream(dr + DLFilename, FileMode.Create);
    
                    long length = response.ContentLength;
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[2048];
    
                    readCount = responseStream.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                        writer.Write(buffer, 0, readCount);
                        readCount = responseStream.Read(buffer, 0, bufferSize);
    
                        backgroundWorker1.ReportProgress(readCount);
    
                    }
    
                    responseStream.Close();
                    response.Close();
                    writer.Close();
    
                    /////////////////////////////////////////////
                    File.Move(dr + DLFilename, dr + ChangeFileToo);
    
                    btnDownload.Invoke( new Action( () =>
                    {
                        ChangeFileToo = file_name.Text;
                    }));
                                  
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Error : " + ee.Message.ToString() + "\r\n", "Med Little Helper");
                    return;
                }
            }
    
    
    


    and here's a last parts of the code.

     

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
            }
    
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (!(e.Error != null))
                {
                    label4.Text = ("Error : " + e.Error.Message);
                    btnDownload.Text = "Download";
                    ChangeFileToo = null;
                }
                else
                {
                    ListDownloads(Category, downloadedMaps);
                    btnDownload.Text = "Download";
                    MessageBox.Show("Download Complete!", "Med Little Helper");
                    ChangeFileToo = null;
                    label4.Text = "Download Complete!";
                }
            }       
    
    



    Pro-Forums
    Delta Force Barracks
    On the Frontlines
    Sunday, September 04, 2011 9:40 AM
  • Hi

    You are trying to assign string to Uri..

    you should do this way

    Uri sUrlToReadFileFrom = new Uri(!SamplesOn ?  "ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename :  "ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename);
    
    

     or

     Uri sUrlToReadFileFrom = null;
    
    
            if (!SamplesOn)
                sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + DLFilename);
            else
                sUrlToReadFileFrom = new Uri("ftp." + e.Argument + "/" + Category.ToLower() + "/" + SampleMapsPath + "/" + DLFilename);
    
    

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    I'm all so getting another new error that I never got before.

    Exception has been thrown by the target of an invoctaion

    It point to Application.Run(new MainForm()); in the program.cs file.

    The only thing that I did that was different was I publish it too.

     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines

    • Edited by Joesoft11a Sunday, September 04, 2011 10:48 AM Updated it
    Sunday, September 04, 2011 10:01 AM
  • I fount out what's causing the error. It the ReportProcess for the progressbar. I'm going to have to start a new post on this one guys. Thank you very much

    '


    Pro-Forums
    Delta Force Barracks
    On the Frontlines
    Sunday, September 04, 2011 11:50 AM
  • Error : Invalid URI: The format of the URI could not be determined.

    Put "ftp://" in front of your URI. WebRequest.Create() cannot know if it's a FTP or a HTTP site.
    • Edited by Louis.fr Tuesday, September 06, 2011 10:22 AM
    Tuesday, September 06, 2011 10:22 AM