Vorrei creare un programma in Visual C# che all'avvio vada a leggere un file .txt all'indirizzo:
"
http://www.miosito.it/licenza.tx"
e funzioni in questo modo:
Se licenza.txt esiste allora il programma può essere usato dall'utente altrimenti no e l'applicativo di deve chiudere. Ho provato a scrivere questo codice ma non funziona e non riesco a capire come correggerlo.
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;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string url = "http://www.miosito.com/licenza.txt";
string result = null;
try
{
WebClient client = new WebClient();
result = client.DownloadString(url);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("ERRORE DI LICENZA!");
this.Close();
}
}
private void C_TextChanged(object sender, EventArgs e)
{
}
private void Calcola_Click(object sender, EventArgs e)
{
double Variabile_A = double.Parse(A.Text);
double Variabile_B = double.Parse(B.Text);
double Variabile_C;
Variabile_C = Variabile_A + Variabile_B;
C.Text = Variabile_C.ToString("0");
}
private void A_TextChanged(object sender, EventArgs e)
{
}
private void B_TextChanged(object sender, EventArgs e)
{
}
}
}
ciao e grazie come sempre per il supporto
P.S.: Il programma esegue la somma di 2 numeri. Qui sotto trovate l'interfaccia:
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Liberare le risorse in uso.
/// </summary>
/// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Codice generato da Progettazione Windows Form
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.A = new System.Windows.Forms.TextBox();
this.B = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.C = new System.Windows.Forms.TextBox();
this.Calcola = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(26, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(57, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Variabile A";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(148, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Variabile B";
//
// A
//
this.A.Location = new System.Drawing.Point(29, 53);
this.A.Name = "A";
this.A.Size = new System.Drawing.Size(100, 20);
this.A.TabIndex = 2;
this.A.TextChanged += new System.EventHandler(this.A_TextChanged);
//
// B
//
this.B.Location = new System.Drawing.Point(151, 53);
this.B.Name = "B";
this.B.Size = new System.Drawing.Size(100, 20);
this.B.TabIndex = 3;
this.B.TextChanged += new System.EventHandler(this.B_TextChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(26, 93);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(71, 13);
this.label3.TabIndex = 4;
this.label3.Text = "Somma A + B";
//
// C
//
this.C.Location = new System.Drawing.Point(29, 126);
this.C.Name = "C";
this.C.Size = new System.Drawing.Size(100, 20);
this.C.TabIndex = 5;
this.C.TextChanged += new System.EventHandler(this.C_TextChanged);
//
// Calcola
//
this.Calcola.Location = new System.Drawing.Point(29, 164);
this.Calcola.Name = "Calcola";
this.Calcola.Size = new System.Drawing.Size(75, 23);
this.Calcola.TabIndex = 6;
this.Calcola.Text = "Calcola";
this.Calcola.UseVisualStyleBackColor = true;
this.Calcola.Click += new System.EventHandler(this.Calcola_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.Calcola);
this.Controls.Add(this.C);
this.Controls.Add(this.label3);
this.Controls.Add(this.B);
this.Controls.Add(this.A);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Controllo licenza";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox A;
private System.Windows.Forms.TextBox B;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox C;
private System.Windows.Forms.Button Calcola;
}
}