I'm writing a Visual Studio editor plugin. This plugin's functionality includes modifying the project's folder and file structure. I also need access to physical folder where the project is being held. Currently I'm getting the project folder in the following way:
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)serviceProvider.GetService(typeof(EnvDTE.DTE));
var projectItem = dte.Solution.FindProjectItem(pszMkDocument);
var project = projectItem.ContainingProject;
Then I may query System.IO.Path.GetDirectoryName(project.FileNames[0]) and get required path.
But how can I do so in case of website project in Visual Studio? Websites does not have project file as such, and the previous method returns a http:// path instead of local path.
The EnvDTE.Project.Properties collection contains a property named "FullPath" whose Value returns the physical folder for web site projects.
you can use server.mappath
HttpContext.Current.Server.MapPath("~/App_Data/Example.xml");
Related
So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.
I am new in mvc and c# and I can't solve following problem:
I am trying to create a folder named "Items" in solution folder.
I have tryed to use CreateDirectory method:
Directory.CreateDirectory("~/Images");
But it didn't work for me - folder wasn't created ..
Partly working solution was to create a folder by :
Directory.CreateDirectory(Server.MapPath("~/Images"));
"Items" folder was created, but it is not included in the solution:
How to create folder in solution directory so that it is included in project ?
(I needs to by done by code not by hand)
You need to understand what solution and csproj file is used for
In general, they're being designed and used for development with Visual Studio, and once the project is compiled, all these files will be ignored and excluded from the deployment package
Directory.CreateDirectory(Server.MapPath("~/Images"));
The code above simply create the directory if not existed yet in the deployment package at run-time, so you won't see it in your solution unless you run the project locally (either debug/release mode, it does not matter here). However, everything will run normally in hosted environment (ex: IIS).
For your information, here's the brief of what solution and csproj is
solution (.sln) file: contains information to manage one or many individual projects, contains build environments (for each project), start up mode (useful when you want to start multiple projects in one run), project dependencies and so on. Take a note that VS also read from suo file (solution user options) which is used to defined user-custom preferences (you should not include the .suo file in the version control, because it's custom settings)
csproj file: define the structures of project, what the namespace is, what is static folders, embedded resources, references, packages, etc.
Lastly, if you create the folder manually, VS will auto include that folder into deployment package AND csproj, but depends on the file type, you might need to change the Build Action and Copy To Output Directory in file properties.
Hope it helps.
A deployed web application on a web server doesn't have any notion of Visual Studio solution or projects. So the Directory.CreateDirectory(Server.MapPath("~/Images")) is the correct way to create a folder inside your web application at runtime but we cannot be talking about including it into a solution because this hardly makes sense in a pre-compiled web application. If you create the directory on your local development machine, you could always manually include the folder to the corresponding .csproj file, but at runtime this will not make any difference whatsoever.
The reason I wanted to create a folder (if didn't exist) was to make sure it exits before I try to store image in it.
After reading posts here and a few google searches I have concluded that the proper way to handle image upload would be
To create (In my case) folder "Images" by hand to be sure it exists
Then storing uploaded img in existing folder:
string path =Server.MapPath("~/Images/"+ UploadedImageName);
file.SaveAs(path);
I am using visual studio 2008 and I have a few files that I want to reference when in #debug mode. How do I get the directory of the workspace so that I do not have to manually change it every time I am at a different computer?
For instance, when I'm at work, my workspace is in c:\work2, when I'm at home, it is in d:\work.
This isn't an issue on the live product but it is when I change computers or other uses load the project since the files are in different directories for each workspace.
thanks!
Eroc
Add the test files (XML, spreadsheet, etc) as a 'Resource.' How to Add a Resource to your Project
Then, in your test files, you can use a relative address to access those files.
private const String = "./testFile1.xml";
You are able to have some pre-build command to change directory.
I have a folder named Template in my solution. I want some files to be copied in to it and accessed from it. How can i set the path to that? Will this folder be there when i deploy the application?
does this work?
File.Move(#"DebriefReportTemplate.docx", #"~\Template\DebriefReportTemplate.docx");
It won't be created unless you either build a setup/deployment project to create it at install time, or add code in your app to create it upon first invocation.
If you are worried about the existence of the Template folder, you could just create it at some point in your code.
string path = System.IO.Path.Combine("", "Template");
System.IO.Directory.CreateDirectory(path);
and then move the file
File.Move(#"DebriefReportTemplate.docx", #"Template\DebriefReportTemplate.docx");
EDIT: This answer is for an ASP.NET application.
If Template folder (including its content) is part of the web project, the deployment should work automatically. If you want to add files to this folder at runtime, you can use
Server.MapPath(#"~\Template\DebriefReportTemplate.docx")
, but be careful, the web application usually runs under an identity which has limited access to the local resources.
The same thing applies if you have a Win app. What you need to do is to add the folder and the files to the project, as Content. You will need a setup project though.
You may use
string sourceFile = Path.GetDirectoryName(Application.ExecutablePath)+#"\Template\DebriefReportTemplate.docx";
string destinationFile = #"C:\DebriefReportTemplate.docx";
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
References :
MSDN: Copy Files & Folders
C# Examples: Get Application Directory [C#]
I have a class library project which contains some content files configured with the "Copy if newer" copy build action. This results in the files being copied to a folder under ...\bin\ for every project in the solution. In this same solution, I've got a ASP.NET web project (which is MVC, by the way). In the library I have a static constructor load the files into data structures accessible by the web project. Previously I've been including the content as an embedded resource. I now need to be able to replace them without recompiling. I want to access the data in three different contexts:
Unit testing the library assembly
Debugging the web application
Hosting the site in IIS
For unit testing, Environment.CurrentDirectory points to a path containing the copied content. When debugging however, it points to C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE. I've also looked at Assembly.GetExecutingAssembly().Location which points to C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\c44f9da4\9238ccc\assembly\dl3\eb4c23b4\9bd39460_f7d4ca01\. What I need is to the physical location of the webroot \bin folder, but since I'm in a static constructor in the library project, I don't have access to a Request.PhysicalApplicationPath.
Is there some other environment variable or structure where I can always find my "Copy if newer" files?
Assembly.Location does indeed point to the location of the assembly after it has been shadow copied.
However, that MSDN page states:
To get the location before the file has been shadow-copied, use the CodeBase property.