Question reading text file using System.IO.File.ReadAllText - problem

madhugp

Member
Joined
Nov 27, 2017
Messages
20
Programming Experience
5-10
Dear all,

I am trying to read the text file which has exe files folder path ( d:\retail\ ) from menu items.
I am using the following code..
C#:
private void rMSALESToolStripMenuItem_Click(object sender, EventArgs e)
{
    string str = System.IO.File.ReadAllText(@"d:\retail\rep_path.txt");
    Process myProcess = new Process();
    myProcess.StartInfo.FileName = str + "rm_sales.exe";
    myProcess.Start();
}
he contents of rep_path file is d:\retail\

but when I tried to call executable file from menu It displays the following error
"the system cannot find the specified file".
plz find the attached pic. This code is working in my own pc and in other client machines.
But in one client machine this is showing this error. I am not able to understand the problem.
Also I am using "system.io" name space.
plz give the solution.

thanks and regards
madhu
 

Attachments

  • error.jpg
    error.jpg
    873.6 KB · Views: 42
Last edited by a moderator:
If the error message says that the file cannot be found then the file cannot be found. The fact that you're showing a photo of a Windows crash report, rather than a screenshot of an unhandled exception in the VS debugger, suggests that this is occurring after deployment. Did it work in testing? If not then why are you even running it after deploying? If so then the code obviously works, so the issue is obviously that the file is not present at the specified path. Either the path is not what you think it is or the file is simply not at the expected path. One potential issue that comes to mind is that your text file has a line break in it, so your path can't possibly be valid. That's just a guess though, because that's all we actually have to work with.

In other news, you should be using Path.Combine to combine partial paths. Also, you're using a much more verbose method than is required to start the new process.
C#:
private void rMSALESToolStripMenuItem_Click(object sender, EventArgs e)
{
    var textFilePath = @"D:\retail\rep_path.txt";
    var exeFolderPath = File.ReadAllText(textFilePath);
    var exeFilePath = Path.Combine(exeFolderPath, "rm_sales.exe");
    
    Process.Start(exeFilePath);
}
 
Out of curiosity, why is this setting stored in a separate .txt file? Why not in the app.config?
 
I have to agree with Skydiver but, if you are going to have a text file containing one line that is a file path, you can write your code such it will be tolerant of a spurious line break at the end. Instead of this:
C#:
var exeFolderPath = File.ReadAllText(textFilePath);
you could use this:
C#:
var exeFolderPath = File.ReadLines(textFilePath).First();
That will read just the content of the first line, ignoring any line break at the end and even any additional text after that line break.
 
Back
Top Bottom