希望访问visualstudio2013中另一个解决方案中表单中文本框中的数据

本文关键字:文本 数据 中文 解决方案 访问 visualstudio2013 另一个 希望 表单 | 更新日期: 2023-09-27 18:00:47

我有两个解决方案TrafferService和Sender。TranferService有WCF服务和IISHost来承载该服务。在Sender解决方案中,我有windows窗体应用程序。在该表单中,我使用按钮浏览和选择文件,文本框显示选定的文件路径,另一个按钮(发送)通过WCF服务传输该文件。但我无法访问传输解决方案中的文本框值。它显示"该名称在当前上下文中不存在"
TransferService 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TransferService" in both code and config file together.
public class TransferService : ITransferService
{
    public File DownloadDocument()
    {
        File file = new File();
        String path = txtSelectFilePath.Text;
        file.Content = System.IO.File.ReadAllBytes(@path);
        //file.Name = "ImagingDevices.exe";
        return file;
    }
}
}

我在这行遇到错误

String path = txtSelectFilePath.Text;

表单.cs 代码

    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 Sender
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Browse_Click(object sender, EventArgs e)
    {              
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            txtSelectFilePath.Text = openFileDialog1.FileName;
        }
    }
    private void Send_Click(object sender, EventArgs e)
    {
        TransferService.TransferServiceClient client = new TransferService.TransferServiceClient();
        TransferService.File file = client.DownloadDocument();
        System.IO.File.WriteAllBytes(@"C:'DownloadedFiles'" + file.Name, file.Content);
        MessageBox.Show(file.Name + " is downloaded");
    } 

}
}

ITransferService.cs 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ITransferService" in both code and config file together.
[ServiceContract]
public interface ITransferService
{
    [OperationContract]
    File DownloadDocument();
}
[DataContract]
public class File
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public byte[] Content { get; set; }
}
}

提前很多Thanx。。。。。。。。。。

希望访问visualstudio2013中另一个解决方案中表单中文本框中的数据

然后为类创建一个构造函数,该构造函数以字符串形式接收路径,如下所示:

public class TransferService : ITransferService
{
    private string _path;
    public TransferService(string path) {
        _path  = path
    }
    public File DownloadDocument()
    {
        File file = new File();
        //String path = txtSelectFilePath.Text;
        file.Content = System.IO.File.ReadAllBytes(_path);
        //file.Name = "ImagingDevices.exe";
        return file;
    }
}

然后在.cs 上

 TransferService.TransferServiceClient client = new TransferService.TransferServiceClient(txtSelectFilePath.Text);