求大神-找错误

qq_32197169 2018-09-15 04:28:27

private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices[0] < 0 || listBox2.SelectedIndex < 0)//if (listView1.SelectedIndices[0] < 0 || listBox2.SelectedIndex < 0)
{
MessageBox.Show("请先选择相应文件");
return;
}

double fPrice = 0;
string sPrice = textBox6.Text;
{
{
}
}
fPrice = Parse(sPrice);
{
}
FileStream fs = null;
try
{

string FilePath = Path + "\\" + listView1.SelectedItems[0].Text + "\\" + listBox2.SelectedItem.ToString();
FileInfo fi = new FileInfo(FilePath);

fs = File.Open(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

byte[] FileByte = new byte[fs.Length];
fs.Read(FileByte, 0, (int)fs.Length);

//日期设置
int pos = (int)GetLinePos(FileByte, 11);//日期位置23行
var PriceBytesByTen = Encoding.ASCII.GetBytes(GetPriceStr(fPrice, 10));//日期位置剩余10个可用字符
PriceBytesByTen.CopyTo(FileByte, pos + 26);//日期位置去除前26个字

fs.Seek(0, SeekOrigin.Begin);
fs.Write(FileByte, 0, FileByte.Length);
fs.SetLength(FileByte.Length);

MessageBox.Show("修改成功");
}
catch(Exception ex)
{
MessageBox.Show("修改失败:"+ex.Message);
if(fs!=null)
fs.Dispose();
}
}
}

}

大家好我是新人 代码是别人那里复制来的文本修改 修改成功后后面多了2位小数 .00 还有不可以输入符号
求大神帮忙修改下 去掉2位小数 和可以输入任何字符
...全文
241 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_32197169 2018-09-15
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Price
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd,out Rect lpRect);
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32", EntryPoint = "ScreenToClient")]
public static extern int ScreenToClient(int hwnd,ref POINTAPI lpPoint);
public struct POINTAPI
{
public uint x;
public uint y;
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
private static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr hWnd, StringBuilder ClassName, int nMaxCount);
[DllImport("user32.dll")]
public static extern IntPtr GetActiveWindow();
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);
[DllImport("user32.dll")]
public static extern IntPtr GetFocus();
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMiliseconds;
}
// 用于设置系统时间
[DllImport("Kernel32.dll")]
public static extern bool SetLocalTime(ref SystemTime sysTime);
// 用于获得系统时间
[DllImport("Kernel32.dll")]
public static extern void GetLocalTime(ref SystemTime sysTime);
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, IntPtr lpszWindow);
[DllImport("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);
[DllImport("user32.dll")]
private static extern bool EnumChildWindows(IntPtr hwndParent, WNDENUMPROC lpEnumFunc, int lParam);
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
public class Dir
{
public List<FileItem> Files = new List<FileItem>();
public string Name;
public DateTime Time;
}
public struct FileItem
{
public string Name;
public string Count;
}
Thread ChangeTimeThread = null;
DateTime BeginTime = DateTime.Now;
public List<Dir> Dirs = new List<Dir>();

public string Path = "FloderPath ";
public string WindowTitle = "WindowTitle ";
public string Password = "Password ";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count < 1)
return;
if ( listView1.SelectedIndices[0] < 0 || listBox2.SelectedIndex < 0)
{
MessageBox.Show("请先选择相应文件");
return;
}

double fPrice = 0;
string sPrice = textBox1.Text;
bool NotNumber = false;
foreach (var item in sPrice)
{
if ((item < '0' || item > '9') && item != '.')
{
NotNumber = true;
break;
}
}
if (sPrice.Contains('.') && (sPrice.IndexOf('.') < sPrice.Length - 3 || sPrice.IndexOf('.') > sPrice.Length-2))
{
MessageBox.Show("请输入有效的价格");
return;
}
fPrice = Parse(sPrice);
if (NotNumber || fPrice > 999999 || fPrice < -99999)
{
MessageBox.Show("请输入有效的价格");
return;

}
FileStream fs = null;
try
{

string FilePath = Path + "\\" + listView1.SelectedItems[0].Text + "\\" + listBox2.SelectedItem.ToString();
FileInfo fi = new FileInfo(FilePath);

var CreationTime = fi.CreationTime;
var LastAccessTime = fi.LastAccessTime;
var LastWriteTime = fi.LastWriteTime;

fs = File.Open(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);


byte[] FileByte = new byte[fs.Length];
fs.Read(FileByte, 0, (int)fs.Length);

//第一条数量
int pos = (int)GetLinePos(FileByte, 21);////////第一条数量位置21行
byte[] OneCountBytes = {0x20, 0x20, 0x20, 0x20, 0x20, 0x31, 0x2E, 0x30, 0x30 };
OneCountBytes.CopyTo(FileByte, pos);

//第一条价格
byte[] PriceBytes = Encoding.ASCII.GetBytes(GetPriceStrRight(fPrice, 8));////////第一条价格位置剩余8个可用字符
PriceBytes.CopyTo(FileByte, pos + 12);////////第一条价格位置去除前12个字符

//第一条第二处价格
pos = (int)GetLinePos(FileByte, 23);//第二处价格位置23行
var PriceBytesByTen = Encoding.ASCII.GetBytes(GetPriceStr(fPrice, 10));////////第二价格位置剩余10个可用字符
PriceBytesByTen.CopyTo(FileByte, pos - 17);////////第二价格位置去除后17个字

//删除多余项
int pos2 = 0;
int LineCount = GetLineCount(FileByte);
if (LineCount > 32)///////多出32行全部删除
{
pos = (int)GetLinePos(FileByte, 23);//23行打折清零等于0
byte[] BottomBytes = {0xC1, 0xCB, 0xCB, 0xC1, 0xC3, 0xC5, 0xD3, 0x20, 0xC5, 0xCD, 0xD4, 0xCF, 0xD3};
int NewLen = 0;
if (FindStrinByte(FileByte, BottomBytes) > 0)
{
pos2 = (int)GetLinePos(FileByte, LineCount - 10);////////剩余从下面往上数10行不删除
}
else
{
pos2 = (int)GetLinePos(FileByte, LineCount - 9);////////剩余从下面往上数9行不删除
}
byte[] LastBytes = new byte[FileByte.Length - pos2];
Array.Copy(FileByte, pos2, LastBytes, 0, FileByte.Length - pos2);
Array.Copy(LastBytes, 0, FileByte, pos, LastBytes.Length);
NewLen = pos + (FileByte.Length - pos2);
var FileByteTemp = new byte[NewLen];
Array.Copy(FileByte, FileByteTemp, NewLen);
FileByte = FileByteTemp;
}

//第三处价格
pos = (int)GetLinePos(FileByte, 26);///////第三处价格位置26行
PriceBytesByTen.CopyTo(FileByte, pos - 13);//////第三处价格位置剩余13个可用字符

//第四处价格
pos = (int)GetLinePos(FileByte, 28);///////第4处价格位置28行
PriceBytesByTen.CopyTo(FileByte, pos - 12);//////第4处价格位置剩余12个可用字符

//最底部数量
byte[] TotalBytes = { 0xD3, 0xD5, 0xCD, 0x2E, 0xD0, 0xCF, 0xD3, 0xCF, 0xD4, 0xC7, 0xD4, 0xC1, 0xD3, 0x3A };
pos = FindStrinByte(FileByte, TotalBytes);
byte[] WriteTotalBytes = { 0xD3, 0xD5, 0xCD, 0x2E, 0xD0, 0xCF, 0xD3, 0xCF, 0xD4, 0xC7, 0xD4, 0xC1, 0xD3, 0x3A, 0x20, 0x20, 0x20, 0x31, 0x2E, 0x30, 0x30 };
WriteTotalBytes.CopyTo(FileByte, pos);

fs.Seek(0, SeekOrigin.Begin);
fs.Write(FileByte,0, FileByte.Length);
fs.SetLength(FileByte.Length);

#region
#endregion
fs.Flush();

fs.Close();
fs.Dispose();

File.SetCreationTime(FilePath,CreationTime);
File.SetLastWriteTime(FilePath, LastWriteTime);
File.SetLastAccessTime(FilePath, LastAccessTime);

TodayPrice[listBox2.SelectedIndex] = (decimal)fPrice;

decimal TotalPrice = 0;
foreach (var item in TodayPrice)
{
TotalPrice += item;
}
textBox5.Text = TotalPrice.ToString().Replace(',','.');

MessageBox.Show("修改成功");
}
catch(Exception ex)
{
MessageBox.Show("修改失败:"+ex.Message);
if(fs!=null)
fs.Dispose();
}


}
private bool isHave2(byte[] bytes)
{
byte[] lbytes = new byte[] { 0xC1, 0xCB, 0xCB, 0xC1, 0xC3, 0xC5, 0xD3, 0x20, 0xC5, 0xCD, 0xD4, 0xCF,
0xD3, 0x20, 0x31, 0x30, 0x20, 0xC7, 0xCC, 0xC5, 0xD1, 0xD9, 0xCD, 0x20, 0xC1, 0xCB, 0xCB, 0xC1,
0xC3, 0xC5, 0xD3, 0x20, 0xC5, 0xCD, 0xD4, 0xCF, 0xD3, 0x20, 0x31, 0x30, 0x20, 0xC7, 0xCC, 0xC5,
0xD1, 0xD9, 0xCD};
if(FindStrinByte(bytes, lbytes) != -1)
return true;
return false;

}
private bool isHaveLP(byte[] bytes)
{
byte[] lbytes = new byte[] { 0x3C,0x25,0x4C,0x50,0x3E};
if (FindStrinByte(bytes, lbytes) != -1)
return true;
return false;

}
private int FindStrinByte(byte [] bytes,byte[] byteStr)
{
for (int i = 0; i < bytes.Length-byteStr.Length; i++)
{
bool Find = true ;
for (int j = 0; j < byteStr.
kuake56 2018-09-15
  • 打赏
  • 举报
回复
输入在哪??

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧