Problem with Command Prompt Process

  • Thread starter Thread starter Dyl
  • Start date Start date
D

Dyl

Hi all,

I am having a problem with a command prompt process. It runs the cmd
prompt sucessfully, and changes the directory succesfully, but I can't
get the StartInfo.Arguments to work properly. My code is below, the
first stuff finds the most recently modified folder.

DirectoryInfo Direc = new DirectoryInfo(@"U:\MyFld\October");
DirectoryInfo[] FoldersArr = Direc.GetDirectories();

DateTime MostRecent = FoldersArr[0].LastWriteTime;
DirectoryInfo MostRecentDir = FoldersArr[0];

for (int i = 1; i < FoldersArr.Length; i++)
{
if (MostRecent.CompareTo(FoldersArr.LastWriteTime) < 0)
{
MostRecent = FoldersArr.LastWriteTime;
MostRecentDir = FoldersArr;
}
}

-------------------------Process-----------------------------------

Process proc = new Process();
proc.StartInfo.FileName = "cmd";
proc.StartInfo.WorkingDirectory = @Direc.FullName;
proc.StartInfo.Arguments = "compact /u /s:" +
MostRecentDir.Name;
proc.StartInfo.UseShellExecute = false;
proc.Start();
 
make sure that the directory name is enclosed with double quotes otherwise
spaces in the directory name (e.g. c:\program files\...) will cause
problems.

HTH

Ollie Riches
 
Thanks for your reply Ollie,

But I am not sure where to put the double quotes in my code. You said
around the directory name, but could you specify where in my code?
 
try this:

proc.StartInfo.Arguments = "compact /u /s:\"" + MostRecentDir.Name + "\"";

HTH

Ollie Riches
 
so if you run it from the command line manually it works? if it does try
hardcoding an example in your application to make sure it works and then try
an example from your application manually from the command line - if any of
that makes any sense :)

Ollie Riches
 
I hardcoded a specific case into my code, and it still does not work.
This time the window flashes, so I assume it's executing the command
but when I check to see if the folder is compressed, it is not. Is
there a way to keep the window from going away?

Thanks
 
Are you specifying a full path or a relative path, if a relative path then
try full path.

HTH

Ollie Riches
 
Hi Ollie,

Even when I hardcode something simple like "mkdir test," it still
doesn't work. This is very strange. I've looked at other posts, and
it seems my code is identical. Anyway, thanks again for your help.

-Dylan
 
Rag,

That did it. Thanks for helping me. What does the /c do? Is that
current?

Thanks again.
Dylan
 
Here is another way to build the argument string

variable = @String.Concat("\"", arguments_here, "\"");
 
Back
Top