Roslyn CodeFix can't find previously created AdditionalDocument - c#

I am writing a C# Roslyn Analyzer & CodeFix which will create a new .txt file, based on some variables, if it doesn't exist and it will append a new line if it does.
The Analyzer and the creation of the file work perfectly using the following code in RegisterCodeFix Action:
var proj = document.Project.AddAdditionalDocument("file.txt",
"text content");
return proj.Solution;
Although, when I am trying to search the project's collection AdditionalDocuments it is empty, even if the file is created previously (and the project is saved, if that matters).
var doc = document.Project.AdditionalDocuments.FirstOrDefault(x =>
x.Name == "file.txt"); //doc is null
I tried adding the new file as a plain Document instead of an AdditionalDocument but the file.txt is created as a source code file file.cs and not as a .txt one, but, for a reason, I can find it as file.cs in the Project.Documents collection.
Any thoughts of how can I create a non-source code file in a CodeFixProvider and use it?

You need to apply the changes to your workspace after you've added the document:
workspace.TryApplyChanges(proj.Solution);
However, according to this link, it seems that a current bug means AddAdditionalDocument() doesn't persist the document beyond the workspace in memory, meaning the project will not have the new document when reloaded.
Also, AddDocument() will only add documents with an extension corresponding to the language of the containing project, for instance, calling AddDocument() on on a C# Class Library project, will rename the extension to '.cs' if not already so.

Related

How to remove a file from DICOMDIR using fo-dicom

I have created the the DICOMDIR using fo-dicom as follows.
var dicomDir = new DicomDirectory();
var dicomFile = Dicom.DicomFile.Open(dicomFilePath);
dicomDir.AddFile(dicomFile, filePathForDicomDir);
DicomWriteOptions options = DicomWriteOptions.Default;
options.ExplicitLengthSequenceItems = false;
dicomDir.Save(dicomDirPath, options);
I have to remove the already added file from the existing DICOMDIR, but couldn't find a method in the DicomDirectory.cs class.
How do I successfully remove a file reference from a DICOMDIR using fo-dicom, rather than recreating the entire DICOMDIR again?
Have you tried the Delete method?
dicomDir.File.Delete();
Your request is rather unusual. fo-dicom does not provide a method in DicomDirectory to remove an entry from an existing DICOMDIR.
The filesets in DICOM are meant to be immutable. Such file-sets are a collection of files that are for example burnt on a CD and then are indexed with the DICOMDIR file. So when should you create a DICOMDIR? When you have a set of files to burn on CD and for those you create the DICOMDIR file.
Also internally the DicomDirectoryRecords are list of entries with offsets that reference each other. So removing an entry means internally to completely rebuild the records-structure. So there is not a big advantage of removing an entry over recreating the DICOMDIR.
What is the use case where you need to remove an entry and recreating the DICOMDIR is not possible? If there is a valid use case, then you have to create an issue in fo-dicom project on github.

Xamarin.Forms Can not Read Text file from Resources Returned Null

I am using Xamarin.Forms, and I am trying to read text file from Resources, Code Below Returned Null value when trying to fetch text file.
var assembly = typeof(BookPage).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("AboutResources.txt");
//assembly here return null value, I can not find Text Resources
My Code under the following Class Inherited From ContentPage On PLC Project
public class BookPage : ContentPage
//First get the list of all resources available for debugging purpose
assembly.GetManifestResourceNames()
This will list all the (fully qualified names) of all resources embedded in the assembly your code is written in.
Link: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcenames(v=vs.110).aspx
Check if it has "AboutResources.txt" that you are expecting.
Check if you have incorrectly embedded the resource file, it will not show up in the list returned by the call to GetManifestResourceNames(). Make sure you you match the case of the name.
The following steps working fine for me:
1-First of all I added resource text file to same project
2-The file path must be like "Your Project name.your custom folder.filename".
var fileName = "MySampleProject.XMLData.rawxmldata.xml".
3-Make sure that Build Action is EmbeddedResource.

Location of file is unchanged after changing List of folders via Roslyn

I'm trying to change file location of existing file via Roslyn.
var msWorkspace = MSBuildWorkspace.Create();
var solution = msWorkspace.OpenSolutionAsync(Constants.pathToSolution).Result;
DocumentId documentIdToMove = ConsoleHelpers.GetDocumentIdForDocumentWithName(solution, "Person.cs");
var newSolution = solution.WithDocumentFolders(documentIdToMove, new List<string> { "SecondLevel", "ThirdLevel" });
msWorkspace.TryApplyChanges(newSolution);
Originally, the file is in the "SecondLevel" folder inside main project folder.
According to documentation, WithDocumentFolders method should create a new solution instance with the document specified updated to be contained in
the sequence of logical folders.
After running the code, program is completed without any exceptions, file is changed on disk, but the location remains the same. Also, TryApplyChanges method returns true.
When creating new document in a project and then saving changes to disk, the new file is created in location specified by the sequence of folders without any issues.
Is changing location of existing file in project and then saving the changes to disk possible?
Changing folders like this isn't supported. Feel free to file a bug on GitHub.

Loading FlowDocument.xaml that is part of my solution

I created a FlowDocument.xaml in my current WPF project. What i want to do is when the user clicks a button the XAML document will be loaded in the code behind, have some data on the document modified, and then print it out. The sticking point is i don't know how load the flow document so that i can modify it.
When I do:
FileStream fs = File.Open("FlowDocument.xaml", FileMode.Open)
It says that it can't find the file. The file is part of the project and I'm guessing it gets packaged with the rest of the project when compiled.
Any help is appreciated
Assuming it is configured to be a Resource, then you can load it like so:
FlowDocument doc= Application.LoadComponent(new Uri("/Path/FlowDocument.xaml", UriKind.RelativeOrAbsolute)) as FlowDocument;
This looks like it might be a path/relative path issue...just for testing purposes, try specifying the entire physical/absolute path in the File.Open statement...
You could also do
string path = Directory.GetCurrentDirectory();
to check to see what the current directory is and then make sure that the file FlowDocument.xaml is in that directory

Accessing Resources at Runtime

I have an XML file included as part of my Silverlight 4.0 project that I'd like to access at runtime. I have the file saved in a directory named Resources with the Build Action set to "Content" and the Copy to Output Directory set to "Do not copy". If I decompress the XAP file, I see the XML file in the location I expect it to be, but I'm not sure how to reference it from code. I currently have the following:
Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(#"/AssemblyName;component/Resources/MyFile.xml")
Unfortunately, stream is null after running the code above. In addition to the path mentioned above, I've tried "/Resources/MyFile.xml", "/MyFile.xml" and "MyFile.xml", but they all experience the same behavior.
What is the correct way to access an XML file embedded as a resource in a Silverlight application?
A resource with build action "Content" just gets embedded into the xap file, with the same relative directory structure as the application. It does not get embedded as a resource in the assembly.
When set to build action "Content", you should be able to just load the file using something like (or whatever suits your needs):
XElement.Load(<relative directory>/<file>)
The method you're using currently (using a resource stream) is for embedded resources (which have their build action set to "Resource"). And for those, although I haven't tried yet if your method works, usually you'll get the resources using
Application.GetResourceStream
I have used the code snip below to get access to drawables. Not sure it's completely relevant, but hoping this will give you a hint one way or another ...
Resources res = getResources();
spec = tabHost.newTabSpec("groups").setIndicator("Groups", res.getDrawable(R.drawable.ic_tab_groups)).setContent(intent);
As was mentioned by Willem van Rumpt, "content" resources are not usual resources (they aren't stored in assembly). I've checked out this article and could't found at all that you could reference resource, marked as "content" from other assembly.
So, you have two options:
Define XML as embedded resource
Define XML as resource
In first case stream request looks like:
var a = Assembly.Load("AssemblyName");
var s = a.GetManifestResourceStream(#"DefaultNamespace.Resources.XMLFile2.xml");
In second case:
var a = Assembly.Load("AssemblyName");
var rm = new ResourceManager("AssemblyName.g", a);
using (var set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true))
{
var ums = (UnmanagedMemoryStream)set.GetObject(#"Resources/XMLFile1.xml", true);
}
Hope this helps.

Categories

Resources