I'm creating a console application in C#, and I want to check if a specific file (foo.exe). But when the path contains spaces (C:\A Folder With Spaces\) it checks if foo.exe exists at this directory: C:\A.
Question: How can I check inside of a folder that contains spaces?
It looks like you are passing the name of the file as command-line parameter. In this case the split at the space is done by Windows cmd command processor when you pass C:\A Folder With Spaces\ as parameter. To fix this, enclose the file name in doublequotes:
c:\test>myprog.exe "C:\A Folder With Spaces\foo.exe"
If (File.Exists(#"C:\A Folder With Spaces\foo.exe")
{
//the # sign makes the spaces be taken literally.
}
Sounds like you're supplying the path as an argument to the console application? In which case enclose the path argument in quotes
Related
I have a textbox where I put the name of a file that contains the forward-slash character '/'
When I grab the text from the textbox the '/' is automatically replaced with '\' and it obviously can't find the file, for example:
I write C:\Users\Temp\my/file.txt
I get C:\Users\Temp\my\file.txt
So, instead of opening "my/file.txt", it searches for a directory "my" which contains "file.txt"
How can I solve this?
If you're using Windows (which I assume you are, given the paths in your example), then / is an invalid character for a filename:
The easiest solution here would be to remove the slash from the filename as this is likely to cause further issues down the line.
I am writing a utility that edits .docx files. I've made it so that when the user right clicks on the correct type of file, it automatically makes the changes and saves the document with a bit of text appended to the file name. All of this works great, except for the fact that I am receiving heavily truncated file names. If the file name contains more than one word, the string passed to the program is has most of its characters replaced by a single ~. Is there any way to either read the original file name, or have the parameter be the full string?
I found the solution to what I was trying to do. I ended up using the C# method Path.GetFullPath.
string path = Path.GetFullPath(originalpath);
This outputs the full file name as opposed to the truncated one.
http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
File.getCanonicalPath will give you what you want
http://msdn.microsoft.com/en-us/library/aa988183(v=vs.80).aspx
Put the whole path between two double quot; like:
var fn = "\"C:\\Path With Spaces And Special Characters\\#\\to\\My File.docx\"";
// send fn as an argument to the other process
i have to start process which is placed inside Program Files. But the problem is that Process.Start does not taking space in path.
Process regeditProcess = Process.Start("regedit.exe", "/s C:\\Program Files\\Test Folder\\sample.reg");
Path:
C:\\Program Files\\Test Folder\\sample.reg
there is a space between Program and Files in 'Program Files'.
Thats my problem. How to avoid space?
You should pass command line arguments, containing spaces, in quotes ("), like this:
Process regeditProcess = Process.Start("regedit.exe", "/s \"C:\\Program Files\\Test Folder\\sample.reg\"");
Process.Start is not the problem here, the problem is that regedit.exe doesn't accept spaces in the parameter. Put it into quotes:
Process.Start("regedit.exe", "/s \"C:\\Program Files\\Test Folder\\sample.reg\"");
also, you should use %ProgramFiles% or something equivalent to get the program files folder instead of hardcoding "C:\\Program Files".
You can do something like this to get Program files
Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles)
Here is more detailed code
if(Environment.Is64BitOperatingSystem)
{
Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
}
else
{
Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles)
}
The proper thing to do would be to quote the path that contains spaces. So the argument string should be like this:
"/s \"C:\\Program Files\\Test Folder\\sample.reg\""
Though when working with paths, you generally should always use verbatim literal strings.
#"/s ""C:\Program Files\Test Folder\sample.reg"""
Otherwise, you could convert the path using 8.3 names. I don't know of any methods to do this for you in the framework but the rules are simple. If you have a long name that is longer than 6 characters, you take the first 6 non-space characters and append it with tilde (~) followed by a number (usually starting with 1). If multiple files have the same 6 characters, the number is incremented in alphabetical order. So in your case it could be written:
#"/s C:\Progra~1\TestFo~1\sample.reg"
If I have an executable called app.exe which is what I am coding in C#, how would I get files from a folder loaded in the same directory as the app.exe, using relative paths?
This throws an illegal characters in path exception:
string [ ] files = Directory.GetFiles ( "\\Archive\\*.zip" );
How would one do this in C#?
To make sure you have the application's path (and not just the current directory), use this:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getcurrentprocess.aspx
Now you have a Process object that represents the process that is running.
Then use Process.MainModule.FileName:
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule.filename.aspx
Finally, use Path.GetDirectoryName to get the folder containing the .exe:
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx
So this is what you want:
string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + #"\Archive\";
string filter = "*.zip";
string[] files = Directory.GetFiles(folder, filter);
(Notice that "\Archive\" from your question is now #"\Archive\": you need the # so that the \ backslashes aren't interpreted as the start of an escape sequence)
Hope that helps!
string currentDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string archiveFolder = Path.Combine(currentDirectory, "archive");
string[] files = Directory.GetFiles(archiveFolder, "*.zip");
The first parameter is the path. The second is the search pattern you want to use.
Write it like this:
string[] files = Directory.GetFiles(#".\Archive", "*.zip");
. is for relative to the folder where you started your exe, and # to allow \ in the name.
When using filters, you pass it as a second parameter. You can also add a third parameter to specify if you want to search recursively for the pattern.
In order to get the folder where your .exe actually resides, use:
var executingPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
As others have said, you can/should prepend the string with # (though you could also just escape the backslashes), but what they glossed over (that is, didn't bring it up despite making a change related to it) was the fact that, as I recently discovered, using \ at the beginning of a pathname, without . to represent the current directory, refers to the root of the current directory tree.
C:\foo\bar>cd \
C:\>
versus
C:\foo\bar>cd .\
C:\foo\bar>
(Using . by itself has the same effect as using .\ by itself, from my experience. I don't know if there are any specific cases where they somehow would not mean the same thing.)
You could also just leave off the leading .\ , if you want.
C:\foo>cd bar
C:\foo\bar>
In fact, if you really wanted to, you don't even need to use backslashes. Forwardslashes work perfectly well! (Though a single / doesn't alias to the current drive root as \ does.)
C:\>cd foo/bar
C:\foo\bar>
You could even alternate them.
C:\>cd foo/bar\baz
C:\foo\bar\baz>
...I've really gone off-topic here, though, so feel free to ignore all this if you aren't interested.
Pretty straight forward, use relative path
string[] offerFiles = Directory.GetFiles(Server.MapPath("~/offers"), "*.csv");
I am using the ConfigurationManager.AppSetting["blah"].ToString() method to get the path to the folder that contains the files I'm needing. But I'm throwing an UnsupportedFormatException on the path when it tries to use Directory.GetFiles(path).
The returning value has the escape characters included and I'm not sure how to keep it from returning the extra characters. This is what the path looks like after it is returned:
\\\\\\\\C:\\\\folder1\\\\folder2
I needed to remove the first four "\" to give it a correct path.
you have extra back-slash \ at the beginning of your path.
try putting "C:\folder1\folder2" instead of "\\C:\folder1\folder2" in your config file, and it will work.