i want to delete file.txt
but this is not deleting the file. any input?Code:System.Diagnostics.Process.Start("cmd.exe", @"/c del d:\\file.txt");
Printable View
i want to delete file.txt
but this is not deleting the file. any input?Code:System.Diagnostics.Process.Start("cmd.exe", @"/c del d:\\file.txt");
and if you run this command alone, does it work?
also check the exit code
Code:System.Diagnostics.Process.ExitCode
Why to do it in such a obscure way? Ins't File.Delete better?
Maybe new forum could be established: toy fixing :)
this is my complete codeBut i am getting error in the stream reader(filepath)Code:System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
StreamReader streamReader = new StreamReader(filePath);
string readtext = streamReader.ReadToEnd();
streamReader.Close();
System.IO.File.Delete("d:\file.txt"); //Delete the output dump file
the File is getting genereatedCode:Could not find file 'd:\file.txt'..
i have started to do coing in c# just 4 days back. I want to get the route print command to be executed and get that to a file . So when i searched some forum, i got the reply to do withTo delete also , i was trying the same way. i was not aware of the other option available. Now i moved code to file.delete . thanks for the help on that .Code:System.Diagnostics.Process.Start
Is there a way i can run the command prompt from the c# other than what i have done using System.Diagnostics.Process.Start
The file is not getting deleted
also at first time rum ( after manually deleting the file.txt) , i am getting file not found error atCode:System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
using (StreamReader streamReader = File.OpenText(filePath))
{
string s = "";
if ((s = streamReader.ReadLine()) != null)
{
readtext = streamReader.ReadToEnd();// streamReader.ReadToEnd();
}
}
/* delete all unwanted files */
System.IO.File.Delete("d:\\file.txt"); //Delete the output dump file
hERE IS my Complete code. This code some times work proplery by reading and writing ( when there is a breakpoint), some times it is not workingCode:using (StreamReader streamReader = File.OpenText(filePath))
Is it something to do with the timer?Code:class Program
{
static void deletefiles()
{
System.IO.File.Delete("d:\\madavas1.txt");// delete madavas1.txt
System.IO.File.Delete("d:\\madavas2.txt");// delete madavas2.txt
System.IO.File.Delete("d:\\file.txt"); //Delete the output dump file
}
static void Main(string[] args)
{
string regMatch = "Active Routes:";//string to search for inside of text file. It is case sensitive.
string filePath = @"d:\file.txt";
string readtext = "";
string path = "";
bool found = false, last = false;
path = @"d:\\file.txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path)) // Create a file to write to madavas2.txt.
{
}
}
System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
using (StreamReader streamReader = File.OpenText(filePath))
{
string s = "";
if ((s = streamReader.ReadLine()) != null)
{
readtext = streamReader.ReadToEnd();// streamReader.ReadToEnd();
}
}
/* delete all unwanted files */
deletefiles();
path = @"d:\\madavas1.txt";
if (!File.Exists(path))
{
// Create a file to write to madavas1.txt.
using (StreamWriter sw = File.CreateText(path))
{
}
}
path = @"d:\\madavas2.txt";
if (!File.Exists(path))
{
// Create a file to write to madavas2.txt.
using (StreamWriter sw = File.CreateText(path))
{
}
}
/* create mdavas2.txt for active route*/
StringReader madvas2 = new StringReader(readtext);
string madvas2temp = null;
while ((madvas2temp = madvas2.ReadLine()) != null)
{
if (Regex.IsMatch(madvas2temp, regMatch))
{
found = !found;
}
if (found)
{
if (Regex.IsMatch(madvas2temp, "======"))
{
found = false;
break;
}
System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\madavas2.txt", true);
file.WriteLine(madvas2temp);
file.Close();
}
}
madvas2.Close();
/* create mdavas1.txt for persistent route*/
StringReader madvas1 = new StringReader(readtext);
string madvas1temp = null;
regMatch = "Persistent Routes:";
while ((madvas1temp = madvas1.ReadLine()) != null)
{
if (Regex.IsMatch(madvas1temp, regMatch))
{
found = !found;
}
if (found || last)
{
if (Regex.IsMatch(madvas1temp, "======"))
{
found = false;
break;
}
System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\madavas1.txt", true);
file.WriteLine(madvas1temp);
file.Close();
}
}
madvas1.Close();
List<string[]> sl = new List<string[]>();
string temp2;
string[] delim = { " " };
Int32 testvalue = 0;
using (StreamReader sr = new StreamReader("d:\\madavas2.txt"))
{
while ((temp2 = sr.ReadLine()) != null)
{
sl.Add(temp2.Split(delim, StringSplitOptions.RemoveEmptyEntries));
// testvalue=int32.parse(s1(0));
}
testvalue = Int32.Parse(sl[2][4]);
}
deletefiles();
}
}
try to call
right afterCode:System.Diagnostics.Process.WaitForExit();
Code:System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
System.Diagnostics.Process.WaitForExit()