I am trying to download a file stored in a folder.
string path = "D:\\app_data\\Clients\\Client " + jobdescription.ClientID + "\\Job " + jobdescription.JobDescriptionID + "\\";
string file = Path.Combine(path, jobdescription.JobTitle + ".docx");
return File(path, "application/docx", jobdescription.JobTitle + ".docx");
The error generated is:
Server Error in '/' Application.
Could not find a part of the path 'D:\app_data\Clients\Client 1\Job 2\'
But the file with specified filename is in the folder. What am i doing wrong ?
You use path instead of using file, in your third line.
It should read:
return File(file, "application/docx", jobdescription.JobTitle + ".docx");
Related
I have some json files that I need to read from my asp.net core app. They are under a folder called data
--MyProject
---Startup.cs
---Data
------dataset1.json
------dataset2.json
I am using IHostingEnvironment ContentRootPath to read the files:
string pathToFile = hostingEnvironment.ContentRootPath
+ Path.DirectorySeparatorChar
+ "Data"
+ Path.DirectorySeparatorChar
+ "dataset1.json"
which returns C:\SourceControl\Test.Backend\src\Test.Web\Data\dataset1.json
This works fine when I publish my code in IIS. However when I am debugging the files are copied into bin folder and the above code does not work.
How can I read the files while debugging?
#if DEBUG
string pathToFile = hostingEnvironment.ContentRootPath
+ Path.DirectorySeparatorChar
+ "bin"
+ Path.DirectorySeparatorChar
+ "Data"
+ Path.DirectorySeparatorChar
+ "dataset1.json"
#else
string pathToFile = hostingEnvironment.ContentRootPath
+ Path.DirectorySeparatorChar
+ "Data"
+ Path.DirectorySeparatorChar
+ "dataset1.json"
#endif
you can have two different paths when you are in debug mode and in production with this approach. Just change the first path to your needs
I am trying to create folders in nested manner.
if (file.ContentLength > 0 && file != null)
{
string path = "~/Videos/" + Session["username"] + "_" + Session["userid"];
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filename = path + file.FileName;
filepath = "/Videos/" + Session["username"] + "_" + Session["userid"];
file.SaveAs(filename);
If you see here- /Videos/ folder is what I have currently on disk. Where another folder with user's name and id is what I want to create inside this Videos folder. How Would I be creating this folder inside this folder?
Because currently it is showing me this error -
Access to the path '~/Videos/shaun_2' is denied.
I tried restarting visual studio with administrator's credentials. But it still remains here.
I'm assuming that you are using ASP.NET: try to use Server.MapPath("~/...") to get the physical path
See MSDN
Can anyone help with this? I keep getting an error File beeing used by another process.
Here's my code so far:
string username = textBox1.Text;
string password = textBox2.Text;
string SecurityCode = textBox3.Text;
if(!Directory.Exists(path + "\\Login"))
Directory.CreateDirectory(path + "\\Login");
if (!File.Exists(path + "\\Login\\Username.omg" + "\\Login\\Password.omg" + "\\Login\\SecurityCode.omg"))
File.Create(path + "\\Login\\Username.omg");
File.Create(path + "\\Login\\Password.omg");
File.Create(path + "\\Login\\SecurityCode.omg");
File.AppendAllText(path + "\\Login\\Username.omg", username);
File.AppendAllText(path + "\\Login\\Password.omg", password);
File.AppendAllText(path + "\\Login\\SecurityCode.omg", SecurityCode);
MessageBox.Show("User Created: Welcome: " + username);
}
File.Create returns a Stream that you need to close before trying to access the file just created
using(File.Create(path + "\\Login\\Username.omg"))
;
using(File.Create(path + "\\Login\\Password.omg"))
;
using(File.Create(path + "\\Login\\SecurityCode.omg"))
;
a simple using statement around the File.Create call could help to close and dispose the returned stream
However, as stated in the comments above from Mr Hans Passant, your simple scenario could be served better by a simple call to File.WriteAllText()
File.WriteAllText(Path.Combine(path, #"login\username.omg"), username);
File.WriteAllText creates the file if it doesn't exist and overwrite its content if it exists. In your code, instead, you append the info to the same files over and over.
I am not sure that this is really what you want here.
By the way, your call to File.Exists is wrong. You should test each file separately
string userNameFile = Path.Combine(path, #"login\username.omg");
if (!File.Exists(userNameFile)
{
using(File.Create(userNameFile))
;
}
I am using Path.GetTempPath function to get temp file path to store xml file at temporary location. At first this works successfully but for next run this gives a exception as "Illegal characters in file path".
string filepath = System.IO.Path.GetTempPath();
if (Interface.IsDebugMode)
{
xmlRepository.SaveDataToFile(filepath + #"\\savedFile.xml", true);
}
This should not work at all
xmlRepository.SaveDataToFile(filepath + #"\\savedFile.xml", true);
it needs to be This
xmlRepository.SaveDataToFile(filepath + "\\savedFile.xml", true);
or this
xmlRepository.SaveDataToFile(filepath + #"\savedFile.xml", true);
but not both And as the comment below says you really should be using this
xmlRepository.SaveDataToFile(Path.Combine(filepath, "savedFile.xml"), true);
The odd thing is that something like System.IO.File.Delete() works
and the file gets deleted but will give "access to path is denied error" for .Move() operation.
All files are located in the same folder, user "Network service" has all
full control rights for the folder and all subfolders in it etc.
Folders are located in the project directory and can be seen in solution explorer.
Exception Details: System.UnauthorizedAccessException: Access to the path is denied.
foreach(var info in FileActions.Where(x => x.OldSortOrder != x.SortOrder))
{
string FileToRename;
string NewName;
string OldFilePath;
string OldFileThumbPath;
FileToRename = info.ProductID + "/" + info.OldSortOrder + "-" + info.ImageID + ".jpg";
NewName = info.SortOrder + "-" + info.ImageID + ".jpg";
OldFilePath = System.Web.HttpContext.Current.Request.MapPath("~/Content/ProductImages/" + FileToRename);
OldFileThumbPath = System.Web.HttpContext.Current.Request.MapPath("~/Content/ProductImages/" + info.ProductID + "/thumbs/" + FileToRename);
System.IO.File.Move(OldFilePath, NewName);
System.IO.File.Move(OldFileThumbPath, NewName);
}
Its because you map the path for the first files but not for the NewName.
So did not have the full path to know what to rename/move the file, and needs the full path to work correctly.
With out the path this is probably try to move it on the default folder of the asp.net pool that is probably don't have this permissions.
So the code will be
NewName = System.Web.HttpContext.Current.Request.MapPath("~/Content/ProductImages/"
+ info.SortOrder + "-" + info.ImageID + ".jpg" );
and debug this lines to see if the directories and files are all correct.