Get url from firefox 8 not working anymore - c#

I have a windows applicaiton c# catching the url of a running firefox instance.
I have always used "MozillaContentWindow" to get firefox URL but i dont understand why it dont work anymore.
string s = GetUrlFromBrowsersWithIdentifier("MozillaContentWindow", foreGround);
public string GetUrlFromBrowsersWithIdentifier(string identifier, int foreground)
{
try
{
IntPtr ptr = new IntPtr(foreground);
var aeBrowser = AutomationElement.FromHandle(ptr);
return aeBrowser == null ? "" : GetURLfromBrowser(aeBrowser, identifier);
}
catch (Exception ex)
{
return "";
}
}
string GetURLfromBrowser(AutomationElement rootElement, string identifier)
{
try
{
Condition condition1 = new PropertyCondition(AutomationElement.IsContentElementProperty, true);
Condition condition2 = new PropertyCondition(AutomationElement.ClassNameProperty, identifier);
var walker = new TreeWalker(new AndCondition(condition1, condition2));
var elementNode = walker.GetFirstChild(rootElement);
if (elementNode != null)
{
var p = elementNode.GetSupportedPatterns();
if (p.Any(autop => autop.ProgrammaticName.Equals("ValuePatternIdentifiers.Pattern")))
{
var valuePattern = elementNode.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
if (valuePattern != null)
return (valuePattern.Current.Value);
}
}
}
catch
{
return "";
}
return "";
}
Now when it enters "walker.GetFirstChild(rootElement);" it just stops there. I cant figure out why. This only happend on latest version of firefox.
Did they change the name of the value bar containing the url?
Thank you

Try using MozillaWindowContentClass for newer versions.

Related

windows application Memory consumption increase while tracking browser(chorme) url using C#

Hi i have a winForms application which runs a 10 sec threading timer and in that function i am calling this function whenever active process is chrome but it start with low system memory of 28 mb and goes upto 500-600mb with in hours if i use chrome constantly can someone tell what i am doing wrong
public string ChromeTabItemURL()
{
try
{
// Url Tracking Enable and Disabled From Web
if (!TrackDayWiseHelper.isTrackDayWiseActivated())
return string.Empty;
if (StorageBag.isUserDisableUrlTrack)
return string.Empty;
Process[] procsChrome = Process.GetProcessesByName("chrome");
if (procsChrome.Length == 0)
{
return "";
}
AutomationElement elm = null;
AutomationElement elmUrlBar = null;
AutomationPattern[] patterns = null;
foreach (Process chrome in procsChrome)
{
// the chrome process must have a window
if (chrome.MainWindowHandle == IntPtr.Zero)
{
continue;
}
// find the automation element
elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
// if it can be found, get the value from the URL bar
if (elmUrlBar != null)
{
if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
{
continue;
}
patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
elmUrlBar = null;
if (val.Current.Value != "")
{
if (Regex.IsMatch(val.Current.Value, #"^(https:\/\/)?[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,4}).*$"))
{
// prepend http:// to the url, because Chrome hides it if it's not SSL
if (!val.Current.Value.StartsWith("http"))
{
string value = "http://" + val.Current.Value;
Console.WriteLine(value);
return value;
}
}
}
return val.Current.Value;
}
elmUrlBar = null;
}
}
procsChrome = null;
elm = null;
elmUrlBar = null;
patterns = null;
return "";
}
catch (System.ComponentModel.Win32Exception ex)
{
ExceptionLogging.SendExcepToDB(ex); Crashes.TrackError(ex);
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex); Crashes.TrackError(ex);
}
return "";
}
code seems to be working fine i tried using jettbrains dott trace and this didnt come up as most resource consuming process but dont know why it is happening if i run application without this everything seems to be working fine

The last value in Read.Line() StreamReader

Tell me please, how can I do so that would only give the last meaning of this search.
At the moment, about 15 MessageBox.Show is opened.
How to make it so that only the latter would show?
For the fifth hour I am suffering with different variations. Nothing happens.
TextReader tr = null;
try
{
File.Copy(SteamLogFilePath, tmpFile, true);
}
catch { }
try
{
tr = new StreamReader(tmpFile);
try
{
string line = null;
while (true)
{
line = tr.ReadLine();
if (line.Contains("RecvMsgClientLogOnResponse") && line.Contains("OK"))
{
tmpSplit1 = line.Split(')');
string SteamIdbrut = tmpSplit1[1];
tmpSplit1 = SteamIdbrut.Split(']');
string SteamIdnet = tmpSplit1[0].Replace(" : [", "");
long steam32 = Convert.ToInt64((GetFriendID(SteamIdnet)));
MessageBox.Show((FromSteam32ToSteam64(steam32)).ToString());
}
}
}
catch { }
}
catch { }
if (tr != null)
tr.Close();
You could use this instead:
string lastLine = File.ReadLines(tmpFile)
.Where(line => line.Contains("RecvMsgClientLogOnResponse") && line.Contains("OK"))
.LastOrDefault();
// rest of code to create the MessageBox
This code replaces all of your code from 2nd try to tr.Close();.

How to fix a boolean method that returns true everytime asp.net

I designed my webpage to read a data string then display the results on labels in an html table. I am attempting to highlight the row that my database reads as a current order. My only problem is only one record is set to be active but they all highlight as if they were active. I use an array to set my data and I also use the label to get the ID I need (all is in code below). I have posted my method and where I use it in the asp page load. How can I fix my method to return correctly?
The implementing of the method in page load
if (lineData.IsCurrentOrderFind(L68.Text))
{
myTable.Rows[1].Cells[0].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[1].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[2].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[3].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[4].BgColor = "#FE2E2E";
}
Here is method that label above gets passed to
public bool IsCurrentOrderFind(string itemNumber)
{
StringBuilder sqlString = new StringBuilder();
sqlString.Append("SELECT * ");
sqlString.Append("FROM WorkOrder ");
sqlString.Append("WHERE LineNumber = " + ConfigurationManager.AppSettings["Line"] + " AND LineCompleted = 0 AND (ScaleGroup LIKE '%1' OR ScaleGroup LIKE '%3') ");
sqlString.Append(" AND CaseGenNum6 = #CaseGenNum6");
SqlDataReader reader = null;
SqlConnection dbConn = App_Code.DBHelper.getConnection();
SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("#CaseGenNum6", itemNumber) };
try
{
reader = App_Code.DBHelper.executeQuery(dbConn, sqlString.ToString(), parameters);
while (reader.Read())
{
IsCurrentOrder = (reader["IsCurrentOrder"] != DBNull.Value && !string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())) ? true : false;
}
reader.Close();
reader.Dispose();
dbConn.Close();
dbConn.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (dbConn != null)
{
try { dbConn.Close(); dbConn.Dispose(); }
catch { }
}
if (reader != null)
{
try { reader.Close(); reader.Dispose(); }
catch { }
}
}
if (IsCurrentOrder == true) I realize this is not necessary
{
return true;
}
else
{
return false;
}
}
The problem could be with this expression:
!string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())
Instead of calling ToString(), try simply casting it to a string:
!string.IsNullOrEmpty((string)reader["IsCurrentOrder"])
Possibly even better (the previous line might throw an exception if it's not really a string):
!string.IsNullOrEmpty(reader["IsCurrentOrder"] as string)
The reason being is that if the string is really null, calling ToString() will return a non-null string "null".
IsCurrentOrder is not declared locally. It seems to be declared at a higher scope. When you enter this function, nothing is initializing the variable (back to false). So, it is remaining at its last setting. Try this code instead:
public bool IsCurrentOrderFind(string itemNumber)
{
bool IsCurrentOrder = false;
//and the rest of your source code
the line
IsCurrentOrder = (reader["IsCurrentOrder"] != DBNull.Value && !string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString())) ? true : false;
}
It's not actually checking the value of the field, only that it's not null or empty.
Try
if(
(reader["IsCurrentOrder"] != DBNull.Value
&&
!string.IsNullOrEmpty(reader["IsCurrentOrder"].ToString()))
)
{
IsCurrentOrder = reader["IsCurrentOrder"];
}
else
IsCurrentOrder = false;
I think there is a lot of refactoring you could do to this method though that will simplify the logic.

How can I change the title of the welcomepage programatically?

The code below is not working. (no exceptions thrown). Totally clueless why is not changed When I check in the GUI, there is a new version, with no changes!
public static void SetEntityWebName(ProcessEntity entity, SPWeb entityWeb)
{
try
{
entityWeb.AllowUnsafeUpdates = true;
var welcomePageListItem = entityWeb.GetFile(entityWeb.RootFolder.WelcomePage).Item;
var welcomePage = entityWeb.GetFile(entityWeb.RootFolder.WelcomePage);
welcomePage.CheckOut();
if (entity.Type == Entity.Job)
{
entityWeb.Title = ((SyncJobs_Result)entity.Entity).JobName;
welcomePageListItem["Title"] = ((SyncJobs_Result)entity.Entity).JobName;
welcomePage.Update();
}
if (entity.Type == Entity.Client)
{
entityWeb.Title = ((SyncClients_Result)entity.Entity).ClientName;
welcomePageListItem["Title"] = ((SyncClients_Result)entity.Entity).ClientName;
welcomePage.Update();
}
if (entity.Type == Entity.Opportunity)
{
entityWeb.Title = ((SyncOpportunities_Result)entity.Entity).OpportunityName;
welcomePageListItem["Title"] = ((SyncOpportunities_Result)entity.Entity).OpportunityName;
welcomePage.Update();
}
welcomePage.CheckIn(string.Empty);
welcomePage.Publish(string.Empty);
entityWeb.Update();
}
catch (Exception ex)
{
}
}
I think you also have to update the welcomePageListItem list item .
I am not sure but , give it a try

How can I turn ON radio of a Wifi adapter that is actually OFF?

I'm using Managed Wifi to get the radio state of my Wifi adapter.
How can I turn the radio ON in case it is actually off ?
Something like this :
WlanClient wlanClient = new WlanClient()
var targetInterface = wlanClient.Interfaces.FirstOrDefault()
if (targetInterface != null)
{
bool radioIsOff = targetInterface .RadioState.PhyRadioState[0].dot11SoftwareRadioState == Wlan.Dot11RadioState.Off;
if (radioIsOff)
{
// TODO
}
}
I just added this to the WlanInterface class of the Managed Wifi API :
IntPtr radioStatePtr = new IntPtr(0L);
try
{
Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
radioState.dwPhyIndex = 0; // TODO : can change ???
radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On; // ignored in fact, according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms706791(v=vs.85).aspx
radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.On;
radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
Marshal.StructureToPtr(radioState, radioStatePtr, false);
Wlan.ThrowIfError(
Wlan.WlanSetInterface(
client.clientHandle,
info.interfaceGuid,
Wlan.WlanIntfOpcode.RadioState,
(uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)),
radioStatePtr,
IntPtr.Zero));
}
finally
{
if (radioStatePtr.ToInt64() != 0)
Marshal.FreeHGlobal(radioStatePtr);
}
Tested on Win 7.
I was struggling with this and I just want to share my solution
(download managed wifi recommended above)
Add WlanApi.cs and Interop.cs to your project.
Add using NativeWifi.
In WlanApi.cs
change to:
public IntPtr clientHandle;
(You need the clientHandle. Not sure why it was set to private?)
Use this code:
string arg1 = "true"; //set to false if you want to turn it off.
arg1 = arg1.ToLower();
IntPtr radioStatePtr = new IntPtr(0L);
try
{
WlanClient wc = new WlanClient();
foreach (var iface in wc.Interfaces)
{
//WlanInterface
if(iface.InterfaceName.ToLower()=="wifi")
{
Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
radioState.dwPhyIndex = 0;
if(arg1=="true")
{
radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On;
radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.On;
}
else
{
radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.Off;
radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.Off;
}
radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
Marshal.StructureToPtr(radioState, radioStatePtr, false);
Wlan.WlanSetInterface(wc.clientHandle, iface.InterfaceGuid, Wlan.WlanIntfOpcode.RadioState, (uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)), radioStatePtr, IntPtr.Zero);
}
}
}
finally
{
if (radioStatePtr.ToInt64() != 0)
Marshal.FreeHGlobal(radioStatePtr);
}
Good luck :)

Categories

Resources