tags:

views:

33

answers:

3

i am using this to create a new folder

 System.IO.Directory.CreateDirectory(@" + somevariable); 

the thing is that when i enter the folder c:\newfolder\newfolder in the textbox and is trying to recieve the value up in the controller it is replaced with double slash( \) c:\\newfolder\\newfolder. how would i prevent \ quotes from coming in the path

Secondly the string.replace is also not working for replacing \ with \\

  string strText = OrganMeta.vcr_MetaValue;  
  string gf = strText.Replace("\\", @"\");
+1  A: 

Are you sure it's replaced it with \\? If you hover over the variable it will appear to have \\ where there should be a single \ but if you view it in the text visualizer it will show correctly.

Not sure what you mean by string.replace is not working...?? Can you give an example of the code that's not working?

Andy Robinson
the thing is that if i hard cord c:\newfolder\newfolder it works but if i don't then it doesn't works and it shows \\ in the string
mazhar kaunain baig
I've created a test MVC2 project with a single textbox and a controller that receives a FormCollection. If I then put the value of the submitted text into ViewData and show it in the View it's correct, I've also included a count of the number of '\' chars in the string and it's correct. Can you try the same thing in a new MVC project and see if it works?Your replace is correct it will look for \\ and replace with \.
Andy Robinson
Doh! Cheers Dave, missed that it didn't have a @ before the \\.
Andy Robinson
+2  A: 

"\\" is equivalent to a string of one character, a backslash. @"\" is also equivalent to a single character, a backslash.

so your Replace method is replacing one form of a backslash with a different form.

try this:

string gf = strText.Replace( @"\\", @"\" );

OR

string gf = strText.Replace( "\\\\", "\\" );

as far as the folder thing goes, Andy is right, it will show a double-backslash in the IDE when in fact there is only one in the string. is there an error when Directory.CreateDirectory() is called? or is the folder created?

dave thieben
A: 

Slashes don't get doubled between the form submit and your controller action.

It's far more likely that you're viewing the result in the debugger or another context that shows two slashes to allow you to distinguish between escaped characters (\n) and a literal slash ().

Write the string to the debug window to verify this.

System.Diagnostics.Debug.WriteLine("SomeText"); 
David Lively