asp.net mvc file path issue \ - asp.net

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("\\", #"\");

"\\" 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?

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?

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");

Related

Create list item that contains a file location with "C:\" [duplicate]

I run R on Windows, and have a csv file on the Desktop. I load it as follows,
x<-read.csv("C:\Users\surfcat\Desktop\2006_dissimilarity.csv",header=TRUE)
but the R gives the following error message
Error: '\U' used without hex digits in character string starting "C:\U"
So what's the correct way to load this file. I am using Vista
replace all the \ with \\.
it's trying to escape the next character in this case the U so to insert a \ you need to insert an escaped \ which is \\
Please do not mark this response as correct as smitec has already answered correctly. I'm including a convenience function I keep in my .First library that makes converting a windows path to the format that works in R (the methods described by Sacha Epskamp). Simply copy the path to your clipboard (ctrl + c) and then run the function as pathPrep(). No need for an argument. The path is printed to your console correctly and written to your clipboard for easy pasting to a script. Hope this is helpful.
pathPrep <- function(path = "clipboard") {
y <- if (path == "clipboard") {
readClipboard()
} else {
cat("Please enter the path:\n\n")
readline()
}
x <- chartr("\\", "/", y)
writeClipboard(x)
return(x)
}
Solution
Try this: x <- read.csv("C:/Users/surfcat/Desktop/2006_dissimilarity.csv", header=TRUE)
Explanation
R is not able to understand normal windows paths correctly because the "\" has special meaning - it is used as escape character to give following characters special meaning (\n for newline, \t for tab, \r for carriage return, ..., have a look here ).
Because R does not know the sequence \U it complains. Just replace the "\" with "/" or use an additional "\" to escape the "\" from its special meaning and everything works smooth.
Alternative
On windows, I think the best thing to do to improve your workflow with windows specific paths in R is to use e.g. AutoHotkey which allows for custom hotkeys:
define a Hotkey, e.g. Cntr-Shift-V
assigns it an procedure that replaces backslashes within your Clipboard with
slaches ...
when ever you want to copy paste a path into R you can use Cntr-Shift-V instead of Cntr-V
Et-voila
AutoHotkey Code Snippet (link to homepage)
^+v::
StringReplace, clipboard, clipboard, \, /, All
SendInput, %clipboard%
My Solution is to define an RStudio snippet as follows:
snippet pp
"`r gsub("\\\\", "\\\\\\\\\\\\\\\\", readClipboard())`"
This snippet converts backslashes \ into double backslashes \\. The following version will work if you prefer to convert backslahes to forward slashes /.
snippet pp
"`r gsub("\\\\", "/", readClipboard())`"
Once your preferred snippet is defined, paste a path from the clipboard by typing p-p-TAB-ENTER (that is pp and then the tab key and then enter) and the path will be magically inserted with R friendly delimiters.
Replace back slashes \ with forward slashes / when running windows machine
I know this is really old, but if you are copying and pasting anyway, you can just use:
read.csv(readClipboard())
readClipboard() escapes the back-slashes for you. Just remember to make sure the ".csv" is included in your copy, perhaps with this:
read.csv(paste0(readClipboard(),'.csv'))
And if you really want to minimize your typing you can use some functions:
setWD <- function(){
setwd(readClipboard())
}
readCSV <- function(){
return(readr::read_csv(paste0(readClipboard(),'.csv')))
}
#copy directory path
setWD()
#copy file name
df <- readCSV()
Replacing backslash with forward slash worked for me on Windows.
The best way to deal with this in case of txt file which contains data for text mining (speech, newsletter, etc.) is to replace "\" with "/".
Example:
file<-Corpus(DirSource("C:/Users/PRATEEK/Desktop/training tool/Text Analytics/text_file_main"))
I think that R is reading the '\' in the string as an escape character. For example \n creates a new line within a string, \t creates a new tab within the string.
'\' will work because R will recognize this as a normal backslash.
readClipboard() works directly too. Copy the path into your clipboard
C:\Users\surfcat\Desktop\2006_dissimilarity.csv
Then
readClipboard()
appears as
[1] "C:\\Users\\surfcat\\Desktop\\2006_dissimilarity.csv"
A simple way is to use python.
in python terminal type
r"C:\Users\surfcat\Desktop\2006_dissimilarity.csv"
and you'll get back
'C:\Users\surfcat\Desktop\2006_dissimilarity.csv'

Replace substring not working [duplicate]

This question already has an answer here:
C# how to replace Slash Quote \"
(1 answer)
Closed 8 years ago.
I am working in asp.net mvc. want to replace \ character to / character. But it is not working.
Let
string path="D:\Qamar\Cartoons\Image.jpg";
path=path.Replace("\","/");
I get error in second line. Please help how to replace.
Try this:
string path="D:\Qamar\Cartoons\Image.jpg";
path=path.Replace("\\","/");
You need to escape the backslash in the first argument for it to be treated as...a backslash (i.e. "\\" instead of "\").
You need to escape back-slashes. The easiest way is to prefix your string with #:
path=path.Replace(#"\","/");
Another method is to escape it with another backslash:
path=path.Replace("\\","/");
try this
th=path.Replace("\\","/")
\ is a special escape character in string literals in c#.
You can precede the string with # to make it verbatim or escape the \ with another \:
path=path.Replace(#"\","/");
or
path=path.Replace("\\","/");
\ is escape character, so your code will not even compile use # or \\ to make the code compile. then it will work
string path=#"D:\Qamar\Cartoons\Image.jpg";
path=path.Replace(#"\","/");
or
string path="D:\\Qamar\\Cartoons\\Image.jpg";
path=path.Replace("\\","/");
but if you are working with Path or URI you can use the inbuilt C# methods to do it like below
System.Uri uri1 = new Uri(#"D:\Qamar\Cartoons\Image.jpg");
string whatYouWant = uri1.AbsolutePath; //Result is: "D:/Qamar/Cartoons/Image.jpg"

How to replace '/' with '\\' using QString replace()?

Can anyone help with following? Suppose I have a QString with a filepath stored of a certain file, I want to replace the /(slashes) from it with \\(double backslashes) I tried:
mystring.replace("/","\\");
But it only puts a single \ instead of \\
String before replacement: D:/myfiles/abc.zip
String after replacement: D:\myfiles\abc.zip
Expected string: D:\\myfiles\\abc.zip
You need to use:
mystring.replace("/","\\\\");
The compiler uses \ as an escape character in strings (for things like \t, \n or \r) so that \\ is actually turned into \. If you need two backslashes, you need to start with four.
If you want to convert paths to Windows format, you could simply use QDir::toNativeSeparators():
qDebug() << QDir::toNativeSeparators("c:/windows/path"); // Prints "c:\windows\path"
1) Why do you want to replace them, AFAIR forward slashes work in file operations too (irrespective of OS).
2) Did you try to read the docs - for example why \\ results in single backslash?

ConfigurationManager.AppSettings convert "\n" to "\\n" why?

I have a AppSetting in web.config.
<add key="key" value="\n|\r"/>
When i read it by ConfigurationManager.AppSettings["key"] it gives "\\n|\\r".
Why ?
In the debugger, becuase the backslash is a special character used for things like tabs (\t) and line endings (\n), it has to be escaped by the use of another backslash. Hence any text that contains an actual \ will be displayed as \. If you print it out to a file or use it in any other way, you will find your string only contains the one .
This isn't ConfigurationManager doing anything.
The backslash escaping syntax is only recognized inside of string literals by the C# compiler. Since your string is being read from an XML file at runtime, you need to use XML-compatible escaping (character entities) in order include those characters in your string. Thus, your app settings entry should look like the following:
<add key="key" value="&x10;|&x13;"/>
Because 10 and 13 are the hex values for linefeed and carriage return, respectively.
Like cjk said, the extra slash is being inserted by the debugger to indicate that it is seeing a literal slash and not an escape sequence.
I solved the same problem with a string replacement.
Not beautful.. but works!
ConfigurationManager.AppSettings["Key"].Replace("\\n", "\n")
string str = "\n";// means \n
string str1 = #"\n";// means \\n
From the AppSettings, It seems that when you extract the key's value, # is internally wrapped.. It is done by the compiler not runtime.

ActiveXObject("Shell.Application") - how to pass arguments with spaces?

I run exe from my asp.net with JavaScript using ActiveXObject. It runs successfully, except parameters:
function CallEXE() {
var oShell = new ActiveXObject("Shell.Application");
var prog = "C:\\Users\\admin\\Desktop\\myCustom.exe";
oShell.ShellExecute(prog,"customer name fullname","","open","1");
}
Example, I pass that like parameters,[1] customer name,[2] fullname, but after space character, Javascript perceive different parameter.
How can I fix?
ShellExecute takes the 2nd parameter to be a string that represents all the arguments and processes these using normal shell processing rules: spaces and quotes, in particular.
oShell.ShellExecute(prog,"customer name fullname",...)
In this case the 3 parameters that are passed are customer, name, fullname
oShell.ShellExecute(prog,"customer 'a name with spaces' fullname",...)
As corrected/noted by Remy Lebeau - TeamB, double-quotes can be used to defined argument boundaries:
oShell.ShellExecute(prog,'customer "a name with spaces" fullname',...)
In this case the 3 parameters that are passed are customer, a name with spaces, fullname
That is, think of how you would call myCustom.exe from the command-prompt. It's the same thing when using ShellExecute.
Happy coding.
Try escaping your spaces with a backslash. The cmd.exe cd command does this, maybe you'll get lucky and it'll work here as well...
oShell.ShellExecute(prog,"customer a\ name\ with\ spaces fullname", ...)

Resources