문제

I am creating a file:

 String strPath = Application.StartupPath;
....
 string filePath = strPath + "\\Books" + ".zpl";

later on the path is send to command line to be copied to parrallel port (for printing..)

string command = "copy " + filePath + " lpt1"; //prepare a string with the command to be sent to the printer
// The /c tells cmd that we want it to execute the command that follows, and then exit.
System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

sinf.UseShellExecute = false;
sinf.CreateNoWindow = true;

System.Diagnostics.Process p = new System.Diagnostics.Process(); // new process            
p.StartInfo = sinf;//load start info into process. 
p.Start();

My problem is that Application.StartupPath may contain folder names with spaces (like e.g: Document and Settings). In that case I have to put quotes on that folder names. However Application.StartupPath=> the address path resulting will be known after the installation of the program in the client, so I don't if and how many folder names need quoting

Any ideas?

도움이 되었습니까?

해결책

Even if the quotes are not always required the do not harm => use them all the time.

BUT you should not use Application.StartupPath for such a task.
You can use something like the following:

var dir = Path.Combine(Environment
    .GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyProgram");
if(!Directory.Exists(dir))
    Directory.CreateDirectory(dir);
var path = Path.Combine(dir, "books.zpl");

In the startup path you will NOT have write privileges if you are not an admin and the application is installed to "program files".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top