我有项目,我想收到买家的收据并将其写在记事本上,但我的问题是我在其他计算机上安装我的应用程序。当按钮点击
时,记事本没有打开,没有任何反应 var path = Path.Combine(Directory.GetCurrentDirectory(), "\\Receipt.txt");
using (StreamWriter w = new StreamWriter(path))
{
string x=label1.Text.Replace(" ",string.Empty);
string x2 = label2.Text.Replace(" ", string.Empty);
string x3 = label3.Text.Replace(" ", string.Empty);
string x4 = label4.Text.Replace(" ", string.Empty);
string x5 = label5.Text.Replace(" ", string.Empty);
w.Write("***************OFFICIAL RECEIPT*************** \r\n", true);
w.Write("\r\n"+"\r\n"+"***************Buyer Information*************"+"\r\n");
w.Write(x+"\r\n",true);
w.Write(x2+ "\r\n", true);
w.Write(x3 + "\r\n", true);
w.Write(x4 + "\r\n", true);
w.Write(x5+ "\r\n", true);
w.Write("\r\n" + "***************PURCHASE ITEM*****************" + "\r\n");
w.Write("ITEM QUANTITY PRICE" + "\r\n");
for (int xx = 0; xx<listBox1.Items.Count;xx++ )
{
w.Write(listBox1.Items[xx].ToString()+"\r\n");
}
w.Write("_____________________________________________"+ "\r\n");
w.Write("TOTAL " + totalprice+"\r\n"+"\r\n");
w.Write(" -THIS IS YOUR OFFICIAL RECEIPT- "+"\r\n");
w.Write(" THANK YOU FOR BUYING! ");
w.Close();
}
Process.Start("notepad.exe", path);
答案 0 :(得分:0)
假设这是winforms,作为测试,将catch块更改为:
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
throw;
}
如果它在您的开发箱上工作而不在另一台计算机上工作,我愿意打赌您有权限问题。只需尝试上面的代码,看看你得到了什么消息。然后你就可以弄清楚出了什么问题以及如何解决它。
答案 1 :(得分:0)
尝试更改
var path = Path.Combine(Directory.GetCurrentDirectory(), "\\Receipt.txt");
到
var path = Application.StartupPath + "\\Receipt.txt";
还
Process.Start("notepad.exe", path);
到
Process.Start("notepad.exe", "\"" + path + "\"");
在某些情况下,改变这有助于某些人
如果问题是由于文件夹名称中的空格
,则提供引号可能会有所帮助