I have a folder with a list of files that I've included in my silverlight solution as "Content". I know how to load individual files using Application.GetResourceStream(), but I wish to load an entire directory with multiple files in it.
Attempted to use DirectoryInfo but it throws a Securityexception, and using Application.GetResourceStream() with a directory path returns null.
I solved by using a standard file naming convention, with a specified index in the filename, and then I just build each filename with using a loop and the index of the loop and load each stream into an array.
Related
I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.
I'm trying to find a file in a settings folder in my application. I have a xml file there. When I run the following code:
XDocument xDoc = XDocument.Load(#"..\settings\Settings.xml");
I get the DirectoryNotFoundException and the exception says not found at \bin\settings\Settings.xml'., instead of above. I even tried the full root directory to see the issue, C://... but it still includes a bin folder?
How can I have it so it doesn't include the bin part?
By default, the build results in Visual Studio are saved in a folder like bin\Debug. Since you use a relative path that jumps one folder higher, you get yourProjectFolder\bin\settings\Settings.xml. That file doesn't exist, since it's presumably in the project folder, not the bin folder.
The typical way to deal with this is to make sure the files that are supposed to be a part of the content actually have Build Action set to Content.
Using a rooted path definitely works - most likely, you made a mistake somewhere; either the path isn't rooted at all, or you're doing something like interpreting the path as an URI rather than a file path. XDocument.Load takes a URI, not a file path - the proper way to reference an absolute path on the filesystem would be file://C:/ThePath/Settings/Settings.xml.
I'm running into a strange problem using ZipFile and ZipArchive with .Net 4.5.
ZipFile.CreateFromDirectory takes all content of a directory, including folders that are empty.
If I try to create the same zip file using Windows explorer by right clicking > Send to > Compressed folder, I get a warning message saying the empty folder was omitted.
I'm loading the resulting zip file into an application that runs on Apache Tomcat. This application throws errors for every single file contained in the zip that I produced with ZipFile.CreateFromDirectory. The zip that I created manually through Windows explorer is read just fine.
I suspect the problem lies in the empty zipped folders, but haven't yet been able to definitively conclude this. If the empty folders are the cause, I'd need a way to use ZipFile.CreateFromDirectory excluding empty folders.
Taken from my comment above:
I have no .NET 4.5, but from the remarks section: "The directory structure from the file system is preserved in the archive. If the directory is empty, an empty archive is created." So this is by design.
So you either have to
fix it in the comsuming app on tomcat or you have to
create a temporary folder which just contains the non-empty folders, if possible
I haven't found a way to exclude empty folders in CreateFromDirectory in the first place.
Alternatively, I can remove empty directories from the created zip file. Though this still causes errors in the Tomcat application.
// compress and copy new zip
ZipFile.CreateFromDirectory(dirtocopy.FullName, NewZipFilePath);
using (ZipArchive za = ZipFile.Open(NewZipFilePath, ZipArchiveMode.Update))
{
// only empty folders end with \
List<ZipArchiveEntry> emptyFolders = (from ZipArchiveEntry zae in za.Entries
where zae.FullName.EndsWith("\\")
select zae).ToList<ZipArchiveEntry>();
emptyFolders.ForEach((ZipArchiveEntry folder) => folder.Delete());
}
Inside my solution, I have a folder called "Server". This folder contains a project called "InvestmentAdvisory", which contains the folders Report/Languages/. Inside Languages are a class "Translation", and a CSV file. The class is supposed to load the CSV file.
The namespace of the class "Translation" is like:
"Server.Modules.InvestmentAdvisory.Report.Languages"
After the application is built, the CSV file ends up in:
MyApplication\Main\Source\InvestmentAdvisory.Services.Implementation\bin\Debug\Report\Languages\
How do I find the relative path to the CSV from the inside of my Translation class?
Sorry if i'm miss understanding but if you need the directory of your application you can get the path by using
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Assuming that your assembly/exe outputs will be in
MyApplication\Main\Source\InvestmentAdvisory.Services.Implementation\bin\Debug
You can use the relative path:
.\Report\Languages\CsvFileName.csv
The easiest approach would be to keep the csv file in the output folder.
Add the csv to project
Select the csv file and in properties window
(Press F4 to open the window if not present), set Copy To Output
Directory (Always/If Newer).
Then you would not need a path as long as the assembly/exe are all in the same folder i.e. output folder.
I have an application that upon execution It copies two folders with subfolders that are in the same location to another windows location %AppData%
Now I have the following files :
MyApp.exe , Folder1, Folder2
In each folder there are subfolders. How to embed these two folders as resources inside the application so after compiling the program, I get only one executable file. And when I click on it, it extract the two folders to the same location then do the rest of job.
I know how to add a file as embedded resource then retrieve it using reflection,
but how about a folder Is that even possible??
I had to solve this problem recently. I embedded a ZIP file, and then decompressed it at runtime.
.NET 4.5 includes ZIP functionality. If not, use SharpZipLib or DotNetZip.