I have created a windows store application that prints out network information using the using Windows.Networking.Connectivity; name space.
Although the application is running and working correctly, the MegabytesUsed method is return "Not Defined". why is this.
If you run the app in Local Machine and you are connected with ethernet you dont use DataPlan.
If you want to debug open with Simulator and enable "Use simulated network prperties" and then change Data Limit Status Flag and you will see results.
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
DataPlanStatus dps = profile .GetDataPlanStatus() ;
textBlock.Text = dps.DataPlanUsage.MegabytesUsed.ToString();
Related
Issue:
As the title already says; connecting to a newtork using Windows.Devices.WiFi.WiFiAdapter does not seem to work if I'm not already logged in to the PC.
Does anyone have any ideas on why it doesn't work? Or if there are any other workarounds?
Some background:
I've created a console application using Windows.Devices.WiFi.Adapter API to connect to our wireless network automatically in certain situations, such as at startup and logons.
I connect using the following code:
switch (available_network.SecuritySettings.NetworkAuthenticationType)
{
case NetworkAuthenticationType.Open80211:
case NetworkAuthenticationType.None:
case NetworkAuthenticationType.Unknown:
task_connect = Program.MainAdapter.ConnectAsync(available_network, reconnectionKind).AsTask();
break;
default:
task_connect = Program.MainAdapter.ConnectAsync(available_network, reconnectionKind, Program.Credentials).AsTask();
break;
}
The application works fine when I'm logged in to Windows 10, but not while logged out, or from a command line started as another user. This severely limits the possibilities on what I can do with it.
I tried setting it up so it logs in at startup via a scheduled task (using the System account) but while the application runs, can find wireless networks, and tries to connect, it always fails with status NetworkNotAvailable.
I've tried two computers. Both have the same OS version: 21H1 (19043.1348)
Source files:
My application is heavily based on (copy pasted from) https://github.com/Microsoft/Windows-iotcore-samples/blob/master/Samples/WiFiConnector/CS/README.md
My source files are here: https://github.com/henricbjork2liu/wifish
I made a UWP application. I use the roamingdata.I save the setting by this:
public static void WriteCode(string pwd)
{
ApplicationDataContainer RoamingSettings = ApplicationData.Current.RoamingSettings;
RoamingSettings.Values["Code"] = EncryptHelper.PwdEncrypt(pwd);
}
I read the setting by this:
public static string GetCode()
{
ApplicationDataContainer RoamingSettings = ApplicationData.Current.RoamingSettings;
string str = (String)RoamingSettings.Values["Code"];
if (!String.IsNullOrEmpty(str))
return str;
else
return EncryptHelper.PwdEncrypt("123");
}
I complete the application and upload to windows store and passed check. Then I download this app on my phone.
I change the ApplicationData.Current.RoamingSettings on phone. I write something to the ApplicationData.Current.RoamingFolder on my phone.
Next I closed the app on my phone and download the app on my PC. But when I opened the app on my PC, I found that the ApplicationData.Current.RoamingSettings and the ApplicationData.Current.RoamingFolder didn't change anything.
I checked the C:\Users\XXX\AppData\Local\Packages\XXX\RoamingState on my PC,there was nothing. I checked C:\Users\XXX\AppData\Local\Packages\XX\Settings on my PC,there was roaming.lock and settings.dat. But I can't read the lastest settings and roaming data that I haved added on my phone anymore.
I have waited for 2 hours, there was no change on my PC.
there is something that I should state first:
1 All the deploy work was done by windows store.
2 I check my PC application setting after closed the app on phone. I even shut down my mobile phone to observe the change to my PC.
what's wrong with my code? or what's wrong with the roamingdata mechanism? I need an answer, thank you!
The code you've posted is right. However, there are some possible reasons for the failure of roaming data sync:
Any user can benefit from roaming app data if they use a Microsoft account to log on to their device. However, users and group policy administrators can switch off roaming app data on a device at any time. If a user chooses not to use a Microsoft account or disables roaming data capabilities, she will still be able to use your app, but app data be local to each device.
Keep in mind that roaming data is associated with a user's Microsoft account. Roaming data will only sync if a user logs into her devices using the same Microsoft account and installs the app on several devices.
Don't use roaming for data that relies on instant syncing. Windows doesn't guarantee an instant sync; roaming could be significantly delayed if a user is offline or on a high latency network.
Roaming of settings is not instant. The system weighs several factors when determining when to send the data. We can detect whether new roaming data has arrived on the local device by listening for the ApplicationData.DataChanged event. This event occurs when app data has just finished syncing from the cloud. Any time a device receives new roaming data, the DataChanged event will fire, passing in the updated ApplicationData object. This lets us make any adjustments to our app when data has changed.
For important, time critical settings, use the HighPriority setting associated with RoamingSettings like following:
// High Priority setting, for example, last page position in book reader app
roamingSettings.values["HighPriority"] = "65";
This is a special key in the roaming settings we can use for data we need to sync immediately. Adding HighPriority to any setting will have it synced as quickly as possible.
Don't roam large sets of app data. There's a limit to the amount of app data an app may roam; use RoamingStorageQuota property to get this maximum. If an app hits this limit, no data can roam until the size of the app data store no longer exceeds the limit.
The name of each setting can be 255 characters in length at most. Each setting can be up to 8K bytes in size and each composite setting can be up to 64K bytes in size. The sync engine may limit the total size of settings and files that can roam. It’s important to keep track of the amount of data you’re attempting to roam. If the total amount of data you’re attempting to sync exceeds the limit, then nothing will sync between the devices.
App data only roams between installed apps with the same version number. For example, devices on version 2 will transition data between each other and devices on version 3 will do the same, but no roaming will occur between a device running version 2 and a device running version 3. If you install a new app that utilized various version numbers on other devices, the newly installed app will sync the app data associated with the highest version number.
If you are using versioning in your roaming date, please make sure you are dealing with the right version.
These are some possible reasons that can cause roaming data doesn't sync between devices. For more info, please check Roaming data in Store and retrieve settings and other app data.
I have an application that takes screenshots from the local computer.
This works since many years correctly until suddenly a colleague reported me that he got an "The handle is invalid" error from my application.
This error came from inside the .NET framework from Graphics.CopyFromScreen().
To work around this I replaced this function with C++ code using GetDC(GetDesktopWindow()) / GetDC(NULL) and BitBlt() to copy the screen into a bitmap. Now I got ERROR_INVALID_HANDLE.
This happens on Windows 7.
What is going on there ?
I can not investigate this problem on my own because I cannot reproduce it and my colleague is in another country.
I searched in Google and lots of people report this error.
But all posts that I found were from people who tried to take a screenshot from a client computer through ASP code on a server. I don't understand how people can have the strange desire to capture the client's computer from a website. It is obvious that this will not work.
But I could not find one single case where someone reports this problem from an application that cannot capture the screen of the SAME computer in the SAME session where the application itself is running.
After investigating more with my colleague and giving him ideas what he can try, he told me that he starts my application through a remote desktop session.
The remote desktop session creates a virtual desktop (you see for example that the desktop wallpaper is missing).
I told my colleague to install a VNC client to remote control the computer instead of a remote desktop session and now all works fine. He installed TightVNC which uses the REAL desktop user session instead of creating a virtual session and locking the screen of the machine.
So if anyone gets reports of "The handle is invalid" while taking a screen capture, ask your users if they use a remote desktop session.
To detect a remote desktop session in code you can write:
in C++:
if (GetSystemMetrics(SM_REMOTESESSION) > 0)
{
MessageBox(m_hWnd, L"This application may not work correctly in a remote desktop session", "Error", MB_ICONSTOP);
}
or in C#:
if (System.Windows.Forms.SystemInformation.TerminalServerSession)
{
Messagebox.Show("This application may not work correctly in a remote desktop session");
}
Note that the problem is not reproducible on all computers. When I test on my own Windows 7 it works. So there are probably any additional system settings or other factors that trigger the "The handle is invalid" error (service packs / hotfixes...?).
But my colleague reports that he has never seen the error again after he stopped using the remote desktop connection.
There are a few reasons this can happen but the underlying theme is that the desktop window isn't available when this method is called.
In addition to the reasons mentioned above, another reason this can happen is if this method is being called when the screen is locked.
The code for CopyFromScreen has this section:
int result = SafeNativeMethods.BitBlt(targetDC, destinationX, destinationY, destWidth, destHeight, screenDC, sourceX, sourceY, (int) copyPixelOperation);
//a zero result indicates a win32 exception has been thrown
if (result == 0) {
throw new Win32Exception();
}
It would seem to me that the safest course of action would be that if you make use of this function, make sure that you also write your code assuming that receiving a Win32Exception or an unavailable Desktop Window is a use case which must be handle so the application doesn't crash.
Background
The company I work on is developing a kiosk-like application for tablets running Windows 8 Pro (on desktop mode). The user shouldn't be able to access anything that isn't the application itself: charms will be disabled, the taskbar will be hidden behind the application, etc.
This also means the user shouldn't be able to change network settings, leaving the responsability to keep the device always connected to us. Up to now, I had success using the Mobile Broadband API to assure the device is connected whenever there's a mobile network available. It'll detect disconnect events and try to connect again.
The Problem
Although the user shouldn't be able to do it, I'm considering the case where the user follows this steps:
User opens right-side charm,
clicks on Settings,
clicks on Network,
clicks on More PC Settings,
clicks on Wireless, and
disables the mobile broadband device.
I would like to be able to revert this programmatically and enable it again.
The Attempts
I have tried some different ways to force 3G being reenabled. Most of them give me the same result: they supposedly enable the device without errors, but I still cannot use it. Enable-NetAdapter in Powershell doesn't throw errors, and the Enable method of Win32_NetworkAdapter appears to work, but no dice.
I thought maybe the method IMbnRadio::SetSoftwareRadioState could be what I'm after, but I can't get to it when the device is disabled. The method IMbnInterfaceManager::GetInterfaces throws a COMException claiming the element could not be found (HRESULT = 0x80070490).
MbnInterfaceManager mbnInterfaceManager = new MbnInterfaceManager();
IMbnInterfaceManager interfaceManager = (IMbnInterfaceManager)mbnInterfaceManager;
// The following line throws a COMException:
IMbnInterface[] interfaces = (IMbnInterface[])interfaceManager.GetInterfaces();
mobileInterface = interfaces[0];
mobileRadio = (IMbnRadio)mobileInterface;
uint requestId;
mobileRadio.SetSoftwareRadioState(MBN_RADIO.MBN_RADIO_ON, out requestId);
Is there a way to override user preferences set on "More PC Settings?"
I found a sketchy way to solve this. Keep in mind this is undocumented, wrong, shameless and immoral, and will probably break eventually. The client is aware of this, but prefers to keep the access to the OS limited.
The setting in case is saved in the Registry. At least in the computers I've checked, it's stored in HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0022 in a key named RadioOff.
The Airplane Mode setting is also stored in the Registry, but in a different place. It's at HKLM\SYSTEM\CurrentControlSet\Control\RadioManagement\SystemRadioState in a key named (Default).
After changing these keys and rebooting, everything seems to work fine. I'll repeat though: you really shouldn't be doing this, especially the Airplane Mode thing.
I am developing an application using c# and asp. It need to access some places in the local network . There is a text box in the form which accept the path to be accessed from the user and will store it to a string variable named location.
The if loop always return false if the application run in windows 7. and it occurs only when I run from the installed application, otherwise it will return true if the path is true. Here is the code:
The input to textbox BackupLocation is like this
\\192.168.0.33\Others (F)
. It work fine if the application is hosted on a system which have windows XP
System.IO.DirectoryInfo locationInfo = new System.IO.DirectoryInfo(BackupLocationTxt.Text);
if (locationInfo.Exists) // always return false if the application run in windows 7
{
}
Why this happens ?
This happens because the user you are running your application under doesn't have authorization to read those folders. You might need to grant read access to those folders to the account you are running your site under.
Try System.IO.Directory.Exists(string path) instead.
Your ASP.NET application doesn't have permissions to the folder on other computer in local network.
Try use windows service started under LocalService account.