I'm building a webapp using ASP.NET.
On my physical hard drive:
The path for my text file is: D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Data\TextFiles\someFile.txt
The .cs file is located in: D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Account\someCSFile.cs
In my code, I have the followings:
string fileName= Server.MapPath("TextFile/someFile.txt");
The code throws an exception saying that Could not find a part of the path 'D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Account\TextFile\someCSFile.cs
How am I going to use Server.MapPath to make it "go up one level", then find the "Data" folder > "TextFiles" > finally the "someFile.txt" WITHOUT hardcoding the entire file path?
This should do the trick
string fileName= Server.MapPath(#"..\Data\TextFile\someFile.txt");
take a look at this:
StackOverflow Post about Server.MapPath
You can use .. to go up one level:
string fileName= Server.MapPath("../Data/TextFile/someFile.txt");
You can also start from the application root by starting the path with a slash:
string fileName= Server.MapPath("/Data/TextFile/someFile.txt");
Related
I am creating a unit test which works using the following exact path:
string path = #"/Users/{username}/Coding/computershare/ChallengeSampleDataSet1.txt";
I read the text from the file by passing this path
string pricesFromFile = System.IO.File.ReadAllText(path);
However, I do not want to hardcode the complete local file path - I want to use the relative path in the project directory.
Therefore I tried the below using other articles on StackOverflow:
string path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ChallengeSampleDataSet1.txt");
But file is not found. How can I fix this so that I'm able to load the file when running the app from another machine?
Edit: the error in console using the second method is
System.IO.FileNotFoundException : Could not find file '/Users/{username}/Coding/computershare/bin/Debug/net5.0/ChallengeSampleDataSet1.txt'.
This should do the trick:
string path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory + #"..\..\..\", "ChallengeSampleDataSet1.txt");
Note the direction of the slash (\)
Since the error is "/Users/{username}/Coding/computershare/bin/Debug/net5.0/ChallengeSampleDataSet1.txt" it indicates that your base directory is /bin/debug/net5.0 down from the root of the code. By double dotting up three levels, you'll be able to find the file in question.
In a block of code, i have the following line:
CsvFile= #"D:\Web\Preps\en\csr\downloadcenter\ClickCounter.csv";
I try to use Server.MapPath instead:
CsvFile = Server.MapPath(#"../en/csr/downloadcenter/ClickCounter.csv");
(the file from where i write this line is located at the same level as "csr" but in a different folder)
I don't have any errors appearing since i'm not using visual studio. Does anyone know what i'm doing wrong ? Thanks for your help
You said that your file is in a folder at the same level of the csr folder, something like
D:\Web\Preps\en\YourFolder, am I right ? Then, your path is not correct.
Try this :
CsvFile = Server.MapPath(#"../csr/downloadcenter/ClickCounter.csv");
or this :
CsvFile = Server.MapPath(#"../../en/csr/downloadcenter/ClickCounter.csv");
The reason your path is not correct is because the way you're using it, you're trying to access
D:\Web\Preps\en\en\csr\downloadcenter\ClickCounter.csv. There is an unneeded en
Assuming that the directory you are after is "under" your web site directory then:
Server.MapPath("~/")
Will take you to the root of your website, from there navigate to somewhere else i.e.
Server.MapPath("~/en/csr/downloadcenter/ClickCounter.csv")
If you don't want to get to the root of your site and it's located somewhere else on the server, then I have misunderstood the question. In this instance you will need to ensure the site has permissions to the respective directory.
I tried to get file path using below code.
string script = File.ReadAllText(Application.StartupPath + "D:\\Tax Rouding Projects\\10-12-12 TaxRoundingUtility\\TaxRoundingUtility\\Scripts\\GP_SOP_AdjustTax.sql");
But i am getting error : The given path's format is not supported
if i try to open the file from windows explorer.. i am able to go file location..
D:\Tax Rouding Projects\10-12-12 TaxRoundingUtility\TaxRoundingUtility\Scripts\
But why i cannot using c# code...
Any thing i missed in the path...
The problem lies here
Application.StartupPath + "D:\Tax Rouding Projects\10-12-12 TaxRoundingUtility\TaxRoundingUtility\Scripts\GP_SOP_AdjustTax.sql"
This might end up giving you something like
"c:\program files\myappfolder\D:\Tax Rouding Projects\10-12-12
TaxRoundingUtility\TaxRoundingUtility\Scripts\GP_SOP_AdjustTax.sql"
which is an invalid path. Append only portion of path that you need like (the second part is just an example)
Application.StartupPath + #"\TaxRoundingUtility\Scripts\GP_SOP_AdjustTax.sql".
Also make sure to escape the '\' in your file path strings.
Edit: As Dante has mentioned in the comment in the question, If your target path is fixed and known, you do not need the Application.StartppPath. Just load/read the file for which you have the complete path.
I've checked this thread How to get files in a relative path in C#, the directory is in IDE, which is not correct for me. I have a website application, needs to get image files from the same level folder img. I can use following code to get them:
DirectoryInfo path=new DirectoryInfo(#"c:\WebsiteSolution\wwwroot\Chat\img");
FileInfo[] images = path.GetFiles("*");
But I want to use something like .\img to replace the parameter in the first line code, is that possible?
Call the Server.MapPath utility to get the relative path.
DirectoryInfo path = Server.MapPath("~\Chat\img");
For ASP.Net use MapPath - http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx.
Also if you just need to construct path for rendering a page just /Chat/img/My.gif is enough.
You need to find an anchor - something like Application.ExecutablePath - and then use that anchor with Path.Combine() to reach your image directory.
If your Application.ExecutablePath were: #"c:\WebSiteSolution\wwwroot\chat\code", then you could use string imgPath = Path.Combine(Application.ExecutablePath, #"..\img");
If you have a hard time finding a good anchor, there's a bunch to consider. You can get the path of the executing assembly via Assembly.GetExecutingAssembly().Location
There is a text file that I have created in my project root folder. Now, I am trying to use Process.Start() method to externally launch that text file.
The problem I have got here is that the file path is incorrect and Process.Start() can't find this text file. My code is as follows:
Process.Start("Textfile.txt");
So how should I correctly reference to that text file? Can I use the relative path instead of the absolute path? Thanks.
Edit:
If I change above code to this, would it work?
string path = Assembly.GetExecutingAssembly().Location;
Process.Start(path + "/ReadMe.txt");
Windows needs to know where to find the file, so you need somehow specify that:
Either using absolute path:
Process.Start("C:\\1.txt");
Or set current directory:
Environment.CurrentDirectory = "C:\\";
Process.Start("1.txt");
Normally CurrentDirectory is set to the location of the executable.
[Edit]
If the file is in the same directory where executable is you can use the code like this:
var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var file = Path.Combine(directory, "1.txt");
Process.Start(file);
The way you are doing this is fine. This will find the text file that is in the same directory as your exe and it will open it with the default application (probably notepad.exe). Here are more examples of how to do this:
http://www.dotnetperls.com/process-start
However, if you want to put a path in, you have to use the full path. You can build the full path while only caring about the relative path using the method listed in this post:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e763ae8c-1284-43fe-9e55-4b36f8780f1c
It would look something like this:
string pathPrefix;
if(System.Diagnostics.Debugger.IsAttached())
{
pathPrefix = System.IO.Path.GetFullPath(Application.StartupPath + "\..\..\resources\");
}
else
{
pathPrefix = Application.StartupPath + "\resources\";
}
Process.Start(pathPrefix + "Textfile.txt");
This is for opening a file in a folder you add to your project called resources. If you want it in your project root, just drop off the resources folder in the above two strings and you will be good to go.
You'll need to know the current directory if you want to use a relative path.
System.Envrionment.CurrentDirectory
You could append that to your path with Path
System.IO.Path.Combine(System.Envrionment.CurrentDirectory, "Textfile.txt")
Try using Application.StartupPath path as default path may point to current directory.
This scenario has been explained on following links..
Environment.CurrentDirectory in C#.NET
http://start-coding.blogspot.com/2008/12/applicationstartuppath.html
On a windows box:
Start notepad with the file's location immediately following it. WIN
process.start("notepad C:\Full\Directory\To\File\FileName.txt");