请教c#相对路径

C语言答疑课堂 2009-03-25 03:42:53
我在机器A写了一个windows form程序,上面由一个picturebox控件,项目名称叫windowsform。我把这个项目保存在D:\sxp中,picturebox控件的imagelocation是A机器的D:\sxp\windowsform\images\1.gif; 我把这个程序移植到B机器上的E:\gyf上,那么以上那个picturebox控件肯定不能显示1.gif.因为imagelocation用的是绝对路径,这个我知道。那么我要把imagelocation改成相对路径,以在B机器上也可以用,该如何改呢?
...全文
6044 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
gatwin 2011-05-22
  • 打赏
  • 举报
回复
找了好久哦,
longlin_rc 2010-12-08
  • 打赏
  • 举报
回复
我也有类似的问题,不过我的问题在wpf中
Alexander_QQ 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 readfuture 的回复:]

引用 6 楼 swalp 的回复:
C# code
string path=System.Windows.Forms.Application.StartupPath + @"../../../";
System.IO.Directory.SetCurrentDirectory(path);
FileInfo OldFileName = new FileInfo(System.IO.Dir……
[/Quote]
很正解
ianxia2 2010-02-10
  • 打赏
  • 举报
回复
我看看~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
readfuture 2009-03-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 swalp 的回复:]
C# code
string path=System.Windows.Forms.Application.StartupPath + @"../../../";
System.IO.Directory.SetCurrentDirectory(path);
FileInfo OldFileName = new FileInfo(System.IO.Directory.GetCurrentDirectory() + @"\images\1.gif");
[/Quote]
正解
tianjinldl 2009-03-25
  • 打赏
  • 举报
回复
up
mastzou 2009-03-25
  • 打赏
  • 举报
回复
同意楼上:
.\images\1.gif
whowhen21 2009-03-25
  • 打赏
  • 举报
回复
不好意思哦,少了一斜杠哈。
string path = Application.ExecutablePath + @"\images\1.gif";
whowhen21 2009-03-25
  • 打赏
  • 举报
回复
这个嘛:
string path = Application.ExecutablePath + @"images\1.gif";
//这样不就搞定了嘛
nwgogogo 2009-03-25
  • 打赏
  • 举报
回复
5楼正解
swalp 2009-03-25
  • 打赏
  • 举报
回复
我认为很相对,在你的工程下创建一个images文件夹里面放上图片就行了吧
swalp 2009-03-25
  • 打赏
  • 举报
回复

string path=System.Windows.Forms.Application.StartupPath + @"../../../";
System.IO.Directory.SetCurrentDirectory(path);
FileInfo OldFileName = new FileInfo(System.IO.Directory.GetCurrentDirectory() + @"\images\1.gif");
hxxxs 2009-03-25
  • 打赏
  • 举报
回复
AppDomain.CurrentDomain.BaseDirectory是当前程序集的路径,把images文件夹和exe文件放一起,
AppDomain.CurrentDomain.BaseDirectory+"images\\1.gif"就是图片路径了
foryouwife 2009-03-25
  • 打赏
  • 举报
回复
Application.StartupPath这个能获取.exe文件的地址。在这个基础上写相对地址上去就可以了。把分给我吧。
moonshineidolon 2009-03-25
  • 打赏
  • 举报
回复

具体的例子你可以参照

http://hi.baidu.com/netcorner/blog/item/da2cfbdc887e96a7cd1166f1.html
moonshineidolon 2009-03-25
  • 打赏
  • 举报
回复
最简单的办法是把这个图片变成程序的资源

办法有两个
一个是把图片加入到资源文件

一个是在VS里点击图片,选中属性,有个生成操作 -》选中 嵌入的资源


这样你的文件就变成资源了,就可以用反射把这个图片取出来,
在你的命名空间里就可以找到
xupeihuagudulei 2009-03-25
  • 打赏
  • 举报
回复
uo

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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