Why do I get the following error on the C# LINQ code below
DriveInfo does not exist in current context?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var drives = DriveInfo.GetDrives()
.Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
}
}
}
Many thanks in advance!
You need a using directive:
using System.IO;
... to import System.IO.DriveInfo.
Related
I found same question in stack over flow as here and tried same think but still I am receiving ApplicationContext.Current as null.
I am making the web service where I need to pull out a couple of pieces of data from an Umbraco database. I don't need any of the Umbraco views or any of that stuff. I'm new to the Umbraco Core libraries.
What I did was get a below reference to my new web service project
umbraco.dll
Umbraco.Core.dll
umbraco.DataLayer.dll
umbraco.editorControls.dll
umbraco.MacroEngines.dll
umbraco.providers.dll
Umbraco.Web.UI.dll
umbraco.XmlSerializers.dll
UmbracoExamine.dll
And the below class code where I am receiving ApplicationContext.Current as null
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Web.Mvc;
using umbraco.MacroEngines;
using Umbraco.Web;
using Umbraco.Web.WebApi;
using umbraco.NodeFactory;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using umbraco.cms.businesslogic.media;
using umbraco.BusinessLogic;
using System.Configuration;
using log4net;
using System.Reflection;
using Umbraco.Core.Logging;
using System.Web.Optimization;
using System.Web.Http;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using Stripe;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using System.Web.Security;
using Newtonsoft.Json;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.IO;
public class RegisterUserController : UmbracoApiController
{
public static string UmbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
public static IMemberService memberService = ApplicationContext.Current.Services.MemberService;// Here I am getting ApplicationContext.Current as null
}
But I am calling this class I am getting ApplicationContext.Current as null.
So because of that I have Many methods in this class which works on memberService and due to this null reference they are not working. Even other methods which are not using member service are not being called.
For me, the code below seems to work.
public class TestApiController : UmbracoApiController
{
private static string _umbracoConnectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
private static IMemberService _memberService = global::Umbraco.Core.ApplicationContext.Current.Services.MemberService;
public int GetTest()
{
var memberCount = _memberService.Count();
return memberCount;
}
}
If the above code isn't working for you e.g. with AJAX, try the below code:
public class TestApiController : UmbracoApiController
{
public int GetTest()
{
var ms = Services.MemberService;
return ms.Count();
}
}
I am getting 'Method must have a return type' whenever i tried to run this project. I would appreciate your help as i have been struggling to resolve this issue to no avail. Thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;
using System.Threading;
namespace AppiumTest
{
public class Class1
{
AppiumDriver driver;
TestMethod1()
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability("MobileCapabilityType.deviceName", "Test");
capabilities.SetCapability("MobileCapabilityType.deviceName", "Test");
capabilities.SetCapability("MobileCapabilityType.platformVersion", "Emulator");
capabilities.SetCapability("MobileCapabilityType.platformName", "Android");
capabilities.SetCapability("MobileCapabilityType.appPackage", "");
capabilities.SetCapability("MobileCapabilityType.appActivity", "");
driver = new AndroidDriver<AppiumWebElement>(new Uri("http://127.0.0.1.4723/wd/hub"), capabilities);
Thread.Sleep(5000);
driver.FindElementById("com.paypoint.energycontrols:id/btn_sign_in").Click();
driver.FindElement(By.Id("com.paypoint.energycontrols:id/et_email")).SendKeys("");
driver.FindElement(By.Id("com.paypoint.energycontrols:id/et_password")).SendKeys("");
driver.FindElementById("com.paypoint.energycontrols:id/btn_show_pass").Click();
driver.FindElementById("com.paypoint.energycontrols:id/btn_sign_in").Click();
}
}
}
Your method declaration has only method name. In C# methods must have return type and a name. Add a void keyword before TestMethod1():
void TestMethod1() { ... }
I create music player, because I'm learning how to write a Universal App. I'm trying to load a list of all songs in a folder but when I use the GetFiles, Visual Studio shows me the error: "
The name 'Directory' does not exist in the current context"
even though at the beginning of the code I have "using System.IO". What am I doing wrong?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace project_2
{
public sealed partial class Select : Page
{
private void add_new_song(int song_number)
{...}
private string[] load_songs()
{
string[] paths = Directory.GetFiles(#"C:\");
}
public Select()
{...}
private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{...}
}
}
Directory is not part of store apps SDK, and even if it would GetFiles would not exist as there are no synchronous methods in the SDK to access disc resources.
You are likely looking for StoreageFolder.GetFilesAsync or similar API...
I' trying to do port forwarding. I've done following:
NATUPNPLib.dll was added to references
NATUPNPLib was added to using section of page
Following codes were written
But a null reference exception thrown
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Net.Sockets;
using System.Net;
using NATUPNPLib;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
UPnPNATClass upnpnat = new UPnPNATClass();
IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;
foreach (IStaticPortMapping map in mappings)
{
Console.WriteLine(map.ExternalIPAddress);
}
Console.ReadKey();
}
}
}
How can I overcome this problem?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Web.Security;
using System.Data;
using System.IO;
using SurelyKnown.Core;
using System.Configuration;
using System.Collections;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;
using System.Windows.Forms;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod(EnableSession = true)]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
int newOrgID = Convert.ToInt32(Session["uOrgID"].ToString());
The error is on the last line
CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'
WHat should I do to get the session value inside the method.
Use HttpContext.Current.Session
int newOrgID=0;
if(HttpContext.Current.Session["uOrgID"]!=null)
{
int.TryParse(HttpContext.Current.Session["uOrgID"].ToString(),out newOrgID);
}
check for null before using it, something like this:
if(Session["uOrgID"] != null)
{
int newOrgID = Convert.ToInt32(Session["uOrgID"].ToString());
}
You should also read this article to really understand how to access Session state from web services (including web and page methods): Using ASP.NET Session State in a Web Service