Problem in receiving Path as a string
-
Hello, First of all thank you for this magnificent web site. I am new to C# and trying some basics on my own. As you may know if you try to define a path variable as a string in C# you must use "\\" to indicate backslashes: i.e.: string filePath = "C:\\Windows\\Temp\\file.txt"; But what if I should received the path information as a parameter to my main() method? I have tested it many times in simple applications but I could not open a SaveFileDialog in the specified directory. Please consider the following examples that sets SaveFileDialog.InitialDirectory property. The operation of the application is simple, just display a SaveFileDialog pointing to the location passed to the Form1: This one works pretty fine: /// /// The main entry point for the application. /// [STAThread] static void Main() { string initialDirectory = "C:\\Windows\\Temp"; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(initialDirectory )); } and Form1 class has the following codes inside: public partial class Form1 : Form { private string initialDirectory; public Form1(string initialDirectory) { this.initialDirectory = initialDirectory; InitializeComponent(); InitializeSvFDlg(); } private void InitializeSvFDlg() { this.saveFileDialog1.InitialDirectory = this.initialDirectory; this.saveFileDialog1.ShowDialog(); } } -------------------------------------------------------------- I observed that SaveFileDialog does not show the passed directory when I use the following method in main() and start the program from Command Prompt with SvFileDlgShow.exe "C:\\Windows\\Temp" command: /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { string initialDirectory = args[0]; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(initialDirectory)); } Form1 class is the same. -------------------------------------------- But the executable works fine if I use SvFileDlgShow.exe "C:\Windows\Temp" as the start-up command. Is this the i
-
Hello, First of all thank you for this magnificent web site. I am new to C# and trying some basics on my own. As you may know if you try to define a path variable as a string in C# you must use "\\" to indicate backslashes: i.e.: string filePath = "C:\\Windows\\Temp\\file.txt"; But what if I should received the path information as a parameter to my main() method? I have tested it many times in simple applications but I could not open a SaveFileDialog in the specified directory. Please consider the following examples that sets SaveFileDialog.InitialDirectory property. The operation of the application is simple, just display a SaveFileDialog pointing to the location passed to the Form1: This one works pretty fine: /// /// The main entry point for the application. /// [STAThread] static void Main() { string initialDirectory = "C:\\Windows\\Temp"; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(initialDirectory )); } and Form1 class has the following codes inside: public partial class Form1 : Form { private string initialDirectory; public Form1(string initialDirectory) { this.initialDirectory = initialDirectory; InitializeComponent(); InitializeSvFDlg(); } private void InitializeSvFDlg() { this.saveFileDialog1.InitialDirectory = this.initialDirectory; this.saveFileDialog1.ShowDialog(); } } -------------------------------------------------------------- I observed that SaveFileDialog does not show the passed directory when I use the following method in main() and start the program from Command Prompt with SvFileDlgShow.exe "C:\\Windows\\Temp" command: /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { string initialDirectory = args[0]; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(initialDirectory)); } Form1 class is the same. -------------------------------------------- But the executable works fine if I use SvFileDlgShow.exe "C:\Windows\Temp" as the start-up command. Is this the i
selcuks wrote:
But what if I should received the path information as a parameter to my main() method?
The reason you use \\ is because \ is used to escape characters, for example, \" is how you put a quote. You have another option: @"c:\Program Files\Atalasoft\DotImage 6.0" will work fine, the @ means that \ means \. The \\ actually is turned into \ in your final string ( although the debugger will show \\ ). So, if you pass a parameter, it should be a single \, because \\ is in fact how you tell the compiler to emit a single \, if you've not put @ at the front of the string.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )