I have this piece of code written in C# (it's part of implementation of ID3 algorithm)
public static void printNode(TreeNode root, string tabs, ref StreamWriter xa)
{
//Console.ReadKey();
Console.WriteLine(tabs + '|' + root.attribute + '|');
if (root.attribute.values != null)
{
for (int i = 0; i < root.attribute.values.Length; i++)
{
Console.WriteLine(tabs + "\t" + "<" + root.attribute.values[i] + ">");
xa.WriteLine(tabs + "\n" + root.attribute.values[i] +"\n");
TreeNode childNode = root.getChildByBranchName(root.attribute.values[i]);
printNode(childNode, "\t" + tabs, ref xa);
}
}
}
I also have this declaration.
StreamWriter xa = new StreamWriter("tree1.txt");
And issue is that after running code file 'tree1.txt' is always empty. Any idea how to handle this? As you can see I tried references cause it seems to me like right course of action but I don't really know much about C# and recurrence function behaviour.
You need to close the StreamWriter to force it to flush to disk.
You also need to look in the current directory, which might not be what you think it is.
Related
I am copying files from one location to another. I encountered an issue where sometimes a file with the same name would try to save and it would break the program. So I added the logic below to add a number to the filename before copying it:
int counter = 0;
try
{
File.Copy(FileToCopy, FileToSave);
}
catch (Exception)
{
string CurrentFileName = FileToSave.Split('\\', '\\')[4];
string CurretFilePrefix = CurrentFileName.Split('.')[0];
string CurrentFileSuffix = CurrentFileName.Split('.')[1];
string UpdatedFileName = CurretFilePrefix + "_" + counter + "." + CurrentFileSuffix;
File.Copy(FileToCopy, UpdatedFileName);
counter++;
}
However, this is now causing a crash saying the file already exists:
When I check the file does not exist:
Why am I getting this exception? How do I save copies of these files?
A good approach to this problem would include (i) a loop; (ii) use of Path expressions; (iii) avoidance of try-catch when you can test if the file exists; (iv) use of a specific Exception for the extremely unlikely case that two threads or processes are trying to do this same copy at the same time and each get past the File.Exists check; (v) avoidance of while-true-forever loops as even the best code can contain mistakes that could cause a spin-wait forever in production code on a server and it's better to instead have an exception that tells you when something has gone wrong.
int counter = 0;
string proposedDest = dest;
while(counter < 5000)
{
if (!File.Exists(proposedDest))
{
try
{
File.Copy(fileToCopy, proposedDest);
break;
}
catch (IOException ex) when ((uint)ex.HResult == 0x80070050)
{
}
}
counter++;
proposedDest = Path.Combine(Path.GetDirectoryName(dest),
Path.GetFileNameWithoutExtension(dest) +
"_" + counter + Path.GetExtension(dest));
}
;
if (counter == 5000)
throw new Exception($"Could not copy file {fileToCopy} too many retries");
[A better approach would also not use hard coded constants littered through the code ;)]
As CurrentFileName is relative, you are trying to save the file in the location of your executable. Check that directory if your file exists.
Also, there are much better ways to find if the file exists, and also to get the filename and extension of a given file.
i.e. you should use the File.Exists method to determine if the file exists, instead of using exceptions for flow control
if (File.Exists(FileToSave))
{
FileToSave = GetNewFileName(FileToSave)
}
try
{
File.Copy(FileToCopy, FileToSave);
}
catch (Exception)
{
//something went really wrong
}
You should also use the Path methods for getting parts of the name, instead of "knowing" to get the fifth part of the filename (Path.GetExtension, Path.GetFileNameWithoutExtension)
private string GetNewFileName(string oldFileName){
var counter = 0;
var extension = Path.GetExtension(oldFileName);
var directory = Path.GetDirectoryName(oldFileName);
var fileName = Path.GetFileNameWithoutExtension(oldFileName);
var newFileName = Path.Combine(directory,
string.Format("{1}_{2}{3}", filename, counter, extension);
while (File.Exists(newFileName)){
counter++;
newFileName = Path.Combine(directory,
string.Format("{1}_{2}{3}", filename, counter, extension);
}
return newFileName;
}
use this instead:
string CurrentFileName = System.IO.Path.GetFileName(FileToSave);
string CurretFilePrefix = System.IO.Path.GetFileNameWithoutExtension(FileToSave);
string CurrentFileSuffix = System.IO.Path.GetExtension(FileToSave);
string UpdatedFileName = CurretFilePrefix + "_" + counter + "." + CurrentFileSuffix;
Then you have
File.Copy(FileToCopy, UpdatedFileName);
Are you sure this shouldn't be
File.Copy(FileToSave, UpdatedFileName); // FileToSave instead of FileToCopy
I am trying to make to create a .cmd file with this code into it: call .\CopyToTarget.cmd w60 glb "C:\Users\oma\me\trunk-r664\USB-map". I am creating this code ~5 times.
But since \trunk-r664\ is already in use it seems like I cannot write: #"\trunk-r664\USB-map" into the .cmd file for some reason. Does anyone know how to fix it? It keeps getting me the error: UnauthorizedAccesExpection was unhandled, ccess to the path 'C:\Users\me\Desktop\trunk-r664\USB-map' is denied.
using (StreamWriter sw = File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ "\\trunk-r664\\trunk\\cmd\\custom\\RunAll.cmd"))
{
for (int j=0;j<installeerlijst64.Count;j++)
{
sw.WriteLine("call .\\CopyToTarget.cmd " + installeerlijst64[j] + " glb" +
File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\trunk-r664\USB-map"));
}
}
I tried this too, but it tells me I am using an illegal character:
"\""+File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ #"\trunk-r664\USB-map" + "\""));
File.CreateText will create a new file. First time when for loop execute, it will create and open the file USB-map and hold the handle of that file. During second iteration of for loop, it will try to do the same thing. Hence, already in use error.
Remove File.CreateText and you will get the desired result.
sw.WriteLine("call .\\CopyToTarget.cmd " + installeerlijst64[j] + " glb " + "\"" +
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\trunk-r664\USB-map" + "\"");
I have a program that runs a series of plugins using threads. During this process, it writes the runtimes of the plugins to an XDocument so that the program knows how long it's been since the last time that plugin was run. I'm having a problem, though. About once per day (at unpredictable times) when I load the XDocument I get the following error:
[04/01/2013 08:17:10.083] Unexpected end of file has occurred. The following elements are not closed: Database, DatabaseList. Line 4043, position 1.
I ran some trace statements and found out that the time before that, the service failed with about 44 plugins left to run, which apparently caused the XDocument to close without writing the end of the file. It writes to and reads from an XML file on the hard disk, but it performs all operations on the XDocument in memory because I use Linq to do complex operations on the data.
Does anybody know why this might happen? How can I load my XDocument so that it won't wreck the actual file if something happens during the running process?
EDIT: Here's a sample of the code that utilizes the XDocument (named XDoc):
private void RunPlugin(object oQueuedPlugin)
{
PluginState oPluginState = (PluginState)oQueuedPlugin;
PluginResponse oResponse = new PluginResponse();
XElement xPlugin;
lock (xDoc)
{
xPlugin = GetPluginNode(oPluginState.ClientFusionDatabase.Name, oPluginState.Plugin.Name);
}
if (xPlugin == null)
{
API.Log.Write("ActivityTrace.ShowXMLLog", "XML for " + oPluginState.ClientFusionDatabase.Name + " was null.");
XElement NewPlugin = new XElement("Plugin",
new XAttribute("PluginName", oPluginState.Plugin.Name),
new XAttribute("Running", "true"),
new XAttribute("LastStart", DateTime.Now.ToString()),
new XAttribute("LastSuccess", ""),
new XAttribute("LastExitStatus",""));
lock (xDoc)
{
var Location = from database in xDoc.Root.Elements("Database")
where database.Attribute("DatabaseName").Value == oPluginState.ClientFusionDatabase.Name
select database;
Location.FirstOrDefault().Add(NewPlugin);
xDoc.Save(XmlLogFilePath);
}
oResponse = oPluginState.Plugin.Run(oPluginState.ClientFusionDatabase);
if (oResponse == null)
{
API.Log.Write("ActivityTrace.ShowNullReturnLog", oPluginState.ClientFusionDatabase.Name + "- " + oPluginState.Plugin.Name + " returned null.");
}
lock (xDoc)
{
NewPlugin.Attribute("Running").Value = "false";
NewPlugin.Attribute("LastExitStatus").Value = oResponse.ResponseType.ToString();
if (oResponse.ResponseType == PluginResponseTypes.Success || oResponse.ResponseType == PluginResponseTypes.Warning)
NewPlugin.Attribute("LastSuccess").Value = DateTime.Now.ToString();
xDoc.Save(XmlLogFilePath);
}
API.Log.Write("ActivityTrace.ShowXMLLog","Completed " + oPluginState.ClientFusionDatabase.Name + "- " + oPluginState.Plugin.Name + " with XML " + NewPlugin.ToString());
API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + (oResponse.ResponseType + ") ").PadRight(9) + "EXIT MESSAGE: " + (string.IsNullOrEmpty(oResponse.Message) ? "None" : oResponse.Message));
}
else
{
DateTime dLastRun = (DateTime)xPlugin.Attribute("LastStart");
bool bRunning = (bool)xPlugin.Attribute("Running");
if ((DateTime.Now - dLastRun) > oPluginState.Plugin.Interval && !bRunning)
{
lock (xDoc)
{
xPlugin.Attribute("LastStart").Value = DateTime.Now.ToString();
xPlugin.Attribute("Running").Value = "true";
xDoc.Save(XmlLogFilePath);
}
oResponse = oPluginState.Plugin.Run(oPluginState.ClientFusionDatabase);
lock (xDoc)
{
xPlugin.Attribute("Running").Value = "false";
xPlugin.Attribute("LastExitStatus").Value = oResponse.ResponseType.ToString();
if (oResponse.ResponseType == PluginResponseTypes.Success || oResponse.ResponseType == PluginResponseTypes.Warning)
xPlugin.Attribute("LastSuccess").Value = DateTime.Now.ToString();
xDoc.Save(XmlLogFilePath);
}
API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + (oResponse.ResponseType + ") ").PadRight(9) + "EXIT MESSAGE: " + (string.IsNullOrEmpty(oResponse.Message) ? "None" : oResponse.Message));
}
else if (bRunning)
API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + ("SKIPPED) ").PadRight(9) + "REASON: Plugin already running");
}
oPluginState.Complete = true;
}
The problem is that one or more of the plugins is not handling an error correctly, which causes it to not return any response and crash the program.
To read XML files without parsing the whole thing and loading it into memory, you can use the XmlReader class. Then to write XML files, you can use the XmlWriter class. There are some examples on the respective MSDN pages.
However, you'll lose all benefits of LINQ and it works quite a bit differently. There is really no way to combine the benefits of LINQ and XDocument while working on the XML files only on disk.
And then when your service crashes, your XmlWriter might still not get disposed, not flushing its buffers to disk and leaving you still with an incomplete XML file. You should solve the bug that causes the service to crash instead.
I have around 300k image files in a remote location. I download (have to) and write the details of these files to a text file (with some additional info). Due to the nature of the info I'm getting, I have to process each file as they arrive (Also I write each file info to a file line) to get some form of statistics for example, I have a list of objects with attributes size and count to see how many images of certain sizes I have.
I have also thought about getting everything read and written to a file without keeping any statistics info where I could just open the file again to add the statistics. But I can't think of a way to process a 250k line multi attribute file for statistics info.
I know the lists (yeah I have 2 of them) and the constant loop for each item is bugging the application down but is there another way? Right now it's been 2 hours and the application is still on 26k. For each image item, I do something like this to keep count where I check if an image comes with a certain size that did come before, I add it to that List item.
public void AddSizeTokens(Token token)
{
int index = tokenList.FindIndex(item => item.size== token.size);
if (index >= 0)
tokenList[index].count+=1;
else
tokenList.Add(token);
}
What a single line from the file I write to looks like
Hits Size Downloads Local Loc Virtual ID
204 88.3 4212 .../someImage.jpg f-dd-edb2-4a64-b42
I'm downloading the files like below;
try
{
using (WebClient client = new WebClient())
{
if (File.Exists(filePath + "/" + fileName + "." + ext))
{
return "File Exists: " + filePath + "/" + fileName + "." + ext;
}
client.DownloadFile(virtualPath, filePath + "/" + fileName + "." + ext);
return "Downloaded: " + filePath + "/" + fileName + "." + ext;
}
}
catch (Exception e) {
return"Problem Downloading " + fileName + ": " + e.Message;
}
You should be changing your tokenList from List<Token> to Dictionary<long, Token>.
The key is the size.
Your code would look like this:
Dictionary<long, Token> tokens = new Dictionary<long, Token>();
public void AddSizeTokens(Token token)
{
Token existingToken;
if(!tokens.TryGetValue(token.size, out existingToken))
tokens.Add(token.size, token);
else
existingToken.count += 1;
}
That will change it from an O(n) operation to a O(1) operation.
Another point to consider is Destrictor's comment. Your internet connection speed is very possibly the bottle neck here.
Well, I thought perhaps the coding was the issue. Some of the problem was indeed so. As per Daniel Hilgarth's instructions, changing to dictionary helped a lot, but only the first 30 minutes. Then It was getting worse by every minute.
The problem was apparently the innocent looking UI elements that I've fed information. They ate away so much cpu that it killed the application eventually. Minimizing UI info feed helped (1.5k per minute to at slowest 1.3k). Unbelievable! Hope it helps others who have similar problems.
I'm writing a C# app using the WebBrowser control, and I want all content I display to come from embedded resources - not static local files, and not remote files.
Setting the initial text of the control to an embedded HTML file works great with this code inspired by this post:
browser.DocumentText=loadResourceText("myapp.index.html");
private string loadResourceText(string name)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(name);
StreamReader streamReader = new StreamReader(stream);
String myText = streamReader.ReadToEnd();
return myText;
}
As good as that is, files referred to in the HTML - javascript, images like <img src="whatever.png"/> etc, don't work. I found similar questions here and here, but neither is asking exactly what I mean, namely referring to embedded resources in the exe, not files.
I tried res://... and using a <base href='..." but neither seemed to work (though I may have not got it right).
Perhaps (following my own suggestion on this question), using a little embedded C# webserver is the only way... but I would have thought there is some trick to get this going?
Thanks!
I can see three ways to get this going:
1: write the files you need to flat files in the temp area, navigate the WebBrowser to the html file, and delete them once the page has loaded
2: as you say, an embedded web-server - herhaps HttpListener - but note that this uses HTTP.SYS, and so requires admin priveleges (or you need to pre-open the port)
3: like 1, but using named-pipe server to avoid writing a file
I have to say, the first is a lot simpler and requires zero configuration.
/// Hi try this may help u.
private string CheckImages(ExtendedWebBrowser browser)
{
StringBuilder builderHTML = new StringBuilder(browser.Document.Body.Parent.OuterHtml);
ProcessURLS(browser, builderHTML, "img", "src");
ProcessURLS(browser, builderHTML, "link", "href");
// ext...
return builderHTML.ToString();
}
private static void ProcessURLS(ExtendedWebBrowser browser, StringBuilder builderHTML, string strLink, string strHref)
{
for (int k = 0; k < browser.Document.Body.Parent.GetElementsByTagName(strLink).Count; k++)
{
string strURL = browser.Document.Body.Parent.GetElementsByTagName(strLink)[k].GetAttribute(strHref);
string strOuterHTML = browser.Document.Body.Parent.GetElementsByTagName(strLink)[k].OuterHtml;
string[] strlist = strOuterHTML.Split(new string[] { " " }, StringSplitOptions.None);
StringBuilder builder = new StringBuilder();
for (int p = 0; p < strlist.Length; p++)
{
if (strlist[p].StartsWith(strHref))
builder.Append (strlist[p].Contains("http")? strlist[p] + " ":
(strURL.StartsWith("http") ? strHref + "=" + strURL + " ":
strHref + "= " + "http://xyz.com" + strURL + " " ));
else
builder.Append(strlist[p] + " ");
}
builderHTML.Replace(strOuterHTML, builder.ToString());
}
}