I am having an issue trying to reference a drive\path on another on the same network as my application.
string LocationPath = "\\servername\F$\FirstDirectory\SecondDirectory\filename.txt";
I would like to use streamreader to capture the contents of this file but can't seem to access it. This is how I reference the directory in file explorer, how can it be done in C#?
Thanks for any input!
You need to escape the backslash:
string LocationPath = "\\\\servername\\F$\\FirstDirectory\\SecondDirectory\\filename.txt";
Or use a verbatim string:
string LocationPath = #"\\servername\F$\FirstDirectory\SecondDirectory\filename.txt";
Related
I am working on UWP, I want to store paths somewhere, and I found ApplicationData.LocalSettings can do it.
But I got an error when I stored the share folder path through it because of the \ and I don't know why. Is there any good solution to it?
Here is the code,just simple:
var path = #"\\192.168.1.1\test";
ApplicationData.Current.LocalSettings.Values.Keys.Contains<string>(path);
ApplicationData.Current.LocalSettings.Values[path] = xxx;
The contains function and add function also throw COMException
UWP ApplicationData LocalSettings
We could reproduce this problem, it looks "\" char is not support but not "", for this scenario, the better way is using "path" string as key to replace path value-key. And I will report this problem.
ApplicationData.Current.LocalSettings.Values["path"] = xxxx;
Or replace the header \\ with empty.
var path = #"192.168.1.1\test";
I tried your code. LocalSettings cannot use path as a key since it contains the \ symbol.
For example,
String path = #"&&192.168.1.1&test"; // I replaced every \ with &
ApplicationData.Current.LocalSettings.Values[path] = "your string";
will save your string correctly.
From an ASP.NET MVC app I am trying to concatenate two paths, a remote server path with a path extracted from database. I am performing below:
string serverPath = #"\\myServer\TempFolder";
string filePath = GetPathFromDatabaseTable();
string finalPath = System.IO.Path.Combine(serverPath, filePath);
GetPathFromDatabaseTable method returns this string:
\\path\\to\\file.pdf
When concatenating using Path.Combine, the result got into finalPath is:
\\path\\to\\file.pdf
So the prefix serverPath \myServer\TempFolder is removed. Why is happening?
You can use the Uri class to achieve combining a remote and a local path:
string serverPath = #"\\myServer\TempFolder";
string filePath = "\\path\\to\\file.pdf";
Uri serverUri = new Uri(serverPath + filePath);
string finalPath = serverUri.LocalPath;
Which returns
\\myserver\TempFolder\path\to\file.pdf
Is the query returning \\path\\to\\file.pdf exactly? or is that only a representation in c# debugger.
You should not store \\ as directory separator into the database field. the \\ is only needed to escape the string when you write it in c#. (unless you are using the #"\")
If you use \\ in the database field, the first \\ will be seen as a rooted path and probably removes the previous path.
Dropping the leading slash at the beginning from filePath works as explained here in the solution.
so if in database is stored as \path\to\file.pdf then when I read from database I drop the leading slash at the beggining, so GetPathFromDatabaseTable method returns:
path\\to\\file.pdf
instead of:
\\path\\to\\file.pdf
so then when combining using System.IO.Path.Combine it works perfectly.
I have a database table that containing file paths of excel files that I import using a C# script.
The script works fine unless the filepath contains spaces e.g. C:\Temp\My Excel File.xls and I get an Illegal characters in path error message. Unfortunately I am not able to change the file names at the source.
If I hard code the file path to be as below it works fine.
String Filepath = #"C:\Temp\My Excel File.xls";
How do I alter this so I can include a string variable that will store the filepath from the database e.g.
String Filepath = //Code to get FilePath from database
StringCorrectedFilePath = #+FilePath;
Thanks in advance of any help
Edit: Issue is caused by files that start with a number creating invalid escape sequence. e.g. C:\Temp\20160611 My Excel File.xls
Edit 2: SOLVED - Error was caused by carriage return characters appearing after the file extension. Please see my answer for the solution.
Whether you do this
String Filepath = #"C:\Temp\My Excel File.xls";
or this
String Filepath = "C:\\Temp\\My Excel File.xls";
the string stored in memory is just C:\Temp\My Excel File.xls, whatever the debugger may tell you. So when you read some string from somewhere (database, file, user input, ...) you don't need to "escape" backslashes. So just use that string.
Path.GetInvalidFileNameChars
FilePath = string.Concat(FilePath.Split(System.IO.Path.GetInvalidFileNameChars())).Trim();
Well you can replace blank space with %20 character and while retrieving replace back with blank space again like (you may as well choose to use regular expression for the same)
String Filepath = #"C:\Temp\My Excel File.xls";
Filepath = Filepath.Replace(" ", "%20");
While retrieving back
string mypath = pathyouhavegotfromDB.Replace("%20", " ");
I think you need to put quotation marks around the path with spaces.
string filepath = #"C:\Temp\My Excel File.xls";
filepath = $"\"{filepath}\"";
Thanks for everyone's help, I tried all of these and unfortunately they didn't work which led me to believe that the issue wasn't what I originally thought.
It turns out that the files causing the Illegal characters in path all had carriage return characters at the end of the file name, after the file extension.
To resolve this I used the following code and now it works perfectly
FilePath = FilePath.TrimEnd('\r', '\n');
Thanks everyone for your help.
Try this:
String StringCorrectedFilePath = #""+ Filepath;
string slink = "\\README.TXT";
string ipath = "C:\\Users\\Crystal\\Documents\\Visual Studio 2010\\Projects\\workspace\\workspace\\bin\\Debug";
string test = lpath+"\\workspace\\"+slink;
string test1 = "C:\\Users\\Crystal\\Documents\\Visual Studio 2010\\Projects\\workspace\\workspace\\bin\\Debug\\workspace\\README.TXT";
string ftpfullpath = myUri.ToString();
WebClient request = new WebClient();
FileStream file = File.Create(#test);
if i write FileStream file = File.Create(#test); i get error illegal characters.
if i write FileStream file = File.Create(#test1); it works!
i think something is wrong with gluing multiple string path values. i've tried also Path.Combine but also doesn't work
In your third line of code, you've used lpath instead of ipath - i am assuming that's a typo
Use this:
string resultPath= Path.Combine(p1, p2);
MSDN Reference
Taking it that lpath is a typo for ipath, the latter adds an extra slash (or set of slashes, depending on the context) before README.TXT.
That is to say,
\\workspace\\README.TXT
becomes,
\\workspace\\\\README.TXT
You're writing "..\\" + "\\...".
A path cannot have two consecutive \s.
The problem is your either your slink or your test. The code you have ends up with the following path:
C:\Users\Crystal\Documents\Visual Studio 2010\Projects\workspace\workspace\bin\Debug\workspace\\README.TXT
Notice the double slash before README.TXT?
This is caused because slink becomes:
\README.TXT
Then you are trying to combine that with:
C:\Users\Crystal\Documents\Visual Studio 2010\Projects\workspace\workspace\bin\Debug\workspace\
You can do 1 of 2 things to fix the problem:
Remove the \\ from the slink
Remove the trailing \\ from the "\\workspace\\" when you try to combine them
I would suggest option 1 - it clearly separates the filename from the path of the file. Having the \\ preceding the file implies that the filename also has its path info
I have shared folder on ubuntu/samba server of my network.
I am running my c# code on Vista , so How can i read file shared on ubuntu/samba server ?
My code :
String errorLogFile = #"\\\\198.168.0.2\\sharedfolder\myfile.wmv";
//throws excetion login fail
StreamReader sr = new StreamReader(errorLogFile);
sr.Read();
streamWriter.Close();
Use the code provided in this answer to authenticate your code for the remote directory.
Update:
Additionally, the combination of escaped backslashes and verbatim strings is a bad idea. Use one of these but not both.
Also, you are missing the backslash after the name of the shared folder.
It should be like this:
String errorLogFile = #"\\198.168.0.2\sharedfolder\" + finaldate + ".wmv";
If the share is configured properly you should be able to access it via \\ubuntumachine\sambasharename just as you would a Windows share.