I added an alternative code-path using an input string rather than reading from a file.
I would require an empty FileInfo object as I have many instances which access the Name, Length and Extension property.
Ideally I am looking for something like
FileInfo _fileinfo = new FileInfo(File.Empty);
However there is only one FileInfo constructor, which appears to require a valid file. Any solution to creating an empty-initialized FileInfo object, which does not require the creation of an empty dummy file?
I just came across a similar problem. What do you think about starting with:
FileInfo _fileinfo = null;
After that, you could just do:
_fileinfo = new FileInfo(<string of file with path>);
You would then have an object that you could pass as parameter to your methods. Don't foget to check if your object is null before you try to get the values for .Name and so on
if(null != _fileinfo)
{
//some code
}
As pointed out in the comments, FileInfo will happily construct with non-existant paths, but they have to be valid path strings (which you call a 'valid file'). (See https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=net-5.0 for what is considered a valid path.)
The solution would be to simply write
FileInfo _fileinfo = new FileInfo(#"C:\NonExistant.txt");
instead of
FileInfo _fileinfo = new FileInfo(File.Empty);
You can then think of your own test to see if you're dealing with the 'Empty' FileInfo object, such as checking drive letter, extension, File.Exists tests and such.
Related
I'm currently working with the XSD.exe tool to get classes of XSD files.
But when I pass a file to the tool, it changes the path/file.
string fileName = "C:\\TEST\\testFILE.xsd";
Process p = new Process();
p.StartInfo = new ProcessStartInfo("C:\\xsd.exe", "/c /language:CS " + fileName);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.Start();
StringBuilder error = new StringBuilder();
while (!p.HasExited)
error.Append(p.StandardError.ReadToEnd());
MessageBox.Show(error.ToString());
Thats some example code to show you the problem.
The output looks like:
Error: Could not find file "c:\test\testfile.xsd
Of course there is no such file or directory.
Do you guys have any idea how to solve this?
Thank ;)
I found the problem. The path in the above given example is a bad choice. In fact, the path I'm really using contains spaces. The XSD.exe uses spaces to seperate arguments. So you have to add some extra quotations at the beginning and at the end of the path string.
For example :
string cmdPath= String.Format(#"""{0}""", path);
I get the following error while trying to evaluate a predicate in a a4solution:
Fatal error in /some/path at line 9 column 2: Field "field
(A/Attribute <: type)" is not bound to a legal value during
translation.
Here is the code at the origin of the error:
for(ExprVar a : solution.getAllAtoms()){
// additional checks are here to assure that a is of an "appropriate type"
solution.eval(predicate.call(a));
}
In my vain attempts to solve this problem by myself, I read from this source http://code.google.com/p/alloy4eclipse/issues/detail?id=86 that the way the solution has been read from the file might cause this problem.
But the source doesn't give further details.
I have created my solution object as follows :
XMLNode xml = new XMLNode(new StringReader(source.getFileContent()));
this.solution = A4SolutionReader.read(new ArrayList<Sig>(), xml);
Thank you for your support
The problem was that the expression to be evaluated (predicate.call(a)) was drawn from one CompModule object (namely the predicate function was taken from there) while the solution object, against which the expression was evaluated, was not obtained from the same CompModule, but was read from a file.
Generally, when reading a solution from an xml file, to be on the safe side, it is recommended to reread and reconstruct everything from that xml file, e.g.,
XMLNode xmlNode = new XMLNode(new File("my_solution.xml"));
String alloySourceFilename = xmlNode.iterator().next().getAttribute("filename");
Module module = CompUtil.parseEverything_fromFile(rep, null, alloySourceFilename);
A4Solution ans = A4SolutionReader.read(module.getAllReachableSigs(), xmlNode);
In some cases it suffices to just pass the sigs from the original CompModule to the reconstructed solution:
XMLNode xmlNode = new XMLNode(new File("my_solution.xml"));
A4Solution ans = A4SolutionReader.read(originalModule.getAllReachableSigs(), xmlNode);
I have just started utilizing ArrayLists in some C# code and am having some problems when sorting.
First I define create an ArrayList object under my class:
ArrayList cutList = new ArrayList;
Then I set and sort the array list to find the minimum:
cutList.Add("2200","1800","1200","1");
int minList = (int)GetMinValue(cutList);
Using the function:
public static object GetMinValue(ArrayList arrList)
{
ArrayList sortArrayList = arrList;
sortArrayList.Sort();
return sortArrayList[0];
}
Later I try to find the index cutList[2] and I find "1200" because the function also sorted cutList. I have also had the same problem in the past, when I set a variable to an Application settings and then the Applications setting changes when I modify the variable. How to I correctly fix these problems. I have been learning C# on my own and am guilty of skipping around a little bit. Is there a lesson on Objects that I am missing?
The issue in your code is that ArrayList sortArrayList = arrList; does not copy arrList to sortArrayList: the assignment merely creates a new alias for the existing object. To make your code work, use
ArrayList sortArrayList = (ArrayList)arrList.Clone();
I must add that this is probably the most inefficient way of looking up the min element in a list, and also a rather archaic container. I would prefer using List<string> instead of ArrayList, and using LINQ's Min() function to get the minimum element.
I need to add new version of the file to SPFileCollection without changing version (it is a system update of the content of pdf file)
UpdateOverwriteVersion allows me to change metadata but is there a way to change content (bytes) of the file?
WORKAROUND:
Ok, as Stefan found out, there is no satisfactory solution to this.
I found two workarounds:
1.
(Be aware that turning off the version control could lead to wrong behavior, if there are people working on that list... – Stefan)
oFile = oWeb.GetFile(url);
oFile.Item.ListItems.List.EnableVersioning = false;
oFile.Item.ListItems.List.Update();
oFolder.Files.Add(oFile.Name, aBytes, fileProperties, true);
oFile.Item.ListItems.List.EnableVersioning = true;
oFile.Item.ListItems.List.Update();
2.
oFile = oWeb.GetFile(sAdres);
int iFileVersion = oFile.UIVersion;
oFolder.Files.Add(oFile.Name, aBytes, fileProperties, true);
oFileVersion = oFile.Versions.GetVersionFromID(iFileVersion);
if (null != oFileVersion && !oFileVersion.IsCurrentVersion)
{
oFileVersion.Delete();
}
Why don't you use SystemUpdate(false)
http://msdn.microsoft.com/en-us/library/ms481195.aspx
You can load the SPFile object and then call SPFile.SaveBinary(Stream, SPFileSaveBinaryParameters) method to update it's content.
With the CreateVersion property of the SFileSaveBinaryParameters you can specify whether a new version should be created or not.
[...] this is by design. As far as I know , there is no other solutions on it.
A quote from a moderator at Microsoft TechNet forum.
I need to parse an XLIFF file using C#, but I'm having some trouble. These files are fairly complex, containing a huge amount of nodes.
Basically, all I need to do is read the source node from each trans-unit node, do some processing on it, and insert the processed text into the corresponding target node (which will always be present, but empty).
An example of one of the nodes I need to parse would be (the whole file may contain 100s of these):
<trans-unit id="0000000002" datatype="text" restype="string">
<source>Windows Update is not installed</source>
<target/>
<iws:segment-metadata tm_score="0.00" ws_word_count="6" max_segment_length="0">
<iws:status target_content="placeholders_only"/>
</iws:segment-metadata>
<iws:boundary-seg sequence="bs20721"/>
<iws:markup-seg sequence="0000000001">
</trans-unit>
The trans-unit nodes can be buried deep in the files, the header section contains a lot of data. I'd like to use LINQ to XML to read the data, but I'm not having any luck getting it to work. Here's my current code (just trying to read and output the source nodes from the file:
XDocument doc = XDocument.Load(path);
Console.WriteLine("Before loop");
foreach (var transUnitNode in doc.Descendants("trans-unit"))
{
Console.WriteLine("In loop");
XElement sourceNode = transUnitNode.Element("source");
XElement targetNode = transUnitNode.Element("target");
Console.WriteLine("Source: " + sourceNode.Value);
}
I never see 'In loop' and I don't know why, can someone tell me what I'm doing wrong here, or suggest a better way to achieve what I'm trying to do here?
Thanks.
Try
XNamespace df = doc.Root.Name.Namespace;
foreach (XElement transUnitNode in doc.Descendants(df + "trans-unit"))
{
XElement sourceNode = transUnitNode.Element(df + "source");
// and so one, use the df namespace object to qualify any elements names
}
See also http://msdn.microsoft.com/en-us/library/bb387093.aspx.