Granite/0.jpg
我想把,斜杠前面的字符不要,只要0.jpg,我用theEntry.Name.Substring(0,theEntry.Name.IndexOf('/')) 这样实现出来的效果是,把/后面的截取掉了,要怎么把斜杠前面的字符截取掉,大虾们,帮帮忙。
我想把,斜杠前面的字符不要,只要0.jpg,我用theEntry.Name.Substring(0,theEntry.Name.IndexOf('/')) 这样实现出来的效果是,把/后面的截取掉了,要怎么把斜杠前面的字符截取掉,大虾们,帮帮忙。
差不多这样子~
str = str.Substring(str.IndexOf("/")+1);//截后面
//str = str.Substring(0,str.IndexOf("/") - 1); //截前面
Response.Write(str);
。cs 文件
if (FileUpload1.HasFile)
{
try
{
string filePath = Server.MapPath(@"~/ChinaPhoto/CaseImg") + "/" + FileUpload1.FileName; //上传的压缩文件保存的路径
string file1 = FileUpload1.FileName;
string file2 = file1.Substring(0, file1.LastIndexOf(".")); string geshi = file1.Substring(file1.LastIndexOf("."));
if (geshi == ".zip" || geshi == ".dat")
{
string file = Server.MapPath(@"~/ChinaPhoto/CaseImg") + "\\" + file2;
//上传到本地
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(filePath);
}
//1.上传文件名称,作为导航名称
//2.判断导航名称是否存在,
//2.1如果不存在,就添加。
//2.2如果存在,就导入目录,把图片导入文件夹,数据录入数据库。
string sql = "Select * from AASSDD where Type='" + file2 + "'";
OleDbDataAdapter dr = new OleDbDataAdapter(sql, con);
DataSet ds = new DataSet();
dr.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
UnZipClass UnZip = new UnZipClass();
UnZip.UnZip(filePath, filePath.Substring(0, filePath.LastIndexOf('.')), Convert.ToInt32(ds.Tables[0].Rows[0]["PKID"]));
}
else
{
string intosql = "insert into AASSDD(Type,Yc)values('" + file2 + "'," + 0 + ")";
OleDbCommand com = new OleDbCommand(intosql, con);
con.Open();
int i = Convert.ToInt32(com.ExecuteNonQuery());
con.Close();
string topSql = "select top 1 PKID from AASSDD order by PKID DESC";
OleDbCommand Topcom = new OleDbCommand(topSql, con);
con.Open();
string itme = Topcom.ExecuteScalar().ToString();
con.Close();
UnZipClass UnZip = new UnZipClass();
UnZip.UnZip(filePath, filePath.Substring(0, filePath.LastIndexOf('.')), Convert.ToInt32(itme));
this.Label1.Text = "上传成功";
this.Label1.ForeColor = Color.Red;
} }
else
{
this.Label1.Text = "文件格式不正确";
this.Label1.ForeColor = Color.Red;
} }
catch (Exception ex)
{
throw ex;
}
}
else
{
this.Label1.Text = "请选择文件";
this.Label1.ForeColor = Color.Red;
}
}
public string ReplaceTh(string Name)
{
return Name.Replace(" ", "-");
}调用类:
OleDbConnection con = DBHlepr.CreateDB();
public void UnZip(string zipfilepath, string unzippath,int ID)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath)); ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(unzippath);
string fileName = Path.GetFileName(theEntry.Name); //生成解压目录
Directory.CreateDirectory(directoryName); if (fileName != String.Empty)
{
//如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入
if (theEntry.CompressedSize == 0)
break;
//解压文件到指定的目录
//directoryName = Path.GetDirectoryName(unzippath + theEntry.Name.Substring(theEntry.Name.LastIndexOf('/'), theEntry.Name.length - theEntry.Name.LastIndexOf('/')));
directoryName = Path.GetDirectoryName(unzippath + theEntry.Name.Substring(theEntry.Name.IndexOf("/") - 1));
//建立下面的目录和子目录
Directory.CreateDirectory(directoryName); FileStream streamWriter = File.Create(unzippath + theEntry.Name); int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
string sql = "insert into Products(ProductsName,ProductsUrl,Productstype)values('" + fileName + "','" +ReplaceTh(theEntry.Name.ToString()) + "'," + ID + ")";
OleDbCommand com=new OleDbCommand (sql,con);
con.Open();
com.ExecuteNonQuery();
con.Close();
streamWriter.Close();
}
}
s.Close();
}
public string ReplaceTh(string Name)
{
return Name.Replace(" ", "-");
}
String NewName = aa.Substring(i + 1);
Response.Write(NewName);
这样就简单的搞定了
string name=System.IO.Path.GetFileName("路径"); 当然用substring之类的也是可以的。