How to remove a file from DICOMDIR using fo-dicom - c#

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.

Related

Roslyn CodeFix can't find previously created AdditionalDocument

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.

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.

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.

Problem with creating ResXResourceSet object from the embedded resource file

I am trying to open a resx file that is a resource in my C# project. I need to create a ResXResourceSet object. However at runtime an "Illegal characters in path" exception is thrown. This is the code I am trying to use.
var resX = new ResXResourceSet(Project.Properties.Resources.ResXFile);
The ResXResourceSet class has only two constructors (from stream and from file name). How can I create an object of the ResXResourceSet class in this situation?
Use Project.Properties.Resources.ResourceManager.GetStream("ResXFile");
If I understand correctly, the value in ResXFile is a string with the complete contents of the ResX, and not a file path, which is what ResXResourceSet expects when you pass it a string. You'll need to wrap a stream around it.
See this question for getting a stream from a string: how to generate a stream from a string?
Also, if you make the resource file into a project item, like the main resources, you can access its ResourceSet through its ResourceManager: ResXFile.ResourceManager.GetResourceSet()
You can add a ResX to your project by rightclicking on the project > Add > New Item > Resources File.

SharpZipLib ZipEntry how can you change the name of a file in the zip?

How do you change the name stored in the zip file?
Also, can this be done when adding the file via:
ZipFile zf = new ZipFile("path");
zf.BeginUpdate();
zf.Add(filePath); <-- i would like to also be able to change it when adding
zf.CommitUpdate();
Have you looked at the NameTransform property?
See SharpLibZip: Add file without path for an example of usage.
Update: Apparently, the latest version of SharpZipLib contains an overload for Add() that takes the filename.

Categories

Resources