Process() - The system cannot find the specified file

Jalkhov

New member
Joined
Mar 24, 2021
Messages
3
Programming Experience
3-5
Hi, I'm trying to compress a folder with RAR through a button, when I press it for the first time, everything runs correctly, but when I try to do it a second time I get the title error, I think the problem is with the process but I don't know exactly how to fix it.

This is my code

C#:
            string directory = Directory.GetCurrentDirectory();
            string RarExe = Path.Combine(directory, "Rar.exe");
            Directory.SetCurrentDirectory(projectdirval.Text); // This is for work inside the project directory
            Process p= new Process();
            p.StartInfo.FileName = RarExe;

            p.StartInfo.Arguments = String.Format("a -r {0}", "c:\\MyFile.rar");
            p.Start();
            p.WaitForExit();

I am thinking that the problem is that when I perform the action a first time, the Rar.exe file starts to be used, and when I try to perform the action a second time the file is still in use and is not available or something like that, but it is strange, because I check the task manager and the process is finished a second time, it should be clarified that I can do that action as many times as I want when I finish running the program and reopen it, but not when I try to do it more than once in a "session". Another thing, the error gives me specifically in the line of p.Start();
 
Last edited by a moderator:
Solution
It is allowed to tell what line in your code that throws the exception, it makes it easier to get help.
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.
So if you set this again with a relative path it will keep adding the relative part, and you get a path that doesn't exist.

You should also Dispose the Process object when you are finished with it.
It is allowed to tell what line in your code that throws the exception, it makes it easier to get help.
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.
So if you set this again with a relative path it will keep adding the relative part, and you get a path that doesn't exist.

You should also Dispose the Process object when you are finished with it.
 
Solution
On the first run, you have changed the current directory. So on the second run you are in a different directory and it stands to reason it can not find Rar.exe there.
 

Latest posts

Back
Top Bottom