Click here to Skip to main content
15,999,861 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone. İm working on one project but İ cant write them code. İ mean when im opened file Application must get file path and save in listboxbox. While App re opening should read saved file path
Posted
Updated 18-May-15 21:40pm

My code here but not working When im draged file to window , window must get file path and save in listbox can u help me ?
C#
private void listbox1_DragEnter(object sender, DragEventArgs e)
       {
           if (e.Data.GetDataPresent(DataFormats.FileDrop))
               e.Effects = DragDropEffects.All;
           else
               e.Effects = DragDropEffects.None;

       }

       private void listbox1_Drop(object sender, DragEventArgs e)
       {
           string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
           int i;

           for (i = 0; i < s.Length; i++)
               listbox1.Items.Add(s[i]);



           for (i = 0; i < s.Length; i++)
               listbox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(s[i]));

           if (e.Data.GetDataPresent(DataFormats.FileDrop))
           {


           }
       }





       void listbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           try
           {
               string dir = Environment.CurrentDirectory + @"\Files\MyFiles\";
               string file = listbox1.SelectedItem.ToString();

               System.Diagnostics.Process.Start(dir + file);
           }
           catch
           {
               MessageBox.Show("Error Loading File." + Environment.NewLine + "File Not Found.", "Error", MessageBoxButton.OK);

           }
       }



       void b_Click(object sender, RoutedEventArgs e)
       {
           string fileName = ((Button)sender).Tag.ToString();
           System.Diagnostics.Process.Start(fileName);
       }

       private void btnSave_Click(object sender, RoutedEventArgs e)
       {
           const string sPath = "save.txt";

           System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);
           foreach (var item in listbox1.Items)
           {
               SaveFile.WriteLine(item);
           }

           SaveFile.Close();

           MessageBox.Show("Programs saved!");
       }
 
Share this answer
 
v2
Comments
Deepu S Nair 19-May-15 7:55am    
Don't post your comment as solution.
Member 11699952 19-May-15 8:54am    
Oky now can u help me ?
C#
private void btnOpenFiles_Click(object sender, RoutedEventArgs e)
            {
                     OpenFileDialog openFileDialog = new OpenFileDialog();
                     openFileDialog.Multiselect = true;
                     openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                     openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                     if(openFileDialog.ShowDialog() == true)
                     {
                             foreach(string filename in openFileDialog.FileNames)
                                     lbFiles.Items.Add(Path.GetFileName(filename));
                     }
             }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900