使用c#用asp.net中的旧文件替换现有文件 [英] replace existing file with old file in asp.net using c#

查看:211
本文介绍了使用c#用asp.net中的旧文件替换现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用c#将现有文件替换为asp.net中的旧文件。



我的代码如下;



string filepath = Application.StartupPath +\\Preseaintake\\ newtest.xls;

string filepath1 = Application.StartupPath +\\Preseaintake \\\ \\ test.xls;

string filepath2 = Application.StartupPath +\\Preseaintake \\ backup.xls;



if(System.IO.File.Exists(filepath))

File.Replace(filepath1,filepath,filepath2);



xlWorkBook.SaveAs(filepath1,misvalue,misvalue,misvalue,misvalue,misvalue,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,misvalue,misvalue,

misvalue,misvalue,misvalue);

xlWorkBook.Close(true,misvalue,misvalue);





i replaci使用新文件添加现有文件。



我写了上面的代码。但是,当我运行显示错误如下;



无法找到指定的文件。



错误在下面的行显示如下;



File.Replace(filepath1,filepath,filepath2);





来自我上面的代码我犯了什么错误。



请帮帮我。



问候,

Narasiman P.

Replace the existing file with old file in asp.net using c#.

My code as follows;

string filepath = Application.StartupPath + "\\Preseaintake\\newtest.xls";
string filepath1 = Application.StartupPath + "\\Preseaintake\\test.xls";
string filepath2 = Application.StartupPath + "\\Preseaintake\\backup.xls";

if (System.IO.File.Exists(filepath))
File.Replace(filepath1, filepath, filepath2);

xlWorkBook.SaveAs(filepath1, misvalue, misvalue, misvalue, misvalue, misvalue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misvalue, misvalue,
misvalue, misvalue, misvalue);
xlWorkBook.Close(true, misvalue, misvalue);


i replacing existing file with new file.

for that i written a code above. But when i run shows error as follows;

unable to find a specified file.

The error shows in below line as follows;

File.Replace(filepath1, filepath, filepath2);


from my above code what is the mistake i madde.

please help me.

Regards,
Narasiman P.

推荐答案



首先,重命名你的变量,以便名称含义。


First, rename your variables so that the names connote meaning.



使用try-catch块捕获异常。


Use a try-catch block to capture the exception.



以下内容会重写您的代码。发生异常时,将向您提供有关导致错误的原因的指示。我的猜测是你的源文件不存在。这反过来意味着你的存在测试需要针对的是源而不是目的地(就像你现在一样)。


The following rewrites your code. When the exception occurs, you will be provided with an indication as to what caused the error. My guess is that your source file does not exist. This in turn implies that your test for existence needs to be made against the source rather than the destination (as you have now).

string destination_filename = Application.StartupPath + "\\Preseaintake\\newtest.xls";
string source_filename = Application.StartupPath + "\\Preseaintake\\test.xls";
string backup_filename = Application.StartupPath + "\\Preseaintake\\backup.xls";

try
    {
    if ( File.Exists ( destination_filename ) )
        {
        File.Replace( source_filename, 
                      destination_filename, 
                      backup_filename );
        MessageBox.Show ( "Replaced" );
        }
    }
catch ( Exception ex )
    {
    MessageBox.Show ( ex.Message +
                      Environment.NewLine +
                      ex.StackTrace );
    }


这篇关于使用c#用asp.net中的旧文件替换现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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