安装后程序找不到文件 [英] program can't find file after install

查看:62
本文介绍了安装后程序找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个程序,该程序使用包含站点列表的预制文本文件。
现在可以在某些计算机上正常运行,但是在我的朋友计算机上则不能。



我在我的Windows 7计算机中的2台上检查了该程序,和1 XP,我没有任何错误。
该程序在XP上使用了一段时间,现在我的朋友想在家里的Windows 7计算机上安装该程序,但是安装该程序后找不到该文件。



这是他得到的错误:

  System.IO.FileNotFoundException:文件未找到' C:\用户\eli\AppData\漫游\fourmlinks.txt。 
文件名:'C:\Users\eli\AppData\Roaming\fourmlinks.txt'

问题是我将此文件发送到主程序文件夹(应用程序文件)中,但仍然找不到它。



这是程序启动时我用来查找文件的代码:

  sring path =; 
path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+ \\fourmlinks.txt;
System.OperatingSystem osInfo = System.Environment.OSVersion;
if(osInfo.Platform == PlatformID.Win32NT)
{
if(osInfo.Version.Major == 5&& osInfo.Version.Minor!= 0)
{
//正在运行XP
//路径= Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+ \\fourmlinks.txt;
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()。Location)+ \\fourmlinks.txt;
}
}

您可以看到,我试图确保它可以在Windows 7和Windows XP上使用。



注意:
i不在乎更改此文件的工作方式,甚至会丢失这种方式,尝试完全不同的方式在OS上运行(Windows 7和XP)。如果您向我建议新方法,我将很乐意尝试一下。



我的问题:


  1. 该程序如何在某些计算机上工作而在某些计算机上不工作?

  2. 您会将文件放在程序文件夹之外的其他位置吗? / li>

(对不起,我的英语)

解决方案

该程序如何在某些计算机上工作而又不能在某些计算机上工作?

因为该程序取决于计算机特有的功能,所以有所不同在两台计算机之间。例如,您的错误消息是:

文件未找到'C:\Users\eli\AppData\Roaming\fourmlinks.txt'


程序正在用户文件夹中寻找文件;特定于每台计算机上的每个用户,不同的用户和不同的计算机将具有不同的文件。

将文件放置在程序文件夹之外的其他位置吗? / blockquote>

我会吗?不,我不能,因为我不是那台计算机上的那个用户。如果您的意思是我的程序可以将文件放在其他位置吗……,那么可以。

 字符串路径= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fourmlinks.txt); 

这将获得登录用户的私人应用程序数据的路径。如果您的计算机上此文件夹中有 fourmlinks.txt文件,但其他人在其计算机上没有此文件,则该文件将在您的计算机上工作,而不是您所要求的其他人。

 字符串路径= Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()。Location),
fourmlinks 。文本);

这将获得安装文件的路径。如果您和其他人都安装了该程序,则您和其他人都将拥有该文件。如果您中的任何一个更改了文件,则另一个更改了。



因此,您应始终使用 GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly ()。位置)?好吧,这取决于。 GetExecutingAssembly()。Location 通常是标准用户无法写入,只能读取的位置。如果您希望用户仅读取而不写入数据,那么这是放置数据的好地方。



如果要创建供用户写入和读取的新文件,请写入 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)。如果要在应用程序中安装一些起始数据但允许用户更改它,则应在应用程序中包含起始数据文件。它将安装到 System.Reflection.Assembly.GetExecutingAssembly()。Location 。然后,您的应用程序应该执行以下操作:

  string fileName = fourmlinks.txt; 
string target = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),fileName);
if(!File.Exists(target))
{
字符串源= Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()。Location),fileName);
File.Copy(源,目标);
}

GetDirectoryName(System.Reflection。 Assembly.GetExecutingAssembly()。Location) Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)是前者是您安装程序包的一部分;如果用户卸载或更新了应用程序,则所有运行时更改都将被删除。被修改,则您要使用 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)


i made a program that uses a pre-made text file that hold a list of sites. now in some computer the program works fine but, in my friend computer it doesn't.

i check the program on 2 of my windows 7 computers, and 1 xp and i don't have any errors. this program was used for some time on XP, now my friend want to install it in his windows 7 computer at home, but the program doesn't find the file after he install the program.

this is the error he get:

System.IO.FileNotFoundException: file not found 'C:\Users\eli\AppData\Roaming\fourmlinks.txt'.
file name: 'C:\Users\eli\AppData\Roaming\fourmlinks.txt'

the thing is that i ship the this file in main program folder (Application Files), and it still cant find it.

this is the code i use to find the file when the program starts:

sring path = "";
path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";
            System.OperatingSystem osInfo = System.Environment.OSVersion;
            if (osInfo.Platform == PlatformID.Win32NT)
            {
                if (osInfo.Version.Major == 5 && osInfo.Version.Minor != 0)
                {
                    //running XP
                    //path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\fourmlinks.txt";
                    path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\fourmlinks.txt";
                }
            } 

you can see, that i tried to make sure it will work on windows 7 and, windows xp.

NOTE: i don't mind changing the way i work with this file, or even loss this way and try completely different way that will work on the OS's (win 7 and XP). if u suggest me new way, i will be glad to give a try out.

my questions:

  1. how is it possible that the program works in some computer and in some not?
  2. will you put the file in a different place other then the program folder?

(sorry for my English)

解决方案

How is it possible that the program works in some computer and in some not?

Because the program depends on something specific to a computer, and this something is different between the two computers. For example, your error message says:

file not found 'C:\Users\eli\AppData\Roaming\fourmlinks.txt'

The program is looking for a file in the Users folder; that is specific to every user on every machine, different users and different machines will have different files.

will you put the file in a different place other then the program folder

Will I? No, I can't because I'm not that user on that machine. If you mean "can my program put the file in a different place ..." then yes, it can.

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "fourmlinks.txt");

That will get a path to the logged-on user's private application data. If you have the "fourmlinks.txt" file in this folder on your machine but someone else doesn't have this file on their machine, then that will work on your machine but not on someone else's, as you asked.

string path = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
    "fourmlinks.txt");

That will get the path to the installed file. If both you and someone else install the program, then both you and someone else will have the file. If either of you change the file, then the other gets the changes.

So should you always use GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)? Well, that depends. GetExecutingAssembly().Location is typically a place standard users cannot write to, only read from. If you want users to only read and never write to your data, then that is a good place to put it. If you don't, then it isn't.

If you want to create a new file for users to write to and to read from, then write to Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData). If you want to install some starting data with your application but allow the user to change it, then you should include the starting data file in your application. It will get installed to System.Reflection.Assembly.GetExecutingAssembly().Location. Then your application should do something like:

string fileName = "fourmlinks.txt";
string target = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fileName);
if (!File.Exists(target))
{
    string source = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), fileName);
    File.Copy(source, target);
}

Another important difference between GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) and Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) is that the former is part of your installer packages; if the user uninstalls or updates your application all their runtime changes will be deleted.

If you want each user to have their own private data that does not get deleted when the application is modified, then you want to use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).

这篇关于安装后程序找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆