So im pretty much just starting out with c# and i'm just trying to get the text out of a .txt file and display it on my App. I've tried looking everywhere that I can think of but i've only found people doing this for console Apps and when I try apply the same solution to mine it will just gives errors. This is what i'm trying to use at the moment but I just keep getting the error: "cannot convert 'string' to 'System.IO.Stream'" for the path after StreamReader(path). Ive tried it as an console app(changing the output code) and works fine
namespace FileOpenApp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void inputSubmitButton_Click(object sender, RoutedEventArgs e)
{
outputBox.Text = "Testing";
processFile();
}
private void processFile()
{
try
{
string path = #"C:\Users\Fabian\Dropbox\test.txt";
using (StreamReader sr = new StreamReader(path)) //errors here
{
string line;
while ((line = sr.ReadLine()) != null)
{
outputBox.Text = line;
currentProcess.Text = "Done";
}
}
}
catch (Exception e)
{
currentProcess.Text = "Something went wrong";
}
}
}
}
Edit: trying to use this code I am getting the error that File does not exist in the namespace System.IO
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
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 FileOpenApp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void inputSubmitButton_Click(object sender, RoutedEventArgs e)
{
outputBox.Text = "Testing";
processFile();
}
private void processFile()
{
string path = #"C:\Users\Fabian\Dropbox\test.txt";
string contents = "";
System.IO.File.ReadAllText(path, contents);
outputBox.Text = contents;
}
}
}
Also tried;
string path = #"C:\Users\Fabian\Dropbox\test.txt";
outputBox.Text = File.ReadAllText(path);
You should call File.ReadAllText(), which does all of this for you and returns a string.
Related
I am sorry if my question is silly , I am a beginner.I have two forms:
Form1: Displays a Table of Information
Form2: Displays a Form to Fill information
I need to get the information in Form2 to Form1 Using get methods (If there is a better way please suggest it).
My problem is that when I type those get methods in Form1 they are not recognized.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//---------------------------------Initial Stuff------------------------------------
Form2 form2 = null;
//----------------------------------Constructor-------------------------------------
public Form1()
{
InitializeComponent();
}
private void nouveau_Click(object sender, EventArgs e)
{
if (form2 == null)
{
form2 = new Form2();
form2.Show();
}
}
//---------------------------------ListView of Information------------------------------
ListViewItem lvi = new ListViewItem(getClient());
lvi.SubItems.Add(societe.Text);
lvi.SubItems.Add(datedebut.Text);
lvi.SubItems.Add(type.Text);
lvi.SubItems.Add(etat.Text);
}
}
Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 :
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
client.Text="";
societe.Text = "";
datedebut.Text = "";
type.Text = "";
etat.Text = "";
}
//----------------------------Return Functions for table----------------------
public String getClient()
{
return client.Text;
}
public String getSociete()
{
return societe.Text;
}
public String DateDebut()
{
return datedebut.Text;
}
public String getType()
{
return type.Text;
}
public String getEtat()
{
return etat.Text;
}
}
}
So I update my code and tried another way to do things
Now I have 4 .cs files: Principal, FillInfo, Folder, Program
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Principal());
}
}
}
Folder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class Folder
{
//-----------------------------------------CONSTRUCTOR--------------------------
public Folder()
{
this.Customer = "";
this.Company = "";
this.StartDate = "";
this.TechUsed = "";
this.Status = "";
}
//-----------------------------------------GETTERS AND SETTERS-------------------
public string Customer { get; set; }
public string Company { get; set; }
public string StartDate { get; set; }
public string TechUsed { get; set; }
public string Status { get; set; }
}
}
Principal:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Principal : Form
{
//-----------------------------------INITIAL VARIABLES--------------------------------------------------
FillInfo fillinfo = null;
public Folder f;
//-----------------------------------INITIAL METHODS----------------------------------------------------
public Principal()
{
InitializeComponent();
}
//-----------------------------------ELEMENTS METHODS--------------------------------------------------
// NEW BUTTON
private void pNew_Click(object sender, EventArgs e)
{
f= new Folder();
if (fillinfo == null)
{
fillinfo = new FillInfo();
fillinfo.Show();
}
}
//---------------------------------------PROCESSING-----------------------------------------------------
ListViewItem fillInfoListView = new ListViewItem(f.getCustomer());
}
}
FillInfo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class FillInfo : Form
{
//-----------------------------------INITIAL VARIABLES--------------------------------------------------
//-----------------------------------INITIAL METHODS----------------------------------------------------
public FillInfo()
{
InitializeComponent();
}
//-----------------------------------ELEMENTS METHODS--------------------------------------------------
private void fOkButton_Click(object sender, EventArgs e)
{
f.setCustomer = fCustomerTextField.Text;
f.setCompany = fCompanyTextField.Text;
f.setStartDate = FStartDateDatePicker.Text;
f.setTechUsed = fTechUsedDropList.Text;
f.setStatus = fStatusDropList.Text;
fCustomerTextField.Text = "";
fCompanyTextField.Text = "";
FStartDateDatePicker.Text = "";
fTechUsedDropList.Text = "";
fStatusDropList.Text = "";
}
}
}
Assuming Form2 is something that appears and asks the user for info, then disappears when the user is done typing into it, it would probably look like:
private void nouveau_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.ShowDialog(); //SHOW DIALOG
ListViewItem lvi = new ListViewItem(getClient());
lvi.SubItems.Add(form2.Societe); //the property you are busy writing
lvi.SubItems.Add(form2.DateDebut); //the property you are busy writing
lvi.SubItems.Add(form2.Type); //the property you are busy writing. Try and think of a more hepful name than Type
lvi.SubItems.Add(form2.Etat); //the property you are busy writing
//do you need to add that lvi to something?
}
Remove Form2 from being a class level variable
I'm working on a script that shows a form with links to documents.
It's all working correctly but I would like to add a function that automaticly sends an email to the document controller that the selected document is not present.
The email content should contain text (string) with filename of the document that does not exist.
I have found several possibilities but I'm new to C# and I don't know were to start incorporating it in my current script. I want to use Microsoft Outlook for sending the emails.
I have created the following:
#region Namespaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion
using static OpenDocument.Command;
using Form = System.Windows.Forms.Form;
using Application = Autodesk.Revit.ApplicationServices.Application;
using View = Autodesk.Revit.DB.View;
namespace OpenDocument
{
public partial class Form1 : Form
{
#region Variables
//Document links:
string doc01 = #"C:\docs\3D ModelGuide.docx";
string doc02 = #"C:\docs\3D Checklist.docx";
public Form1()
{
InitializeComponent();
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void btnModelguide3D_Click(object sender, EventArgs e)
{
openFile(doc01);
}
private void btnChecklist3D_Click(object sender, EventArgs e)
{
openFile(doc02);
}
void openFile(string fileName)
{
if (System.IO.File.Exists(fileName) == true)
{
System.Diagnostics.Process.Start(fileName);
}
else
{
TaskDialog.Show("Info", notFoundError(fileName));
}
}
string notFoundError(string fileName)
{
string info = "The requested file/folder" + "\n" + fileName + "\n" + "does not exist!"
return info;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using Opera;
namespace WebApplication1
{
public partial class FirstWebPage : System.Web.UI.Page
{
//Inheritance Matlab
public MLApp.MLApp matlab = new MLApp.MLApp();
public Array result;
public String str;
//Page First Loading function
protected void Page_Load(object sender, EventArgs e)
{
}
//When Log_in Button Clicked
protected void Login_Button_Click(object sender, EventArgs e)
{
Operaclass opera = new Operaclass();
result = opera.add(3,4);
str = "" + result;
result = opera.subtract(5, 4);
str = str + ":::" + result;
TextBox1.Text = ""+str;
//matlab.Execute("questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');");
}
}
}
I am getting error at
Operaclass opera = new Operaclass();
Exception is
The type initializer for 'Opera.Operaclass' threw an exception.
Operaclass is a class in Opera namespace. which is created by Matlab as Opera.dll.
Please help..
Thanks
Working on a basic Windows Universal app and currently have the issue where upon pressing back on the details page of my lists it doesn't take me back to the main page but instead closes the app entirely.
MainPage.xaml.cs
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.Phone.UI;
using Windows.UI.Xaml.Navigation;
namespace MoreUniversalAppWork
{
public sealed partial class MainPage : Page
{
public QuoteReader qr = new QuoteReader();
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
qr.DownloadQuotes();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Hardware_Back(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (Frame.Content is ViewQuote)
{
Frame.Navigate(typeof(MainPage));
e.Handled = true;
}
}
private void LoadQuotes(object sender, RoutedEventArgs e)
{
FillList();
}
private void FillList()
{
// Downloads the JSON file with the quotes and filles List //
qr.DownloadQuotes();
var quotes = qr.quotes_dictionary();
list_quotes.Items.Clear();
foreach (var q in quotes)
{
list_quotes.Items.Add(q.Value);
}
if (list_quotes.Items.Count > 0)
{
list_quotes.ScrollIntoView(list_quotes.Items[0]);
}
}
private void ViewQuote(object sender, SelectionChangedEventArgs e)
{
// Upon clicking quote in list, details page is opened //
Frame.Navigate(typeof(ViewQuote), list_quotes.SelectedValue);
}
}
}
ViewQuote.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ComponentModel;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
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;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
namespace MoreUniversalAppWork
{
public sealed partial class ViewQuote : Page
{
string quote;
public ViewQuote()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
quote = e.Parameter as string;
quote_display.Text = quote;
}
}
}
This is normal in Windows Phone 8.1. You may want to have a workaround in App.xaml.cs file.
public App()
{
this.InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null && rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
WP8.1 back button quits the app
This Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using Managed.Adb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
AndroidDebugBridge mADB;
String mAdbPath;
List<Device> devices = AdbHelper.Instance.GetDevices(AndroidDebugBridge.SocketAddress);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e )
{
//mAdbPath = Environment.GetEnvironmentVariable("PATH");
mAdbPath = "C:\\Users\\Nadun\\AppData\\Local\\Android\\android-sdk\\platform-tools";
mADB = AndroidDebugBridge.CreateBridge(mAdbPath + "\\adb.exe", true);
mADB.Start();
var list = mADB.Devices;
textBox1.Text = "" + list.Count;
foreach (Device item in list)
{
Console.WriteLine("");
listBox1.Items.Add("" + item.Properties["ro.build.product"].ToString() + "-" + item.SerialNumber.ToString() );
}
//Console.WriteLine("" + list.Count);
}
private void button2_Click(object sender, EventArgs e)
{
string text = listBox1.GetItemText(listBox1.SelectedItem);
Form2 f2 = new Form2(text);
// f2.Phone = "scs";
SetPhone sp = new SetPhone();
sp.PhoneModel = "Test";
this.Visible = false;
f2.ShowDialog();
}
}
}
This is Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private string phone;
public string Phone
{
get { return this.phone; }
set { this.phone = value; }
}
public Form2(string a)
{
InitializeComponent();
textBox1.Text = a;
}
private void Form2_Load(object sender, EventArgs e)
{
//Form2 f2 = new Form2();
//f2.phone = "s";
//textBox1.Text = f2.Phone;
SetPhone sp = new SetPhone();
textBox1.Text = sp.PhoneModel;
Console.WriteLine("sefsef-"+sp.PhoneModel);
}
}
}
This is my Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
class SetPhone
{
private string phoneModel;
public string PhoneModel {
get { return this.phoneModel; }
set { this.phoneModel = value; }
}
}
}
Get always returning empty.i don't know why.
I am trying to set values from "form1".
i wrote class for that as well.but when i getting values from "form2" it returning empty.i don't know why
Your SetPhone class object which is calling the setter in the button2_click is a local variable, so when you try access the same in Form2_Load using another local variable, it is a completely new object and Get returns an empty string (default value). You should be able to share the SetPhone variable across forms, may be using constructor, then it will retain the values set using the setter