Opening a file in Notepad
Hello. There was a problem when opening a program file in notepad.
string tmp = listBox1.SelectedItem.ToString();
tmp = tmp.Replace("\\", "\\\\");
Process.Start("C:\\Windows\\System32\\notepad.exe", tmp);
When you double-click on a line in the listbox, notepad opens and the error "Syntax error in the file name, folder name, or volume label." This is very strange, because I escape slashes and there should be no error...
I tried to create a string, and write the path to the file in it (naturally escaping slashes, a string like " C:\file.txt"), the file is wonderful I opened it in my notebook and this fact completely baffled me... What is the error and what I am doing wrong, I do not 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);
}
}
}
1
Author: Nicolas Chabanovsky, 2012-08-13
2 answers
// Замени
listBox1.Items.Add(s + "\n");
// на
listBox1.Items.Add(s);
// или
Process.Start("C:\\Windows\\System32\\notepad.exe", tmp.Trim());
1
Author: Murad, 2012-08-13 17:29:07
Remark
If you put @ before the beginning of the string, the compiler will understand the string verbatim.
1
Author: Чистяков Владислав, 2012-08-19 10:47:44