Blog Stats
  • Posts - 42
  • Articles - 0
  • Comments - 61
  • Trackbacks - 36

 

File path in C#

When you have to pass in a file path to do some processing with the file, try using the following C# code.

string path = Path.Combine(Environment.CurrentDirectory, @"..\..\TestDeserialization.xml");

Where,

The TestDeserialization.xml file is in the Project root directory.

It helps to avoid confusion in file paths.


Feedback

# re: File path in C#

Gravatar This is not entirely correct. The CurrentDirectory does not have to be the Project root directory. It might work while debugging, but is not guarenteed to be the path where the assembly is. It is better to use the Assembly.Location property if you want to get the path relative to the assembly the class was loaded from (using Assembly.GetExecutingAssembly()). But, sometimes you want to use the path of the AppDomain then you use Assembly.GetEntryAssembly(). 2/1/2006 3:06 PM | Don Demsak

# re: File path in C#

Gravatar But Don, what if i dont want to use Reflection? 2/2/2006 4:07 PM | Vin

# re: File path in C#

Gravatar Since CurrentDirectory can be anything, especially if your code is in a dll and is running in both console and web apps, you have to be careful what you use as your starting point for your relative pathing. The only way to be certain is to not enable relative paths, which isn't always an option. Otherwise you have to use something like the Assembly class and go from the only sure starting point, the location of the dll (which isn't even always a sure thing, since ASP.Net does recompile your dlls into a temp folder). 2/3/2006 6:19 AM | Don Demsak

# re: File path in C#

Gravatar How would you list (directory/files/*.asp )using path?

Result sample:

ASPFiles/default.asp
ASPFiles/newpage.asp
Images/mylistofimages.asp 4/11/2006 4:18 AM | Liz

# re: File path in C#

Gravatar Can someone help me? I have a problem...my file is on LAN natwork, on computer named "Server", directory "Documents", file name is "Telgr.doc". I need sintax for the file-path, that I can read this file from my application (from my computer)? In C#......

4/23/2007 8:37 AM | Zoran

# re: File path in C#

Gravatar thnkx for this topic it is very helpful 11/14/2007 2:46 AM | moly

# re: File path in C#

Gravatar Thank you Don, I was looking all over for this command as MSDN tends not to be the most helpful source on their own libraries. 1/9/2008 11:28 AM | Josh

# re: File path in C#

Gravatar hi..
I have problem regarding getting source file path for mozilla..my code is working fine for IE but giving problem with mozilla...can anyone suggest what will be the problem? 1/31/2008 5:19 AM | Mamta

# re: File path in C#

Gravatar hi..
I have problem regarding getting source file path for Mozilla..my code is working fine for IE but giving problem with Mozilla...can anyone suggest what will be the problem? 1/31/2008 5:21 AM | Mamta

# re: File path in C#

Gravatar I have a C# web app, with a file upload object. How do i get the full path of that file on the client's computer, not on the server?

eg:
//"Value" does not give you the full file path like it says:
String fileName = myUploader.Value;
//so then i did this:
String filePath = Path.GetFullPath(fileName);
//but that gave my either "C:\Program Files\Microsoft Visual //Studio 8\Common7\IDE\" OR "C:\Windows\System32\"

//when the file actually sits in "D:\ConfigFilesFolder\"

Any Suggestions? 4/2/2008 5:03 AM | Jacques

# re: File path in C#

Gravatar System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(Application.ExecutablePath))+"\\Files\\video1.wmv" etc 4/8/2008 9:36 AM | Abdullah/Turkey

# re: File path in C#

Gravatar I want this:
StreamReader sr = new StreamReader("\\MachineName\\DirectoryName\\FileName.ini").

When I run the above code, the Exception tells me: "Could not find part of the path 'C:\\MachineName\\DirectoryName\\FileName.ini'.".

Why is it referencing the C: drive?? How do I fix this??

Thanks for any help, as I have been researching this for literally days with no solution working yet. 5/22/2008 4:35 PM | Jeff

# re: File path in C#

Gravatar use
C$\\MachineName\\DirectoryName\\FileName.ini'.".

instead of C: 6/6/2008 1:14 AM | Prashant Mishra

# re: File path in C#

Gravatar // Get the path of file source
string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); 6/12/2008 1:44 AM | Teo( Vietnamese)

# re: File path in C#

Gravatar You can get full path by writing as.....
String fileName = FileUpload1.PostedFile.FileName;


now string filename will contain the actual path of ur file .....
ok do enjoy.............. 9/18/2008 3:52 AM | dhruwaman

# re: File path in C#

Gravatar it is good 11/20/2008 1:23 PM | lhking

# re: File path in C#

Gravatar Hi!
I want to get the file's path in client's computer using FileUpload too. dhruwaman it doesn't work on Firefox do u know another way to do that? 11/21/2008 9:31 AM | auzun

# re: File path in C#

Gravatar Thanks This code is worked for me 1/2/2009 1:06 AM | B.V.S.Nagendra

# re: File path in C#

Gravatar thanks for this code 1/31/2009 3:39 AM | Alex

# re: File path in C#

Gravatar Hello,

I have used the advises up here and I get my path like this :

string DirectoryPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

string LogPath = Path.Combine(DirectoryPath, @"..\..\..\Log\COM4.log");

I get in the watcher the following path :
« C:\\Project\\Protocoles\\AutoTestProtocols\\Application\\bin\\Debug\\..\\..\\..\\Log\\COM4.log »
that looks good to me but provoke the following exception :

mpFile_O[Index] = File.Create(LogPath);
=>
« FileStream n'ouvre pas les périphériques Win32, tels que des partitions de disque et des lecteurs de bande. Évitez d'utiliser "\\.\" dans le chemin. »
(in english, should give something like "Filestream doesn't open Win32 peripherals such as disk partition and band readers. Avoid using \\.\ in the path")

How could I get a relative path without "\\.." ?? 5/28/2009 4:13 AM | Chris

# re: File path in C#

Gravatar Finally found the answer, the path was not the problem and the exception text was completely irrelevant : you cannot name a file "COM" or "COMx" where x is a number, it's reserved by Windows...
How the hell was I supposed to know that ? ^^ 5/28/2009 5:06 AM | Chris

# re: File path in C#

Gravatar Made a different solution but thanks for the idea... this is for C# with visual studio 2005.

Since most c# projects end up in the Bin\Debug folder, it uses up 9 extra characters to get to the root of the project so... I just removed the last 9 characters then combined it with the file name I wanted to use. =)

string path = Environment.CurrentDirectory.ToString();
path = path.Substring(0, path.Length - 9);
path += "Diseases.mdf";
connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename="
+ path + @";Integrated Security=True;User Instance=True"; 5/31/2009 11:11 AM | Jayel

# re: File path in C#

Gravatar string path = Environment.CurrentDirectory.ToString();
path = path.Substring(0, path.Length - 9);
path += "Diseases.mdf";
connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename="
+ path + @";Integrated Security=True;User Instance=True"; 6/18/2009 6:50 AM | umar

# re: File path in C#

Gravatar FileUpload1.PostedFile.FileName is not working when we read the file from the client machine.It throws an exeption stating that the file is not found.

Please help 7/18/2009 9:17 AM | radha krishnan

# re: File path in C#

Gravatar i have a text file named DBDetails.txt in my project folder to store the connection details....i need to check whether the file exists in the current path...also it should be working wherever iam installing...i mean i will be placing that text file with the folder in which the exe will working..wat code i have to use..?? 8/2/2009 3:14 AM | siby

# re: File path in C#

Gravatar too waste 8/6/2009 6:34 AM | muthukumar

# re: File path in C#

Gravatar It worked for me.

Thanks
9/3/2009 9:26 AM | Parminder

# re: File path in C#

Gravatar I want to get the path of a file from client computer. In localhost it is working fine but when I try it live on server it generates an error
"System.IO.DirectoryNotFoundException: Could not find a part of the path 'c:\test'."
9/3/2009 10:40 PM | Imtiaz

# re: File path in C#

Gravatar thanks for the anwsers it realy helps me. 9/22/2009 9:19 AM | Peter

Post a comment





 

 

 

Copyright © Vinayak