none
Error help C#

    Question

  • I converted these with an online converter from vb to c#, how can I get rid of these errors?

                    string Files = Convert.ToString(e.Data.GetData(DataFormats.FileDrop));
                    Dictionary<string, string> De = new Dictionary<string, string>();
                    foreach (string F in Files)
                    {
                        System.IO.FileInfo Fi = new System.IO.FileInfo(F);
                        switch (Fi.Extension)

    ///////foreach is underlined - cannot convert type char to string

                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string files = Convert.ToString(e.Data.GetData(DataFormats.FileDrop));
                    if (files.Count() >= 2)
                    {
                        MessageBox.Show("only allowed one main executable!");
                        return;
                    }
                    else
                    {
                        try
                        {
                            System.IO.FileInfo Fi = new System.IO.FileInfo(files);
                            switch (Fi.Extension)
                            {
                                case ".exe":
                                    break;
                                case ".dll":
                                    break;
                                default:
                                    MessageBox.Show("Not a exe or dll file!");
                                    return;
                                    break;
                            }
                            System.Reflection.AssemblyName Testfile = System.Reflection.AssemblyName.GetAssemblyName(files);
                            Merge.MainAssembly = files;
                            PrimaryTb.Text = Fi.FullName;
                            outext = Fi.Extension;
                            AssmblyListBox.Enabled = true;

                           }

    ///////the break under the not a dll or exe text - unreachable code detected


    • Edited by johnmcmil Thursday, July 11, 2013 10:35 AM
    Thursday, July 11, 2013 10:34 AM

Answers

  • Files is a string so the foreach statement is trying to loop through each char in Files (so you get an error when an attempt is made to set F to a char). You want Files to be an enumerable or Array. Try:

    string[] Files = (string[])e.Data.GetData(DataFormats.FileDrop);

    • Marked as answer by johnmcmil Thursday, July 11, 2013 5:59 PM
    Thursday, July 11, 2013 10:47 AM

All replies

  • Files is a string so the foreach statement is trying to loop through each char in Files (so you get an error when an attempt is made to set F to a char). You want Files to be an enumerable or Array. Try:

    string[] Files = (string[])e.Data.GetData(DataFormats.FileDrop);

    • Marked as answer by johnmcmil Thursday, July 11, 2013 5:59 PM
    Thursday, July 11, 2013 10:47 AM
  • For the first error, the variable Files should not be declared as string. Based on the following code I assume you want to go through the files, therefore you need a list of paths (what I assume should be stored in Files), so try to get a variable of the type string[]. 

    For the second error, just erase the return or break statement (depends on what you need). The part of code that can not be reached is break because the return statement always ends the function . 

    Regards MM 

    Thursday, July 11, 2013 10:48 AM