Reputation: 14389
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?
Upvotes: 1
Views: 1039
Reputation: 23093
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".
Upvotes: 1