Could not find Path - c#

I am giving path to my file which i want to read in my program like this, path = "c:/users/abcd/desktop/read.txt" but while debugging i found it's showing like c://users//abcd//desktop//read.txt which is an invalid path.
Is there something i am missing or how should i go about declaring a path.

I would just use:
#"c:\user\abcd\desktop\read.txt"
(note the use of the #). Another likely issue is permissions; does the app have access to user acbd's desktop? You might also look at:
Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory)
and
Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)

Try path = #"c:\user\abcd\desktop\read.txt"
I haven't worked with C# for a while, but is it possible that the debugger is just showing you the escaped version of the string but the string itself is ok? Click on the magnifying glass next to the string in your debugger to show the text visualizer and see if it looks ok there.

I'm guessing that these are backslashes. This is the debugger's way of showing certain characters. A newline would show up as "\r\n", a tab as "\t", and a backslash as "\\". The string "a\b\c\d" will show up in the debugger as "a\\b\\c\\d" because that is what you have to type into code to get the actual string "a\b\c\d". When the debugger shows doubled backslashes, each \ is actually a single .

The debugger is just showing you the escaped version of the file. I note that one of your paths contains the string "user" while the other contains "users" is the pluralization the problem?

Related

Replace double backslashes with single backslash

I have created a search function within a media player that uses a list-box to output the data and when a user clicks a song I want that song to play. However, when I select a song for some reason I get two black slashes instead of one. Please help. I've tried replacing them already.
string path = #"C:\Users\Username\Music\";
path = path.Replace(#"\\", #"\");
string selectedsong, filetoplay;
selectedsong = listBox1.SelectedItem.ToString();
filetoplay = path + selectedsong + ".mp3";
Form1.wplayer.URL = filetoplay;
What I'm currently getting at the moment is C:\\Users\\Username\\Music\\Song.mp3 and as a result the song won't play
That Replace in your code is doing nothing because there are no double backslashes in your string... As others have already pointed out, it's just a matter of debugger visualization so you can copy that and use it in your code, for example.
So, if you do this:
You see the double backslash there but it's not in the actual string, of course. See what you get in the console:
My advice is that you simulate a double click on the selected file by running this:
System.Diagnostics.Process.Start(filetoplay);
It should open your default mp3 player. I think it will give you an error due to missing file, wrong format or something. If it plays, then the bug is on the player part of your code and you can stop worrying about the double slashes. :)
It shows two backslashes in the variable value because the ­\ is escaped. If you print the variable value to console, you should find that it has only one backslash.
Try to use Path.Combine
link to msdn.microsoft.com
P.S. Single \ is "escape sign" and is used with "special charakters" ex: "\t" is "tab" and "\n" is "new line", that means, when you want to post "\" in your string, you must post "\".
P.S.2 When you are creating "path" use "Patch.Combine".

how to make string returned by ResourceManager.GetString not verbatim

Okay, I have a string
string textToShow = "this\nrocks"
which when put in label in winforms window will then show
this
rocks
Which is the result I'd like to get. Now, instead of setting the textToShow in the code, I set it in the resource file. When I tried to get the value from resource file using
Properties.Resources.ResourceManager.GetString("textToShow");
the whole string instead will be treated as verbatim, showing
this\nrocks
when put in a label in a winforms window. This is not the result i'm looking for. What's the best way to store strings with special characters in resource file then? I can do string replace for every special characters, like
string.Replace(#"\n", "\n");
but then I need to replace every special characters whenever I call method ResourceManager.GetString, which I think is not the most elegant solution. If there is some ways to make string returned from method ResourceManager.GetString not verbatim, please do tell me.
Thanks
This was already answered here: StackOverflow: How to deal with newline
Basically you have two useful options:
Use shift + enter in the resource manager text editer to add a new line.
Or use String.Format() to replace {0} with \n on read.
The .Net 4.5 framework has the unescape functionality as shown here:
using System.Text.RegularExpressions;
Regex.Unescape(Properties.Resources.ResourceManager.GetString("textToShow"));
solves your issue. Now you can use \n and \u in the resource files.
On the resource editor type "this<shift+enter>rocks" as the resource value.

how to capture double quotes in a ASP.NET page?

I have a text box on my web page which I want to allow the user to type double quotes in.
It's a web report kind of deal.
So users can enter in
breaking
or
"breaking" if they want to capture the exact word.
The issue is, when I look at the variable when I try to do "breaking", I keep getting this:
"\"breaking bad\""
I need to get "breaking bad" ...
How do I go about doing this properly? Is there NOT a way to do this and therefore I have to simply do a replace?
The backslashes that you are seeing are just Visual Studio's way of displaying quotes inside a string when using the debugger to inspect a variable. You are getting the correct value. If you use the 'Text Visualizer' feature in the debugger to examine the string, you'll see that the backslashes aren't really there.
when I look at the variable when I try to do "breaking", I keep getting this:
"\"breaking bad\""
If by "looking" you mean in the debugger, then what you are seeing is the debugger escaping the quotes by adding the backslashes. If you output that string to the console, a form, database, etc. It will be surrounded by quotes just as you'd expect it to.
This is how the debugger displays it. If you want to see the real version quickly, click on the loupe next to the value you're displaying in the debugger.

Reading a string from a form as a verbatim string, \\ showing instead of \

I have the following property:
public string InstanceName
{
get
{
return cbServerInstanceName.Text;
}
}
where the input for
cbServerInstanceName.Text = "ServerName\ PcName"
This is showing up as
"ServerName\\ PcName"
I tried using the string.replace but couldnt get it to work.
Any ideas?
When you are using C# and looking at strings in the debugger, it will escape certain characters and \ is one of them; it will show in the debugger as \\ but at runtime and not viewed in the debugger, it will be converted to a single \
I'm going to take a stab at it and assume you're seeing "ServerName\\ PcName" using the debugger view in Visual Studio. Since it is showing you "a string\\" instead of a #"string literal\", you will see your slashes escaped. Just as you would with "\r\n" if you added a new-line.
If you can try printing your value to a MessageBox, or Debug or the Console. It should appear as you expect. fingers crossed

Validating File Path w/Spaces in C#

I'm something of a n00b at C# and I'm having trouble finding an answer to this, so if it's already been answered somewhere feel free to laugh at me (provided you also share the solution). :)
I'm reading an XML file into a GUI form, where certain elements are paths to files that are entered into TextBox objects. I'm looping through the controls on the form, and for each file path in each TextBox (lol there's like 20 of them on this form), I want to use File.Exists() to ensure it's a valid file.
The problem with this is that the file path can potentially contain spaces, and can potentially be valid; however File.Exists() is telling me it's invalid, based entirely on the spaces. Obviously I can't hard-code them and do something like
if (File.Exists(#"c:\Path To Stuff"))
and I tried surrounding the path with ", like
if (File.Exists("\"" + contentsOfTextBox + "\""))
but that didn't make a difference. Is there some way to do this? Can I escape the spaces somehow?
Thank you for your time. :)
File.Exists works just fine with spaces. There is something else giving you a problem I'll wager.
Make sure your XML reader isn't failing to read the filename (parts of XML do not allow spaces and some readers will throw an exception if they encounter one).
#"c:\Path To Stuff"
The above could be a directory not a file!
Hence you would want to use Directory.Exists!
#"c:\Path To Stuff\file.txt"
If you did have a file on the end of the path then you would use File.Exists!
As the answer said, File.Exists works with spaces, if you are checking for existence of a Directory however, you should be using Directory.Exists
What is the exact error that you get when File.Exists says it is invalid?
I suspect that you are passing a path to a directory and not a file, which will return false. If so, to check the presence of a directory, use Directory.Exists.
To echo Ron Warholic: make sure the process has permissions over the target folder. I just ran into the same "bug" and it turned out to be a permissions issue.
Did you remember to replace \ with \\ ?
You need to use youtStringValue.Trim() to remove spaces leading/trailing, and Replace to remove spaces in the string you do not want.
Also, rather use System.IO.Path.Combine rather to combine these strings.
You can use # on string variables:
string aPath = "c:\Path To Stuff\text.txt";
File.Exists(#aPath);
That should solve any escape character problems because I don't think this really looks like the spaces being the problem.
hi this is not difficult if you can convert the name of the path to a string array then go through one by one and remove the spaces
once that is done just write() to the screen where you have the files, if it is xml then your xmlmapper will suffice
file.exists() should only be used in certain circumstances if you know that it does exist but not when there can be space chars or any other possible user input

Categories

Resources