Hello. There was a problem opening the program file in Notepad.

string tmp = listBox1.SelectedItem.ToString(); tmp = tmp.Replace("\\", "\\\\"); Process.Start("C:\\Windows\\System32\\notepad.exe", tmp); 

If you double-click on a line in the listbox, the notepad opens and the error "Syntax error in the file name, folder name or volume label." This is very strange, because I screen slashes and errors should not be ...

I tried to create a string, and write the path to the file in it (of course, escaping slashes, a string like "C: \ file.txt "), the file opened remarkably in notepad and this fact put me at a dead end ... i do wrong i don't understand ...


 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; using System.IO ; using System.Diagnostics; namespace FindFile { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { progressBar1.Step = 0; progressBar1.PerformStep(); string TmpFile = ""; int NumberFiles = 0; List<string> FileOfName = new List<string>(); System.String path = PathFile.Text; try { foreach (var file in Directory.GetFiles(path, SelectType.SelectedItem.ToString(), SearchOption.AllDirectories)) { StreamReader FindFile = new StreamReader(file); string str = ""; while (!FindFile.EndOfStream) { str = FindFile.ReadLine(); if ((str.IndexOf(Substring.Text) > -1) && !TmpFile.Equals(file)) { FileOfName.Add(file); TmpFile = file; NumberFiles++; } } } if (NumberFiles != 0) { int StepProgressBar = NumberFiles / 100; progressBar1.Step = StepProgressBar; foreach (string s in FileOfName) { listBox1.Items.Add(s + "\n"); progressBar1.PerformStep(); } } else { progressBar1.Step = 100; progressBar1.PerformStep(); MessageBox.Show("По указанному пути введенная вами строка не найдена"); } } catch (Exception error) { MessageBox.Show(error.ToString()); } } private void listBox1_DoubleClick(object sender, EventArgs e) { string tmp = listBox1.SelectedItem.ToString(); //tmp = tmp.Replace("\\", "\\\\"); MessageBox.Show(tmp); Process.Start("C:\\Windows\\System32\\notepad.exe", tmp); } } } 
  • Seen in ListBox crooked path is registered. show that tama. - Murad
  • everything seems fine foreach listbox - KoCaTKo
  • I have everything open normally (and without Replace too). Look at what tmp is before opening. - Murad
  • I watched, without Replace there is the usual way C: \ file.txt - KoCaTKo
  • I ask the project in the studio. - Murad

2 answers 2

 // Замени listBox1.Items.Add(s + "\n"); // на listBox1.Items.Add(s); // или Process.Start("C:\\Windows\\System32\\notepad.exe", tmp.Trim()); 
  • thanks, here I am a deer ... - KoCaTKo

Notice
If you put an @ before the line, the compiler will understand the line verbatim.