I have created a windows application using C#. the problem is fairly simple. I doono how to refer to the Application path in c#. In Vb we can use App.path to refer to the Application path

My C# code for opening a report is as

public partial class frmSpaBill : Form
    {
        string report_file =@"F:\Spa n saloon\Spa n Saloon\Spa n Saloon\SpaServiceBill.rpt";
        ReportDocument ReportDoc = new ReportDocument();
        public frmSpaBill()
        {
            InitializeComponent();
        }

        private void frmSpaBill_Load(object sender, EventArgs e)
        {
            ReportDoc.Load(report_file);
            crystalReportViewer1.ReportSource = ReportDoc;
            makeReport(report_file);
            SetParamValue("@date",BillVariables.date);
            SetParamValue("@timein", BillVariables.timein);
            SetParamValue("@timeout", BillVariables.timeout);
            SetParamValue("@CustID", BillVariables.custID);
            SetParamValue("@CustName", BillVariables.custName);
            SetParamValue("@SpaID", BillVariables.ServID);
            SetParamValue("@SpaServiceName", BillVariables.ServiceName);
            SetParamValue("@Description", BillVariables.Description.Replace("\r\n", "__"));
            SetParamValue("@Duration", BillVariables.Duration);
            
            SetParamValue("@Price", BillVariables.Price);

            crystalReportViewer1.ReportSource = ReportDoc; 
        }

        private void makeReport(string ReportFile)
        {

            ReportDoc.Load(ReportFile);
            crystalReportViewer1.ReportSource = ReportDoc;

        }

        private void SetParamValue(string paramName, string paramValue)
        {
            for (int i = 0; i < ReportDoc.DataDefinition.FormulaFields.Count; i++)
                if (ReportDoc.DataDefinition.FormulaFields[i].FormulaName == "{" + paramName + "}")
                    ReportDoc.DataDefinition.FormulaFields[i].Text =  "\""+paramValue+"\"" ;

        }
        
    }
}

the problem is thet if i move my project to another directory i have to modify the path string..

Can anyone tell me how to refer in c# like in vb. ive searched the internet but have not seen one yet

Recommended Answers

All 5 Replies

System.Reflection.Assembly exe = System.Reflection.Assembly.GetEntryAssembly();
            string exePath = System.IO.Path.GetDirectoryName(exe.Location);
System.Reflection.Assembly exe = System.Reflection.Assembly.GetEntryAssembly();
   
      string exePath = System.IO.Path.GetDirectoryName(exe.Location);

i put SpaServiceBill.rpt in the paranthesis but it does not work

System.Reflection.Assembly exe = System.Reflection.Assembly.GetEntryAssembly();
   
      string exePath = System.IO.Path.GetDirectoryName(exe.Location);

i put SpaServiceBill.rpt in the paranthesis but it does not work

In the code snippet above, the var exePath will contain the path where you executed the application, which is what I think you had asked. Now, if you combine that with your report name (I believe you said that the report file is in the same location), you will have a full path/filename to the report's location:

string rptFile = exePath + "SpaServiceBill.rpt";

You may consider using Path.Combine() when assembling full paths :) -- it can help if you're unsure that the first directory path ends with a path delimiter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.